1
0
Fork 0
mirror of https://github.com/mastodon/mastodon.git synced 2024-08-20 21:08:15 -07:00

Simplify matcher

This commit is contained in:
Matt Jankowski 2024-07-11 15:41:39 -04:00
parent f44e0a722d
commit 25baa364de
5 changed files with 15 additions and 22 deletions

View file

@ -57,8 +57,8 @@ describe AccountControllerConcern do
expect(assigns(:account)).to eq account expect(assigns(:account)).to eq account
expect(response) expect(response)
.to have_http_status(200) .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('http://test.host/.well-known/webfinger?resource=acct%3Ausername%40cb6e6126.ngrok.io', rel: 'lrdd', type: 'application/jrd+json')
.and have_http_link_header(:alternate, 'https://cb6e6126.ngrok.io/users/username').with_type('application/activity+json') .and have_http_link_header('https://cb6e6126.ngrok.io/users/username', rel: 'alternate', type: 'application/activity+json')
end end
end end
end end

View file

@ -69,7 +69,7 @@ describe 'Accounts show response' do
expect(response) expect(response)
.to have_http_status(200) .to have_http_status(200)
.and render_template(:show) .and render_template(:show)
.and have_http_link_header(:alternate, ActivityPub::TagManager.instance.uri_for(account)) .and have_http_link_header(ActivityPub::TagManager.instance.uri_for(account), rel: 'alternate')
end end
end end

View file

@ -10,8 +10,8 @@ describe 'Link headers' do
get short_account_path(username: account) get short_account_path(username: account)
expect(response) 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') .to have_http_link_header('http://www.example.com/.well-known/webfinger?resource=acct%3Atest%40cb6e6126.ngrok.io', rel: 'lrdd', type: 'application/jrd+json')
.and have_http_link_header(:alternate, 'https://cb6e6126.ngrok.io/users/test').with_type('application/activity+json') .and have_http_link_header('https://cb6e6126.ngrok.io/users/test', rel: 'alternate', type: 'application/activity+json')
end end
end end
end end

View file

@ -3,7 +3,7 @@
RSpec::Matchers.define :include_pagination_headers do |links| RSpec::Matchers.define :include_pagination_headers do |links|
match do |response| match do |response|
links.map do |key, value| links.map do |key, value|
expect(response).to have_http_link_header(key, value) expect(response).to have_http_link_header(value, rel: key.to_s)
end.all? end.all?
end end

View file

@ -1,15 +1,8 @@
# frozen_string_literal: true # frozen_string_literal: true
RSpec::Matchers.define :have_http_link_header do |rel, href| RSpec::Matchers.define :have_http_link_header do |href, **attrs|
chain :with_type do |type|
@type = type
end
match do |response| match do |response|
header_link = link_for(response, rel) link_for(response, attrs)&.href == href
header_link.href == href &&
(@type.nil? || header_link.attrs['type'] == @type)
end end
match_when_negated do |response| match_when_negated do |response|
@ -17,17 +10,17 @@ RSpec::Matchers.define :have_http_link_header do |rel, href|
end end
failure_message do |response| failure_message do |response|
(+'').tap do |string| "Expected `#{response.headers['Link']}` to include `href` value of `#{href}` for `#{attrs}` but it did not."
string << "Expected `#{response.headers['Link']}` to include `href` value of `#{href}` "
string << "with `type` of `#{@type}` " if @type.present?
string << "for `rel=#{rel}` but it did not."
end
end end
def link_for(response, rel) failure_message_when_negated do
"Expected response not to have a `Link` header but `#{response.headers['Link']}` is present."
end
def link_for(response, attrs)
LinkHeader LinkHeader
.parse(response.headers['Link']) .parse(response.headers['Link'])
.find_link(['rel', rel.to_s]) .find_link(*attrs.stringify_keys)
end end
end end