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.



268
269
270
271
272
273
274
275
276
# File 'lib/webmock/request_pattern.rb', line 268

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.



266
267
268
# File 'lib/webmock/request_pattern.rb', line 266

def pattern
  @pattern
end

Instance Method Details

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

Returns:

  • (Boolean)


278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/webmock/request_pattern.rb', line 278

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::HashIncludingMatcher)
    @pattern == body_as_hash(body, content_type)
  else
    empty_string?(@pattern) && empty_string?(body) ||
      @pattern == body ||
      @pattern === body
  end
end

#to_sObject



295
296
297
# File 'lib/webmock/request_pattern.rb', line 295

def to_s
  @pattern.inspect
end