How to use key method of WebMock.Util Package

Best Webmock_ruby code snippet using WebMock.Util.key

request_pattern.rb

Source:request_pattern.rb Github

copy

Full Screen

...38 string39 end40 private41 def assign_options(options)42 options = WebMock::Util::HashKeysStringifier.stringify_keys!(options, deep: true)43 HashValidator.new(options).validate_keys('body', 'headers', 'query', 'basic_auth')44 set_basic_auth_as_headers!(options)45 @body_pattern = BodyPattern.new(options['body']) if options.has_key?('body')46 @headers_pattern = HeadersPattern.new(options['headers']) if options.has_key?('headers')47 @uri_pattern.add_query_params(options['query']) if options.has_key?('query')48 end49 def set_basic_auth_as_headers!(options)50 if basic_auth = options.delete('basic_auth')51 validate_basic_auth!(basic_auth)52 options['headers'] ||= {}53 options['headers']['Authorization'] = WebMock::Util::Headers.basic_auth_header(basic_auth[0],basic_auth[1])54 end55 end56 def validate_basic_auth!(basic_auth)57 if !basic_auth.is_a?(Array) || basic_auth.map{|e| e.is_a?(String)}.uniq != [true]58 raise "The basic_auth option value should be an array which contains 2 strings: username and password"59 end60 end61 def create_uri_pattern(uri)62 if uri.is_a?(Regexp)63 URIRegexpPattern.new(uri)64 elsif uri.is_a?(Addressable::Template)65 URIAddressablePattern.new(uri)66 else67 URIStringPattern.new(uri)68 end69 end70 end71 class MethodPattern72 def initialize(pattern)73 @pattern = pattern74 end75 def matches?(method)76 @pattern == method || @pattern == :any77 end78 def to_s79 @pattern.to_s80 end81 end82 class URIPattern83 include RSpecMatcherDetector84 def initialize(pattern)85 @pattern = case pattern86 when Addressable::URI, Addressable::Template87 pattern88 else89 WebMock::Util::URI.normalize_uri(pattern)90 end91 @query_params = nil92 end93 def add_query_params(query_params)94 @query_params = if query_params.is_a?(Hash)95 query_params96 elsif query_params.is_a?(WebMock::Matchers::HashIncludingMatcher)97 query_params98 elsif rSpecHashIncludingMatcher?(query_params)99 WebMock::Matchers::HashIncludingMatcher.from_rspec_matcher(query_params)100 else101 WebMock::Util::QueryMapper.query_to_values(query_params, notation: Config.instance.query_values_notation)102 end103 end104 def to_s105 str = @pattern.inspect106 str += " with query params #{@query_params.inspect}" if @query_params107 str108 end109 end110 class URIRegexpPattern < URIPattern111 def matches?(uri)112 WebMock::Util::URI.variations_of_uri_as_strings(uri).any? { |u| u.match(@pattern) } &&113 (@query_params.nil? || @query_params == WebMock::Util::QueryMapper.query_to_values(uri.query, notation: Config.instance.query_values_notation))114 end115 def to_s116 str = @pattern.inspect117 str += " with query params #{@query_params.inspect}" if @query_params118 str119 end120 end121 class URIAddressablePattern < URIPattern122 def matches?(uri)123 if @query_params.nil?124 # Let Addressable check the whole URI125 WebMock::Util::URI.variations_of_uri_as_strings(uri).any? { |u| @pattern.match(u) }126 else127 # WebMock checks the query, Addressable checks everything else128 WebMock::Util::URI.variations_of_uri_as_strings(uri.omit(:query)).any? { |u| @pattern.match(u) } &&129 @query_params == WebMock::Util::QueryMapper.query_to_values(uri.query)130 end131 end132 def add_query_params(query_params)133 warn "WebMock warning: ignoring query params in RFC 6570 template and checking them with WebMock"134 super(query_params)135 end136 def to_s137 str = @pattern.pattern.inspect138 str += " with variables #{@pattern.variables.inspect}" if @pattern.variables139 str140 end141 end142 class URIStringPattern < URIPattern143 def matches?(uri)144 if @pattern.is_a?(Addressable::URI)145 if @query_params146 uri.omit(:query) === @pattern &&147 (@query_params.nil? || @query_params == WebMock::Util::QueryMapper.query_to_values(uri.query, notation: Config.instance.query_values_notation))148 else149 uri === @pattern150 end151 else152 false153 end154 end155 def add_query_params(query_params)156 super157 if @query_params.is_a?(Hash) || @query_params.is_a?(String)158 query_hash = (WebMock::Util::QueryMapper.query_to_values(@pattern.query, notation: Config.instance.query_values_notation) || {}).merge(@query_params)159 @pattern.query = WebMock::Util::QueryMapper.values_to_query(query_hash, notation: WebMock::Config.instance.query_values_notation)160 @query_params = nil161 end162 end163 def to_s164 str = WebMock::Util::URI.strip_default_port_from_uri_string(@pattern.to_s)165 str += " with query params #{@query_params.inspect}" if @query_params166 str167 end168 end169 class BodyPattern170 include RSpecMatcherDetector171 BODY_FORMATS = {172 'text/xml' => :xml,173 'application/xml' => :xml,174 'application/json' => :json,175 'text/json' => :json,176 'application/javascript' => :json,177 'text/javascript' => :json,178 'text/html' => :html,179 'application/x-yaml' => :yaml,180 'text/yaml' => :yaml,181 'text/plain' => :plain182 }183 attr_reader :pattern184 def initialize(pattern)185 @pattern = if pattern.is_a?(Hash)186 normalize_hash(pattern)187 elsif rSpecHashIncludingMatcher?(pattern)188 WebMock::Matchers::HashIncludingMatcher.from_rspec_matcher(pattern)189 else190 pattern191 end192 end193 def matches?(body, content_type = "")194 assert_non_multipart_body(content_type)195 if (@pattern).is_a?(Hash)196 return true if @pattern.empty?197 matching_hashes?(body_as_hash(body, content_type), @pattern)198 elsif (@pattern).is_a?(WebMock::Matchers::HashIncludingMatcher)199 @pattern == body_as_hash(body, content_type)200 else201 empty_string?(@pattern) && empty_string?(body) ||202 @pattern == body ||203 @pattern === body204 end205 end206 def to_s207 @pattern.inspect208 end209 private210 def body_as_hash(body, content_type)211 case BODY_FORMATS[content_type]212 when :json then213 WebMock::Util::JSON.parse(body)214 when :xml then215 Crack::XML.parse(body)216 else217 WebMock::Util::QueryMapper.query_to_values(body, notation: Config.instance.query_values_notation)218 end219 end220 def assert_non_multipart_body(content_type)221 if content_type =~ %r{^multipart/form-data}222 raise ArgumentError.new("WebMock does not support matching body for multipart/form-data requests yet :(")223 end224 end225 # Compare two hashes for equality226 #227 # For two hashes to match they must have the same length and all228 # values must match when compared using `#===`.229 #230 # The following hashes are examples of matches:231 #232 # {a: /\d+/} and {a: '123'}233 #234 # {a: '123'} and {a: '123'}235 #236 # {a: {b: /\d+/}} and {a: {b: '123'}}237 #238 # {a: {b: 'wow'}} and {a: {b: 'wow'}}239 #240 # @param [Hash] query_parameters typically the result of parsing241 # JSON, XML or URL encoded parameters.242 #243 # @param [Hash] pattern which contains keys with a string, hash or244 # regular expression value to use for comparison.245 #246 # @return [Boolean] true if the paramaters match the comparison247 # hash, false if not.248 def matching_hashes?(query_parameters, pattern)249 return false unless query_parameters.is_a?(Hash)250 return false unless query_parameters.keys.sort == pattern.keys.sort251 query_parameters.each do |key, actual|252 expected = pattern[key]253 if actual.is_a?(Hash) && expected.is_a?(Hash)254 return false unless matching_hashes?(actual, expected)255 else256 return false unless expected === actual257 end258 end259 true260 end261 def empty_string?(string)262 string.nil? || string == ""263 end264 def normalize_hash(hash)265 Hash[WebMock::Util::HashKeysStringifier.stringify_keys!(hash, deep: true).sort]266 end267 end268 class HeadersPattern269 def initialize(pattern)270 @pattern = WebMock::Util::Headers.normalize_headers(pattern) || {}271 end272 def matches?(headers)273 if empty_headers?(@pattern)274 empty_headers?(headers)275 else276 return false if empty_headers?(headers)277 @pattern.each do |key, value|278 return false unless headers.has_key?(key) && value === headers[key]279 end280 true281 end282 end283 def to_s284 WebMock::Util::Headers.sorted_headers_string(@pattern)285 end286 private287 def empty_headers?(headers)288 headers.nil? || headers == {}289 end290 end291end...

Full Screen

Full Screen

key

Using AI Code Generation

copy

Full Screen

1WebMock::Util::QueryMapper.query_to_values('a=1&b=2')2WebMock::Util::QueryMapper.query_to_values('a=1&b=2')3WebMock::Util::QueryMapper.query_to_values('a=1&b=2')4WebMock::Util::QueryMapper.query_to_values('a=1&b=2')5WebMock::Util::QueryMapper.query_to_values('a=1&b=2')6WebMock::Util::QueryMapper.query_to_values('a=1&b=2')7WebMock::Util::QueryMapper.query_to_values('a=1&b=2')8WebMock::Util::QueryMapper.query_to_values('a=1&b=2')

Full Screen

Full Screen

key

Using AI Code Generation

copy

Full Screen

1key = WebMock::Util.key(uri, method)2stub = WebMock::RequestStub.new(method, uri)3signature = WebMock::RequestSignature.new(method, uri)4error = WebMock::NetConnectNotAllowedError.new(request_signature)5error = WebMock::NetConnectNotAllowedError.new(request_signature)

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Webmock_ruby automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful