Class: WebMock::BodyPattern

Inherits:
Object
  • Object
show all
Includes:
RSpecMatcherDetector
Defined in:
lib/webmock/request_pattern.rb

Constant Summary collapse

BODY_FORMATS =
{
  'text/xml'               => :xml,
  'application/xml'        => :xml,
  'application/json'       => :json,
  'text/json'              => :json,
  'application/javascript' => :json,
  'text/javascript'        => :json,
  'text/html'              => :html,
  'application/x-yaml'     => :yaml,
  'text/yaml'              => :yaml,
  'text/plain'             => :plain
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from RSpecMatcherDetector

#rSpecHashExcludingMatcher?, #rSpecHashIncludingMatcher?

Constructor Details

#initialize(pattern) ⇒ BodyPattern

Returns a new instance of BodyPattern.



273
274
275
276
277
278
279
280
281
# File 'lib/webmock/request_pattern.rb', line 273

def initialize(pattern)
  @pattern = if pattern.is_a?(Hash)
    normalize_hash(pattern)
  elsif rSpecHashIncludingMatcher?(pattern)
    WebMock::Matchers::HashIncludingMatcher.from_rspec_matcher(pattern)
  else
    pattern
  end
end

Instance Attribute Details

#patternObject (readonly)

Returns the value of attribute pattern.



271
272
273
# File 'lib/webmock/request_pattern.rb', line 271

def pattern
  @pattern
end

Instance Method Details

#matches?(body, content_type = "") ⇒ Boolean

Returns:

  • (Boolean)


283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/webmock/request_pattern.rb', line 283

def matches?(body, content_type = "")
  assert_non_multipart_body(content_type)

  if (@pattern).is_a?(Hash)
    return true if @pattern.empty?
    matching_body_hashes?(body_as_hash(body, content_type), @pattern, content_type)
  elsif (@pattern).is_a?(Array)
    matching_body_array?(body_as_hash(body, content_type), @pattern, content_type)
  elsif (@pattern).is_a?(WebMock::Matchers::HashArgumentMatcher)
    @pattern == body_as_hash(body, content_type)
  else
    empty_string?(@pattern) && empty_string?(body) ||
      @pattern == body ||
      @pattern === body
  end
end

#to_sObject



300
301
302
# File 'lib/webmock/request_pattern.rb', line 300

def to_s
  @pattern.inspect
end