class VCR::RequestMatcherRegistry
Keeps track of the different request matchers.
def [](matcher)
- Private: -
def [](matcher) @registry.fetch(matcher) do matcher.respond_to?(:call) ? Matcher.new(matcher) : raise_unregistered_matcher_error(matcher) end end
def initialize
- Private: -
def initialize @registry = {} register_built_ins end
def raise_unregistered_matcher_error(name)
def raise_unregistered_matcher_error(name) raise Errors::UnregisteredMatcherError.new \ "There is no matcher registered for #{name.inspect}. " + "Did you mean one of #{@registry.keys.map(&:inspect).join(', ')}?" end
def register(name, &block)
- Private: -
def register(name, &block) if @registry.has_key?(name) warn "WARNING: There is already a VCR request matcher registered for #{name.inspect}. Overriding it." end @registry[name] = Matcher.new(block) end
def register_built_ins
def register_built_ins register(:method) { |r1, r2| r1.method == r2.method } register(:uri) { |r1, r2| r1.parsed_uri == r2.parsed_uri } register(:body) { |r1, r2| r1.body == r2.body } register(:headers) { |r1, r2| r1.headers == r2.headers } register(:host) do |r1, r2| r1.parsed_uri.host.chomp('.') == r2.parsed_uri.host.chomp('.') end register(:path) do |r1, r2| r1.parsed_uri.path == r2.parsed_uri.path end register(:query) do |r1, r2| VCR.configuration.query_parser.call(r1.parsed_uri.query.to_s) == VCR.configuration.query_parser.call(r2.parsed_uri.query.to_s) end try_to_register_body_as_json end
def try_to_register_body_as_json
def try_to_register_body_as_json begin require 'json' rescue LoadError return end register(:body_as_json) do |r1, r2| begin r1.body == r2.body || JSON.parse(r1.body) == JSON.parse(r2.body) rescue JSON::ParserError false end end end
def uri_without_param_matchers
def uri_without_param_matchers @uri_without_param_matchers ||= Hash.new do |hash, params| params = params.map(&:to_s) hash[params] = URIWithoutParamsMatcher.new(params) end end
def uri_without_params(*ignores)
-
(#call)
- the request matcher
Parameters:
-
ignores
(Array<#to_s>
) -- The names of the query parameters to ignore
def uri_without_params(*ignores) uri_without_param_matchers[ignores] end