Add include_pagination_headers matcher to check Link header in api specs (#29596)

This commit is contained in:
Matt Jankowski 2024-03-15 06:17:45 -04:00 committed by GitHub
parent 6865fda593
commit 2e91a9bd34
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 67 additions and 46 deletions

View file

@ -38,16 +38,14 @@ RSpec.describe 'Blocks' do
expect(body_as_json.size).to eq(params[:limit]) expect(body_as_json.size).to eq(params[:limit])
end end
it 'sets the correct pagination header for the prev path' do it 'sets correct link header pagination' do
subject subject
expect(response.headers['Link'].find_link(%w(rel prev)).href).to eq(api_v1_blocks_url(limit: params[:limit], since_id: blocks.last.id)) expect(response)
end .to include_pagination_headers(
prev: api_v1_blocks_url(limit: params[:limit], since_id: blocks.last.id),
it 'sets the correct pagination header for the next path' do next: api_v1_blocks_url(limit: params[:limit], max_id: blocks.second.id)
subject )
expect(response.headers['Link'].find_link(%w(rel next)).href).to eq(api_v1_blocks_url(limit: params[:limit], max_id: blocks[1].id))
end end
end end

View file

@ -42,9 +42,14 @@ RSpec.describe 'Bookmarks' do
it 'paginates correctly', :aggregate_failures do it 'paginates correctly', :aggregate_failures do
subject subject
expect(body_as_json.size).to eq(params[:limit]) expect(body_as_json.size)
expect(response.headers['Link'].find_link(%w(rel prev)).href).to eq(api_v1_bookmarks_url(limit: params[:limit], min_id: bookmarks.last.id)) .to eq(params[:limit])
expect(response.headers['Link'].find_link(%w(rel next)).href).to eq(api_v1_bookmarks_url(limit: params[:limit], max_id: bookmarks[1].id))
expect(response)
.to include_pagination_headers(
prev: api_v1_bookmarks_url(limit: params[:limit], min_id: bookmarks.last.id),
next: api_v1_bookmarks_url(limit: params[:limit], max_id: bookmarks.second.id)
)
end end
end end

View file

@ -45,16 +45,14 @@ RSpec.describe 'Favourites' do
expect(body_as_json.size).to eq(params[:limit]) expect(body_as_json.size).to eq(params[:limit])
end end
it 'sets the correct pagination header for the prev path' do it 'sets the correct pagination headers' do
subject subject
expect(response.headers['Link'].find_link(%w(rel prev)).href).to eq(api_v1_favourites_url(limit: params[:limit], min_id: favourites.last.id)) expect(response)
end .to include_pagination_headers(
prev: api_v1_favourites_url(limit: params[:limit], min_id: favourites.last.id),
it 'sets the correct pagination header for the next path' do next: api_v1_favourites_url(limit: params[:limit], max_id: favourites.second.id)
subject )
expect(response.headers['Link'].find_link(%w(rel next)).href).to eq(api_v1_favourites_url(limit: params[:limit], max_id: favourites[1].id))
end end
end end

View file

@ -49,16 +49,14 @@ RSpec.describe 'Followed tags' do
expect(body_as_json.size).to eq(params[:limit]) expect(body_as_json.size).to eq(params[:limit])
end end
it 'sets the correct pagination header for the prev path' do it 'sets the correct pagination headers' do
subject subject
expect(response.headers['Link'].find_link(%w(rel prev)).href).to eq(api_v1_followed_tags_url(limit: params[:limit], since_id: tag_follows.last.id)) expect(response)
end .to include_pagination_headers(
prev: api_v1_followed_tags_url(limit: params[:limit], since_id: tag_follows.last.id),
it 'sets the correct pagination header for the next path' do next: api_v1_followed_tags_url(limit: params[:limit], max_id: tag_follows.last.id)
subject )
expect(response.headers['Link'].find_link(%w(rel next)).href).to eq(api_v1_followed_tags_url(limit: params[:limit], max_id: tag_follows.last.id))
end end
end end
end end

View file

@ -44,10 +44,11 @@ RSpec.describe 'Mutes' do
it 'sets the correct pagination headers', :aggregate_failures do it 'sets the correct pagination headers', :aggregate_failures do
subject subject
headers = response.headers['Link'] expect(response)
.to include_pagination_headers(
expect(headers.find_link(%w(rel prev)).href).to eq(api_v1_mutes_url(limit: params[:limit], since_id: mutes.last.id.to_s)) prev: api_v1_mutes_url(limit: params[:limit], since_id: mutes.last.id),
expect(headers.find_link(%w(rel next)).href).to eq(api_v1_mutes_url(limit: params[:limit], max_id: mutes.last.id.to_s)) next: api_v1_mutes_url(limit: params[:limit], max_id: mutes.last.id)
)
end end
end end

View file

@ -98,9 +98,14 @@ RSpec.describe 'Notifications' do
notifications = user.account.notifications notifications = user.account.notifications
expect(body_as_json.size).to eq(params[:limit]) expect(body_as_json.size)
expect(response.headers['Link'].find_link(%w(rel prev)).href).to eq(api_v1_notifications_url(limit: params[:limit], min_id: notifications.last.id.to_s)) .to eq(params[:limit])
expect(response.headers['Link'].find_link(%w(rel next)).href).to eq(api_v1_notifications_url(limit: params[:limit], max_id: notifications[2].id.to_s))
expect(response)
.to include_pagination_headers(
prev: api_v1_notifications_url(limit: params[:limit], min_id: notifications.last.id),
next: api_v1_notifications_url(limit: params[:limit], max_id: notifications[2].id)
)
end end
end end

View file

@ -55,10 +55,11 @@ describe 'Home', :sidekiq_inline do
it 'sets the correct pagination headers', :aggregate_failures do it 'sets the correct pagination headers', :aggregate_failures do
subject subject
headers = response.headers['Link'] expect(response)
.to include_pagination_headers(
expect(headers.find_link(%w(rel prev)).href).to eq(api_v1_timelines_home_url(limit: 1, min_id: ana.statuses.first.id.to_s)) prev: api_v1_timelines_home_url(limit: params[:limit], min_id: ana.statuses.first.id),
expect(headers.find_link(%w(rel next)).href).to eq(api_v1_timelines_home_url(limit: 1, max_id: ana.statuses.first.id.to_s)) next: api_v1_timelines_home_url(limit: params[:limit], max_id: ana.statuses.first.id)
)
end end
end end
end end

View file

@ -83,10 +83,11 @@ describe 'Public' do
it 'sets the correct pagination headers', :aggregate_failures do it 'sets the correct pagination headers', :aggregate_failures do
subject subject
headers = response.headers['Link'] expect(response)
.to include_pagination_headers(
expect(headers.find_link(%w(rel prev)).href).to eq(api_v1_timelines_public_url(limit: 1, min_id: media_status.id.to_s)) prev: api_v1_timelines_public_url(limit: params[:limit], min_id: media_status.id),
expect(headers.find_link(%w(rel next)).href).to eq(api_v1_timelines_public_url(limit: 1, max_id: media_status.id.to_s)) next: api_v1_timelines_public_url(limit: params[:limit], max_id: media_status.id)
)
end end
end end
end end

View file

@ -71,10 +71,11 @@ RSpec.describe 'Tag' do
it 'sets the correct pagination headers', :aggregate_failures do it 'sets the correct pagination headers', :aggregate_failures do
subject subject
headers = response.headers['Link'] expect(response)
.to include_pagination_headers(
expect(headers.find_link(%w(rel prev)).href).to eq(api_v1_timelines_tag_url(limit: 1, min_id: love_status.id.to_s)) prev: api_v1_timelines_tag_url(limit: params[:limit], min_id: love_status.id),
expect(headers.find_link(%w(rel next)).href).to eq(api_v1_timelines_tag_url(limit: 1, max_id: love_status.id.to_s)) next: api_v1_timelines_tag_url(limit: params[:limit], max_id: love_status.id)
)
end end
end end

View file

@ -0,0 +1,13 @@
# frozen_string_literal: true
RSpec::Matchers.define :include_pagination_headers do |links|
match do |response|
links.map do |key, value|
response.headers['Link'].find_link(['rel', key.to_s]).href == value
end.all?
end
failure_message do |header|
"expected that #{header} would have the same values as #{links}."
end
end