class WebMock::RequestPattern
def assign_options(options)
def assign_options(options) options = WebMock::Util::HashKeysStringifier.stringify_keys!(options, deep: true) HashValidator.new(options).validate_keys('body', 'headers', 'query', 'basic_auth') set_basic_auth_as_headers!(options) @body_pattern = BodyPattern.new(options['body']) if options.has_key?('body') @headers_pattern = HeadersPattern.new(options['headers']) if options.has_key?('headers') @uri_pattern.add_query_params(options['query']) if options.has_key?('query') end
def create_uri_pattern(uri)
def create_uri_pattern(uri) if uri.is_a?(Regexp) URIRegexpPattern.new(uri) elsif uri.is_a?(Addressable::Template) URIAddressablePattern.new(uri) elsif uri.respond_to?(:call) URICallablePattern.new(uri) elsif uri.is_a?(::URI::Generic) URIStringPattern.new(uri.to_s) elsif uri.is_a?(String) URIStringPattern.new(uri) else raise ArgumentError.new("URI should be a String, Regexp, Addressable::Template or a callable object. Got: #{uri.class}") end end
def initialize(method, uri, options = {})
def initialize(method, uri, options = {}) @method_pattern = MethodPattern.new(method) @uri_pattern = create_uri_pattern(uri) @body_pattern = nil @headers_pattern = nil @with_block = nil assign_options(options) end
def matches?(request_signature)
def matches?(request_signature) content_type = request_signature.headers['Content-Type'] if request_signature.headers content_type = content_type.split(';').first if content_type @method_pattern.matches?(request_signature.method) && @uri_pattern.matches?(request_signature.uri) && (@body_pattern.nil? || @body_pattern.matches?(request_signature.body, content_type || "")) && (@headers_pattern.nil? || @headers_pattern.matches?(request_signature.headers)) && (@with_block.nil? || @with_block.call(request_signature)) end
def set_basic_auth_as_headers!(options)
def set_basic_auth_as_headers!(options) if basic_auth = options.delete('basic_auth') validate_basic_auth!(basic_auth) options['headers'] ||= {} options['headers']['Authorization'] = WebMock::Util::Headers.basic_auth_header(basic_auth[0],basic_auth[1]) end end
def to_s
def to_s string = "#{@method_pattern.to_s.upcase}".dup string << " #{@uri_pattern.to_s}" string << " with body #{@body_pattern.to_s}" if @body_pattern string << " with headers #{@headers_pattern.to_s}" if @headers_pattern string << " with given block" if @with_block string end
def validate_basic_auth!(basic_auth)
def validate_basic_auth!(basic_auth) if !basic_auth.is_a?(Array) || basic_auth.map{|e| e.is_a?(String)}.uniq != [true] raise "The basic_auth option value should be an array which contains 2 strings: username and password" end end
def with(options = {}, &block)
def with(options = {}, &block) raise ArgumentError.new('#with method invoked with no arguments. Either options hash or block must be specified. Created a block with do..end? Try creating it with curly braces {} instead.') if options.empty? && !block_given? assign_options(options) @with_block = block self end