class RSpec::HttpHelpers::HttpQuerystringMatcher

@api private

Public Class Methods

new(expected, colorize: true) click to toggle source
# File lib/rspec/http_helpers/http_querystring_matcher.rb, line 13
def initialize(expected, colorize: true)
  @expected = querystring_from(expected)
  @diff_format = colorize ? :color : :text
rescue ArgumentError => error
  raise "Invalid expected value: #{error.message}"
end

Public Instance Methods

failure_message() click to toggle source
# File lib/rspec/http_helpers/http_querystring_matcher.rb, line 30
def failure_message
  @wrong_type_error_message || diff_error_message
end
failure_message_when_negated() click to toggle source
# File lib/rspec/http_helpers/http_querystring_matcher.rb, line 34
def failure_message_when_negated
  "expected querystring not to match #{@expected}"
end
matches?(actual) click to toggle source
# File lib/rspec/http_helpers/http_querystring_matcher.rb, line 20
def matches?(actual)
  @actual = querystring_from(actual)
  @expected == @actual
rescue ArgumentError
  got = actual.is_a?(String) ? actual : actual.class
  @wrong_type_error_message = 'expected URI with querystring ' \
    "matching #{@expected} got #{got}"
  false
end

Private Instance Methods

diff() click to toggle source
# File lib/rspec/http_helpers/http_querystring_matcher.rb, line 64
def diff
  expected = @expected.split('&').join("\n") + "\n"
  actual = @actual.split('&').join("\n") + "\n"
  Diffy::Diff.new(expected, actual).to_s(@diff_format)
end
diff_error_message() click to toggle source
# File lib/rspec/http_helpers/http_querystring_matcher.rb, line 56
def diff_error_message
  message = StringIO.new
  message << "expected: #{@expected}\n\n"
  message << "got: #{@actual}\n\n"
  message << "Diff:\n#{diff}"
  message.string
end
querystring_from(value) click to toggle source
# File lib/rspec/http_helpers/http_querystring_matcher.rb, line 52
def querystring_from(value)
  HttpHelpers.normalize_querystring(uri_from(value).query)
end
uri_from(value) click to toggle source
# File lib/rspec/http_helpers/http_querystring_matcher.rb, line 40
def uri_from(value)
  case value
  when nil then URI.parse('')
  when /^https:/ then URI.parse(value)
  when /\?/ then URI.parse(value)
  when String then URI.parse("?#{value}")
  when URI::Generic then value
  else
    raise ArgumentError, "expected String<URI> or URI, got #{value.class}"
  end
end