0
0
Fork 0

Add have_http_link_header matcher and set header values as strings (#31010)

This commit is contained in:
Matt Jankowski 2024-09-05 16:05:38 -04:00 committed by GitHub
parent 09017dd8f0
commit 7efe0bde9d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 54 additions and 39 deletions

View file

@ -3,7 +3,7 @@
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
expect(response).to have_http_link_header(value).for(rel: key.to_s)
end.all?
end

View file

@ -0,0 +1,33 @@
# frozen_string_literal: true
RSpec::Matchers.define :have_http_link_header do |href|
match do |response|
@response = response
header_link&.href == href
end
match_when_negated do |response|
response.headers['Link'].blank?
end
chain :for do |attributes|
@attributes = attributes
end
failure_message do |response|
"Expected `#{response.headers['Link']}` to include `href` value of `#{href}` for `#{@attributes}` but it did not."
end
failure_message_when_negated do
"Expected response not to have a `Link` header but `#{response.headers['Link']}` is present."
end
def header_link
LinkHeader
.parse(@response.headers['Link'])
.find_link(*@attributes.stringify_keys)
end
end
RSpec::Matchers.define_negated_matcher :not_have_http_link_header, :have_http_link_header # Allow chaining