From f44e0a722d53f332bbeeca27087b0da4762c7e4d Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Thu, 11 Jul 2024 12:21:15 -0400 Subject: [PATCH] Use http link header matcher --- .../account_controller_concern_spec.rb | 13 ++++------ spec/requests/accounts_spec.rb | 3 +-- spec/requests/api/v1/timelines/home_spec.rb | 5 ++-- spec/requests/api/v1/timelines/list_spec.rb | 5 ++-- spec/requests/link_headers_spec.rb | 24 ++++--------------- 5 files changed, 15 insertions(+), 35 deletions(-) diff --git a/spec/controllers/concerns/account_controller_concern_spec.rb b/spec/controllers/concerns/account_controller_concern_spec.rb index 6eb970dedbb..a6e8d162a6b 100644 --- a/spec/controllers/concerns/account_controller_concern_spec.rb +++ b/spec/controllers/concerns/account_controller_concern_spec.rb @@ -55,15 +55,10 @@ describe AccountControllerConcern do get 'success', params: { account_username: account.username } expect(assigns(:account)).to eq account - expect(response).to have_http_status(200) - expect(response.headers['Link'].to_s).to eq(expected_link_headers) - end - - def expected_link_headers - [ - '; rel="lrdd"; type="application/jrd+json"', - '; rel="alternate"; type="application/activity+json"', - ].join(', ') + expect(response) + .to have_http_status(200) + .and have_http_link_header(:lrdd, 'http://test.host/.well-known/webfinger?resource=acct%3Ausername%40cb6e6126.ngrok.io').with_type('application/jrd+json') + .and have_http_link_header(:alternate, 'https://cb6e6126.ngrok.io/users/username').with_type('application/activity+json') end end end diff --git a/spec/requests/accounts_spec.rb b/spec/requests/accounts_spec.rb index bf067cdc38a..31d913ee91c 100644 --- a/spec/requests/accounts_spec.rb +++ b/spec/requests/accounts_spec.rb @@ -69,8 +69,7 @@ describe 'Accounts show response' do expect(response) .to have_http_status(200) .and render_template(:show) - - expect(response.headers['Link'].to_s).to include ActivityPub::TagManager.instance.uri_for(account) + .and have_http_link_header(:alternate, ActivityPub::TagManager.instance.uri_for(account)) end end diff --git a/spec/requests/api/v1/timelines/home_spec.rb b/spec/requests/api/v1/timelines/home_spec.rb index 96bd153affe..29de79057ee 100644 --- a/spec/requests/api/v1/timelines/home_spec.rb +++ b/spec/requests/api/v1/timelines/home_spec.rb @@ -94,8 +94,9 @@ describe 'Home', :inline_jobs do it 'returns http unprocessable entity', :aggregate_failures do subject - expect(response).to have_http_status(422) - expect(response.headers['Link']).to be_nil + expect(response) + .to have_http_status(422) + .and not_have_http_link_header end end end diff --git a/spec/requests/api/v1/timelines/list_spec.rb b/spec/requests/api/v1/timelines/list_spec.rb index 98d24567459..8987d6dbf25 100644 --- a/spec/requests/api/v1/timelines/list_spec.rb +++ b/spec/requests/api/v1/timelines/list_spec.rb @@ -47,8 +47,9 @@ describe 'API V1 Timelines List' do it 'returns http unprocessable entity' do get "/api/v1/timelines/list/#{list.id}", headers: headers - expect(response).to have_http_status(422) - expect(response.headers['Link']).to be_nil + expect(response) + .to have_http_status(422) + .and not_have_http_link_header end end end diff --git a/spec/requests/link_headers_spec.rb b/spec/requests/link_headers_spec.rb index b822adbfb85..304955e9e08 100644 --- a/spec/requests/link_headers_spec.rb +++ b/spec/requests/link_headers_spec.rb @@ -6,28 +6,12 @@ describe 'Link headers' do describe 'on the account show page' do let(:account) { Fabricate(:account, username: 'test') } - before do + it 'contains webfinger and activitypub urls in link header' do get short_account_path(username: account) - end - it 'contains webfinger url in link header' do - link_header = link_header_with_type('application/jrd+json') - - expect(link_header.href).to eq 'http://www.example.com/.well-known/webfinger?resource=acct%3Atest%40cb6e6126.ngrok.io' - expect(link_header.attr_pairs.first).to eq %w(rel lrdd) - end - - it 'contains activitypub url in link header' do - link_header = link_header_with_type('application/activity+json') - - expect(link_header.href).to eq 'https://cb6e6126.ngrok.io/users/test' - expect(link_header.attr_pairs.first).to eq %w(rel alternate) - end - - def link_header_with_type(type) - LinkHeader.parse(response.headers['Link'].to_s).links.find do |link| - link.attr_pairs.any?(['type', type]) - end + expect(response) + .to have_http_link_header(:lrdd, 'http://www.example.com/.well-known/webfinger?resource=acct%3Atest%40cb6e6126.ngrok.io').with_type('application/jrd+json') + .and have_http_link_header(:alternate, 'https://cb6e6126.ngrok.io/users/test').with_type('application/activity+json') end end end