class WebMock::BodyPattern

def assert_non_multipart_body(content_type)

def assert_non_multipart_body(content_type)
  if content_type =~ %r{^multipart/form-data}
    raise ArgumentError.new("WebMock does not support matching body for multipart/form-data requests yet :(")
  end
end

def body_as_hash(body, content_type)

def body_as_hash(body, content_type)
  case body_format(content_type)
  when :json then
    WebMock::Util::Parsers::JSON.parse(body)
  when :xml then
    WebMock::Util::Parsers::XML.parse(body)
  else
    WebMock::Util::QueryMapper.query_to_values(body, notation: Config.instance.query_values_notation)
  end
rescue WebMock::Util::Parsers::ParseError
  nil
end

def body_format(content_type)

def body_format(content_type)
  normalized_content_type = content_type.sub(/\A(application\/)[a-zA-Z0-9.-]+\+(json|xml)\Z/,'\1\2')
  BODY_FORMATS[normalized_content_type]
end

def empty_string?(string)

def empty_string?(string)
  string.nil? || string == ""
end

def initialize(pattern)

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

def matches?(body, content_type = "")

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

def matching_body_array?(query_parameters, pattern, content_type)

def matching_body_array?(query_parameters, pattern, content_type)
  return false unless query_parameters.is_a?(Array)
  return false unless query_parameters.length == pattern.length
  query_parameters.each_with_index do |actual, index|
    expected = pattern[index]
    return false unless matching_values(actual, expected, content_type)
  end
  true
end

def matching_body_hashes?(query_parameters, pattern, content_type)

Returns:
  • (Boolean) - true if the paramaters match the comparison

Parameters:
  • pattern (Hash) -- which contains keys with a string, hash or
  • query_parameters (Hash) -- typically the result of parsing
def matching_body_hashes?(query_parameters, pattern, content_type)
  return false unless query_parameters.is_a?(Hash)
  return false unless query_parameters.keys.sort == pattern.keys.sort
  query_parameters.all? do |key, actual|
    expected = pattern[key]
    matching_values(actual, expected, content_type)
  end
end

def matching_values(actual, expected, content_type)

def matching_values(actual, expected, content_type)
  return matching_body_hashes?(actual, expected, content_type) if actual.is_a?(Hash) && expected.is_a?(Hash)
  return matching_body_array?(actual, expected, content_type) if actual.is_a?(Array) && expected.is_a?(Array)
  expected = WebMock::Util::ValuesStringifier.stringify_values(expected) if url_encoded_body?(content_type)
  expected === actual
end

def normalize_hash(hash)

def normalize_hash(hash)
  Hash[WebMock::Util::HashKeysStringifier.stringify_keys!(hash, deep: true).sort]
end

def to_s

def to_s
  @pattern.inspect
end

def url_encoded_body?(content_type)

def url_encoded_body?(content_type)
  content_type =~ %r{^application/x-www-form-urlencoded}
end