Module: HttpDecoy::BodyMatcher

Defined in:
lib/http_decoy/body_matcher.rb

Overview

Shared by the RSpec have_received_request(...).with(body:) chain and the Minitest assert_received_request(..., body:) keyword — matches a logged request body against either an exact Hash of expected key/value matchers (each value tested via ===, so RSpec matchers, classes, and regexes all work) or a single matcher applied to the whole body.

Class Method Summary collapse

Class Method Details

.matches?(actual, matcher) ⇒ Boolean

rubocop:disable Style/CaseEquality -- === is the point: callers pass RSpec matchers, Regexp, Class, or plain values as matcher/v.

Returns:

  • (Boolean)


14
15
16
17
18
19
20
21
22
23
24
# File 'lib/http_decoy/body_matcher.rb', line 14

def matches?(actual, matcher)
  case matcher
  when Hash
    actual.is_a?(Hash) && matcher.all? do |k, v|
      actual_val = actual[k] || actual[k.to_s]
      v === actual_val
    end
  else
    matcher === actual
  end
end