Add have_http_link_header
matcher and set header values as strings (#31010)
This commit is contained in:
parent
09017dd8f0
commit
7efe0bde9d
10 changed files with 54 additions and 39 deletions
|
@ -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
|
||||
|
||||
|
|
33
spec/support/matchers/http_link_header.rb
Normal file
33
spec/support/matchers/http_link_header.rb
Normal 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
|
Loading…
Add table
Add a link
Reference in a new issue