http_link_header.rb 849 B

123456789101112131415161718192021222324252627282930313233
  1. # frozen_string_literal: true
  2. RSpec::Matchers.define :have_http_link_header do |href|
  3. match do |response|
  4. @response = response
  5. header_link&.href == href
  6. end
  7. match_when_negated do |response|
  8. response.headers['Link'].blank?
  9. end
  10. chain :for do |attributes|
  11. @attributes = attributes
  12. end
  13. failure_message do |response|
  14. "Expected `#{response.headers['Link']}` to include `href` value of `#{href}` for `#{@attributes}` but it did not."
  15. end
  16. failure_message_when_negated do
  17. "Expected response not to have a `Link` header but `#{response.headers['Link']}` is present."
  18. end
  19. def header_link
  20. LinkHeader
  21. .parse(@response.headers['Link'])
  22. .find_link(*@attributes.stringify_keys)
  23. end
  24. end
  25. RSpec::Matchers.define_negated_matcher :not_have_http_link_header, :have_http_link_header # Allow chaining