How to use with method of WebMock Package

Best Webmock_ruby code snippet using WebMock.with

request_pattern_spec.rb

Source:request_pattern_spec.rb Github

copy

Full Screen

...3 describe "describing itself" do4 it "should report string describing itself" do5 expect(WebMock::RequestPattern.new(:get, "www.example.com",6 body: "abc", headers: {'A' => 'a', 'B' => 'b'}).to_s).to eq(7 "GET http://www.example.com/ with body \"abc\" with headers {'A'=>'a', 'B'=>'b'}"8 )9 end10 it "should report string describing itself with block" do11 expect(WebMock::RequestPattern.new(:get, "www.example.com",12 body: "abc", headers: {'A' => 'a', 'B' => 'b'}).with {|req| true}.to_s).to eq(13 "GET http://www.example.com/ with body \"abc\" with headers {'A'=>'a', 'B'=>'b'} with given block"14 )15 end16 it "should report string describing itself with query params" do17 expect(WebMock::RequestPattern.new(:get, /.*example.*/, query: {'a' => ['b', 'c']}).to_s).to eq(18 "GET /.*example.*/ with query params {\"a\"=>[\"b\", \"c\"]}"19 )20 end21 it "should report string describing itself with query params as hash including matcher" do22 expect(WebMock::RequestPattern.new(:get, /.*example.*/,23 query: WebMock::Matchers::HashIncludingMatcher.new({'a' => ['b', 'c']})).to_s).to eq(24 "GET /.*example.*/ with query params hash_including({\"a\"=>[\"b\", \"c\"]})"25 )26 end27 it "should report string describing itself with body as hash including matcher" do28 expect(WebMock::RequestPattern.new(:get, /.*example.*/,29 body: WebMock::Matchers::HashIncludingMatcher.new({'a' => ['b', 'c']})).to_s).to eq(30 "GET /.*example.*/ with body hash_including({\"a\"=>[\"b\", \"c\"]})"31 )32 end33 end34 describe "with" do35 before(:each) do36 @request_pattern =WebMock::RequestPattern.new(:get, "www.example.com")37 end38 it "should have assigned body pattern" do39 @request_pattern.with(body: "abc")40 expect(@request_pattern.to_s).to eq(WebMock::RequestPattern.new(:get, "www.example.com", body: "abc").to_s)41 end42 it "should have assigned normalized headers pattern" do43 @request_pattern.with(headers: {'A' => 'a'})44 expect(@request_pattern.to_s).to eq(WebMock::RequestPattern.new(:get, "www.example.com", headers: {'A' => 'a'}).to_s)45 end46 it "should raise an error if options passed to `with` are invalid" do47 expect { @request_pattern.with(foo: "bar") }.to raise_error('Unknown key: "foo". Valid keys are: "body", "headers", "query", "basic_auth"')48 end49 it "should raise an error if neither options or block is provided" do50 expect { @request_pattern.with() }.to raise_error('#with method invoked with no arguments. Either options hash or block must be specified.')51 end52 end53 class WebMock::RequestPattern54 def match(request_signature)55 self.matches?(request_signature)56 end57 end58 describe "when matching" do59 it "should match if uri matches and method matches" do60 expect(WebMock::RequestPattern.new(:get, "www.example.com")).61 to match(WebMock::RequestSignature.new(:get, "www.example.com"))62 end63 it "should match if uri matches and method pattern is any" do64 expect(WebMock::RequestPattern.new(:any, "www.example.com")).65 to match(WebMock::RequestSignature.new(:get, "www.example.com"))66 end67 it "should not match if request has different method" do68 expect(WebMock::RequestPattern.new(:post, "www.example.com")).69 not_to match(WebMock::RequestSignature.new(:get, "www.example.com"))70 end71 it "should match if uri matches request uri" do72 expect(WebMock::RequestPattern.new(:get, "www.example.com")).73 to match(WebMock::RequestSignature.new(:get, "www.example.com"))74 end75 it "should match if request has unescaped uri" do76 expect(WebMock::RequestPattern.new(:get, "www.example.com/my%20path")).77 to match(WebMock::RequestSignature.new(:get, "www.example.com/my path"))78 end79 it "should match if request has escaped uri" do80 expect(WebMock::RequestPattern.new(:get, "www.example.com/my path")).81 to match(WebMock::RequestSignature.new(:get, "www.example.com/my%20path"))82 end83 it "should match if uri regexp pattern matches unescaped form of request uri" do84 expect(WebMock::RequestPattern.new(:get, /.*my path.*/)).85 to match(WebMock::RequestSignature.new(:get, "www.example.com/my%20path"))86 end87 it "should match if uri regexp pattern matches request uri" do88 expect(WebMock::RequestPattern.new(:get, /.*example.*/)).89 to match(WebMock::RequestSignature.new(:get, "www.example.com"))90 end91 it "should match if uri Addressable::Template pattern matches unescaped form of request uri" do92 expect(WebMock::RequestPattern.new(:get, Addressable::Template.new("www.example.com/{any_path}"))).93 to match(WebMock::RequestSignature.new(:get, "www.example.com/my%20path"))94 end95 it "should match if uri Addressable::Template pattern matches request uri" do96 expect(WebMock::RequestPattern.new(:get, Addressable::Template.new("www.example.com"))).97 to match(WebMock::RequestSignature.new(:get, "www.example.com"))98 end99 it "should match for uris with same parameters as pattern" do100 expect(WebMock::RequestPattern.new(:get, "www.example.com?a=1&b=2")).101 to match(WebMock::RequestSignature.new(:get, "www.example.com?a=1&b=2"))102 end103 it "should not match for uris with different parameters" do104 expect(WebMock::RequestPattern.new(:get, "www.example.com?a=1&b=2")).105 not_to match(WebMock::RequestSignature.new(:get, "www.example.com?a=2&b=1"))106 end107 it "should match for uri parameters in different order" do108 expect(WebMock::RequestPattern.new(:get, "www.example.com?b=2&a=1")).109 to match(WebMock::RequestSignature.new(:get, "www.example.com?a=1&b=2"))110 end111 describe "when parameters are escaped" do112 it "should match if uri pattern has escaped parameters and request has unescaped parameters" do113 expect(WebMock::RequestPattern.new(:get, "www.example.com/?a=a%20b")).114 to match(WebMock::RequestSignature.new(:get, "www.example.com/?a=a b"))115 end116 it "should match if uri pattern has unescaped parameters and request has escaped parameters" do117 expect(WebMock::RequestPattern.new(:get, "www.example.com/?a=a b")).118 to match(WebMock::RequestSignature.new(:get, "www.example.com/?a=a%20b"))119 end120 it "should match if uri regexp pattern matches uri with unescaped parameters and request has escaped parameters" do121 expect(WebMock::RequestPattern.new(:get, /.*a=a b.*/)).122 to match(WebMock::RequestSignature.new(:get, "www.example.com/?a=a%20b"))123 end124 it "should match if uri regexp pattern matches uri with escaped parameters and request has unescaped parameters" do125 expect(WebMock::RequestPattern.new(:get, /.*a=a%20b.*/)).126 to match(WebMock::RequestSignature.new(:get, "www.example.com/?a=a b"))127 end128 it "should match if uri Addressable::Template pattern matches uri without parameter value and request has escaped parameters" do129 expect(WebMock::RequestPattern.new(:get, Addressable::Template.new("www.example.com/{?a}"))).130 to match(WebMock::RequestSignature.new(:get, "www.example.com/?a=a%20b"))131 end132 it "should match if uri Addressable::Template pattern matches uri without parameter value and request has unescaped parameters" do133 expect(WebMock::RequestPattern.new(:get, Addressable::Template.new("www.example.com/{?a}"))).134 to match(WebMock::RequestSignature.new(:get, "www.example.com/?a=a b"))135 end136 it "should match if uri Addressable::Template pattern matches uri with unescaped parameter value and request has unescaped parameters" do137 expect(WebMock::RequestPattern.new(:get, Addressable::Template.new("www.example.com/?a=a b"))).138 to match(WebMock::RequestSignature.new(:get, "www.example.com/?a=a b"))139 end140 it "should match if uri Addressable::Template pattern matches uri with escaped parameter value and request has escaped parameters" do141 expect(WebMock::RequestPattern.new(:get, Addressable::Template.new("www.example.com/?a=a%20b"))).142 to match(WebMock::RequestSignature.new(:get, "www.example.com/?a=a%20b"))143 end144 end145 describe "when matching requests on query params" do146 describe "when uri is described as regexp" do147 it "should match request query params" do148 expect(WebMock::RequestPattern.new(:get, /.*example.*/, query: {"a" => ["b", "c"]})).149 to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c"))150 end151 it "should match request query params if params don't match" do152 expect(WebMock::RequestPattern.new(:get, /.*example.*/, query: {"x" => ["b", "c"]})).153 not_to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c"))154 end155 it "should match when query params are declared as HashIncluding matcher matching params" do156 expect(WebMock::RequestPattern.new(:get, /.*example.*/,157 query: WebMock::Matchers::HashIncludingMatcher.new({"a" => ["b", "c"]}))).158 to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))159 end160 it "should not match when query params are declared as HashIncluding matcher not matching params" do161 expect(WebMock::RequestPattern.new(:get, /.*example.*/,162 query: WebMock::Matchers::HashIncludingMatcher.new({"x" => ["b", "c"]}))).163 not_to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))164 end165 it "should match when query params are declared as RSpec HashIncluding matcher matching params" do166 expect(WebMock::RequestPattern.new(:get, /.*example.*/,167 query: RSpec::Mocks::ArgumentMatchers::HashIncludingMatcher.new({"a" => ["b", "c"]}))).168 to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))169 end170 it "should not match when query params are declared as RSpec HashIncluding matcher not matching params" do171 expect(WebMock::RequestPattern.new(:get, /.*example.*/,172 query: RSpec::Mocks::ArgumentMatchers::HashIncludingMatcher.new({"a" => ["b", "d"]}))).173 not_to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))174 end175 end176 describe "when uri is described as Addressable::Template" do177 it "should raise error if query params are specified" do178 expect(WebMock::RequestPattern.new(:get, Addressable::Template.new("www.example.com"), query: {"a" => ["b", "c"]})).179 to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c"))180 end181 it "should match request query params if params don't match" do182 expect(WebMock::RequestPattern.new(:get, Addressable::Template.new("www.example.com"), query: {"x" => ["b", "c"]})).183 not_to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c"))184 end185 it "should match when query params are declared as HashIncluding matcher matching params" do186 expect(WebMock::RequestPattern.new(:get, Addressable::Template.new("www.example.com"),187 query: WebMock::Matchers::HashIncludingMatcher.new({"a" => ["b", "c"]}))).188 to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))189 end190 it "should not match when query params are declared as HashIncluding matcher not matching params" do191 expect(WebMock::RequestPattern.new(:get, Addressable::Template.new("www.example.com"),192 query: WebMock::Matchers::HashIncludingMatcher.new({"x" => ["b", "c"]}))).193 not_to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))194 end195 it "should match when query params are declared as RSpec HashIncluding matcher matching params" do196 expect(WebMock::RequestPattern.new(:get, Addressable::Template.new("www.example.com"),197 query: RSpec::Mocks::ArgumentMatchers::HashIncludingMatcher.new({"a" => ["b", "c"]}))).198 to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))199 end200 it "should not match when query params are declared as RSpec HashIncluding matcher not matching params" do201 expect(WebMock::RequestPattern.new(:get, Addressable::Template.new("www.example.com"),202 query: RSpec::Mocks::ArgumentMatchers::HashIncludingMatcher.new({"a" => ["b", "d"]}))).203 not_to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))204 end205 end206 describe "when uri is described as string" do207 it "should match when query params are the same as declared in hash" do208 expect(WebMock::RequestPattern.new(:get, "www.example.com", query: {"a" => ["b", "c"]})).209 to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c"))210 end211 it "should not match when query params are different than the declared in hash" do212 expect(WebMock::RequestPattern.new(:get, "www.example.com", query: {"a" => ["b", "c"]})).213 not_to match(WebMock::RequestSignature.new(:get, "www.example.com?x[]=b&a[]=c"))214 end215 it "should match when query params are the same as declared as string" do216 expect(WebMock::RequestPattern.new(:get, "www.example.com", query: "a[]=b&a[]=c")).217 to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c"))218 end219 it "should match when query params are the same as declared both in query option or url" do220 expect(WebMock::RequestPattern.new(:get, "www.example.com/?x=3", query: "a[]=b&a[]=c")).221 to match(WebMock::RequestSignature.new(:get, "www.example.com/?x=3&a[]=b&a[]=c"))222 end223 it "should match when query params are declared as HashIncluding matcher matching params" do224 expect(WebMock::RequestPattern.new(:get, "www.example.com",225 query: WebMock::Matchers::HashIncludingMatcher.new({"a" => ["b", "c"]}))).226 to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))227 end228 it "should not match when query params are declared as HashIncluding matcher not matching params" do229 expect(WebMock::RequestPattern.new(:get, "www.example.com",230 query: WebMock::Matchers::HashIncludingMatcher.new({"x" => ["b", "c"]}))).231 not_to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))232 end233 it "should match when query params are declared as RSpec HashIncluding matcher matching params" do234 expect(WebMock::RequestPattern.new(:get, "www.example.com",235 query: RSpec::Mocks::ArgumentMatchers::HashIncludingMatcher.new({"a" => ["b", "c"]}))).236 to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))237 end238 it "should not match when query params are declared as RSpec HashIncluding matcher not matching params" do239 expect(WebMock::RequestPattern.new(:get, "www.example.com",240 query: RSpec::Mocks::ArgumentMatchers::HashIncludingMatcher.new({"a" => ["b", "d"]}))).241 not_to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))242 end243 context "when using query values notation as flat array" do244 before :all do245 WebMock::Config.instance.query_values_notation = :flat_array246 end247 it "should not match when repeated query params are not the same as declared as string" do248 expect(WebMock::RequestPattern.new(:get, "www.example.com", query: "a=b&a=c")).249 to match(WebMock::RequestSignature.new(:get, "www.example.com?a=b&a=c"))250 end251 after :all do252 WebMock::Config.instance.query_values_notation = nil253 end254 end255 end256 end257 describe "when matching requests with body" do258 it "should match if request body and body pattern are the same" do259 expect(WebMock::RequestPattern.new(:get, "www.example.com", body: "abc")).260 to match(WebMock::RequestSignature.new(:get, "www.example.com", body: "abc"))261 end262 it "should match if request body matches regexp" do263 expect(WebMock::RequestPattern.new(:get, "www.example.com", body: /^abc$/)).264 to match(WebMock::RequestSignature.new(:get, "www.example.com", body: "abc"))265 end266 it "should not match if body pattern is different than request body" do267 expect(WebMock::RequestPattern.new(:get, "www.example.com", body: "def")).268 not_to match(WebMock::RequestSignature.new(:get, "www.example.com", body: "abc"))269 end270 it "should not match if request body doesn't match regexp pattern" do271 expect(WebMock::RequestPattern.new(:get, "www.example.com", body: /^abc$/)).272 not_to match(WebMock::RequestSignature.new(:get, "www.example.com", body: "xabc"))273 end274 it "should match if pattern doesn't have specified body" do275 expect(WebMock::RequestPattern.new(:get, "www.example.com")).276 to match(WebMock::RequestSignature.new(:get, "www.example.com", body: "abc"))277 end278 it "should not match if pattern has body specified as nil but request body is not empty" do279 expect(WebMock::RequestPattern.new(:get, "www.example.com", body: nil)).280 not_to match(WebMock::RequestSignature.new(:get, "www.example.com", body: "abc"))281 end282 it "should not match if pattern has empty body but request body is not empty" do283 expect(WebMock::RequestPattern.new(:get, "www.example.com", body: "")).284 not_to match(WebMock::RequestSignature.new(:get, "www.example.com", body: "abc"))285 end286 it "should not match if pattern has body specified but request has no body" do287 expect(WebMock::RequestPattern.new(:get, "www.example.com", body: "abc")).288 not_to match(WebMock::RequestSignature.new(:get, "www.example.com"))289 end290 describe "when body in pattern is declared as a hash" do291 let(:body_hash) { {:a => '1', :b => 'five', 'c' => {'d' => ['e', 'f']}} }292 describe "for request with url encoded body" do293 it "should match when hash matches body" do294 expect(WebMock::RequestPattern.new(:post, 'www.example.com', body: body_hash)).295 to match(WebMock::RequestSignature.new(:post, "www.example.com", body: 'a=1&c[d][]=e&c[d][]=f&b=five'))296 end297 it "should match when hash matches body in different order of params" do298 expect(WebMock::RequestPattern.new(:post, 'www.example.com', body: body_hash)).299 to match(WebMock::RequestSignature.new(:post, "www.example.com", body: 'a=1&c[d][]=e&b=five&c[d][]=f'))300 end301 it "should not match when hash doesn't match url encoded body" do302 expect(WebMock::RequestPattern.new(:post, 'www.example.com', body: body_hash)).303 not_to match(WebMock::RequestSignature.new(:post, "www.example.com", body: 'c[d][]=f&a=1&c[d][]=e'))304 end305 it "should not match when body is not url encoded" do306 expect(WebMock::RequestPattern.new(:post, 'www.example.com', body: body_hash)).307 not_to match(WebMock::RequestSignature.new(:post, "www.example.com", body: 'foo bar'))308 end309 it "should match when hash contains regex values" do310 expect(WebMock::RequestPattern.new(:post, "www.example.com", body: {a: /^\w{5}$/, b: {c: /^\d{3}$/}})).311 to match(WebMock::RequestSignature.new(:post, "www.example.com", body: 'a=abcde&b[c]=123'))312 end313 it "should not match when hash does not contains regex values" do314 expect(WebMock::RequestPattern.new(:post, "www.example.com", body: {a: /^\d+$/, b: {c: /^\d{3}$/}})).315 not_to match(WebMock::RequestSignature.new(:post, "www.example.com", body: 'a=abcde&b[c]=123'))316 end317 context 'body is an hash with an array of hashes' do318 let(:body_hash) { {a: [{'b' => '1'}, {'b' => '2'}]} }319 it "should match when hash matches body" do320 expect(WebMock::RequestPattern.new(:post, 'www.example.com', body: body_hash)).321 to match(WebMock::RequestSignature.new(:post, "www.example.com", body: 'a[][b]=1&a[][b]=2'))322 end323 end324 context 'body is an hash with an array of hashes with multiple keys' do325 let(:body_hash) { {a: [{'b' => '1', 'a' => '2'}, {'b' => '3'}]} }326 it "should match when hash matches body" do327 expect(WebMock::RequestPattern.new(:post, 'www.example.com', body: body_hash)).328 to match(WebMock::RequestSignature.new(:post, "www.example.com", body: 'a[][b]=1&a[][a]=2&a[][b]=3'))329 end330 end331 end332 describe "for request with json body and content type is set to json" do333 it "should match when hash matches body" do334 expect(WebMock::RequestPattern.new(:post, 'www.example.com', body: body_hash)).335 to match(WebMock::RequestSignature.new(:post, "www.example.com", headers: {content_type: 'application/json'},336 body: "{\"a\":\"1\",\"c\":{\"d\":[\"e\",\"f\"]},\"b\":\"five\"}"))337 end338 it "should match if hash matches body in different form" do339 expect(WebMock::RequestPattern.new(:post, 'www.example.com', body: body_hash)).340 to match(WebMock::RequestSignature.new(:post, "www.example.com", headers: {content_type: 'application/json'},341 body: "{\"a\":\"1\",\"b\":\"five\",\"c\":{\"d\":[\"e\",\"f\"]}}"))342 end343 it "should not match when body is not json" do344 expect(WebMock::RequestPattern.new(:post, 'www.example.com', body: body_hash)).345 not_to match(WebMock::RequestSignature.new(:post, "www.example.com",346 headers: {content_type: 'application/json'}, body: "foo bar"))347 end348 it "should not match if request body is different" do349 expect(WebMock::RequestPattern.new(:post, 'www.example.com', body: {a: 1, b: 2})).350 not_to match(WebMock::RequestSignature.new(:post, "www.example.com",351 headers: {content_type: 'application/json'}, body: "{\"a\":1,\"c\":null}"))352 end353 it "should not match if request body is has less params than pattern" do354 expect(WebMock::RequestPattern.new(:post, 'www.example.com', body: {a: 1, b: 2})).355 not_to match(WebMock::RequestSignature.new(:post, "www.example.com",356 headers: {content_type: 'application/json'}, body: "{\"a\":1}"))357 end358 it "should not match if request body is has more params than pattern" do359 expect(WebMock::RequestPattern.new(:post, 'www.example.com', body: {a: 1})).360 not_to match(WebMock::RequestSignature.new(:post, "www.example.com",361 headers: {content_type: 'application/json'}, body: "{\"a\":1,\"c\":1}"))362 end363 end364 describe "for request with xml body and content type is set to xml" do365 let(:body_hash) { {"opt" => {:a => '1', :b => 'five', 'c' => {'d' => ['e', 'f']}}} }366 it "should match when hash matches body" do367 expect(WebMock::RequestPattern.new(:post, 'www.example.com', body: body_hash)).368 to match(WebMock::RequestSignature.new(:post, "www.example.com", headers: {content_type: 'application/xml'},369 body: "<opt a=\"1\" b=\"five\">\n <c>\n <d>e</d>\n <d>f</d>\n </c>\n</opt>\n"))370 end371 it "should match if hash matches body in different form" do372 expect(WebMock::RequestPattern.new(:post, 'www.example.com', body: body_hash)).373 to match(WebMock::RequestSignature.new(:post, "www.example.com", headers: {content_type: 'application/xml'},374 body: "<opt b=\"five\" a=\"1\">\n <c>\n <d>e</d>\n <d>f</d>\n </c>\n</opt>\n"))375 end376 it "should not match when body is not xml" do377 expect(WebMock::RequestPattern.new(:post, 'www.example.com', body: body_hash)).378 not_to match(WebMock::RequestSignature.new(:post, "www.example.com",379 headers: {content_type: 'application/xml'}, body: "foo bar"))380 end381 it "matches when the content type include a charset" do382 expect(WebMock::RequestPattern.new(:post, 'www.example.com', body: body_hash)).383 to match(WebMock::RequestSignature.new(:post, "www.example.com", headers: {content_type: 'application/xml;charset=UTF-8'},384 body: "<opt a=\"1\" b=\"five\">\n <c>\n <d>e</d>\n <d>f</d>\n </c>\n</opt>\n"))385 end386 end387 end388 describe "when body in a pattern is declared as a partial hash matcher" do389 let(:signature) { WebMock::RequestSignature.new(:post, "www.example.com", body: 'a=1&c[d][]=e&c[d][]=f&b=five') }390 it "should match when query params are declared as HashIncluding matcher matching params" do391 expect(WebMock::RequestPattern.new(:post, "www.example.com",392 body: WebMock::Matchers::HashIncludingMatcher.new({:a => '1', 'c' => {'d' => ['e', 'f']}}))).393 to match(signature)394 end395 it "should not match when query params are declared as HashIncluding matcher not matching params" do396 expect(WebMock::RequestPattern.new(:post, "www.example.com",397 body: WebMock::Matchers::HashIncludingMatcher.new({:x => '1', 'c' => {'d' => ['e', 'f']}}))).398 not_to match(signature)399 end400 it "should match when query params are declared as RSpec HashIncluding matcher matching params" do401 expect(WebMock::RequestPattern.new(:post, "www.example.com",402 body: RSpec::Mocks::ArgumentMatchers::HashIncludingMatcher.new({:a => '1', 'c' => {'d' => ['e', 'f']}}))).403 to match(signature)404 end405 it "should not match when query params are declared as RSpec HashIncluding matcher not matching params" do406 expect(WebMock::RequestPattern.new(:post, "www.example.com",407 body: RSpec::Mocks::ArgumentMatchers::HashIncludingMatcher.new({:x => '1', 'c' => {'d' => ['e', 'f']}}))).408 not_to match(signature)409 end410 end411 end412 it "should match if pattern and request have the same headers" do413 expect(WebMock::RequestPattern.new(:get, "www.example.com", headers: {'Content-Type' => 'image/jpeg'})).414 to match(WebMock::RequestSignature.new(:get, "www.example.com", headers: {'Content-Type' => 'image/jpeg'}))415 end416 it "should match if pattern headers values are regexps matching request header values" do417 expect(WebMock::RequestPattern.new(:get, "www.example.com", headers: {'Content-Type' => %r{^image/jpeg$}})).418 to match(WebMock::RequestSignature.new(:get, "www.example.com", headers: {'Content-Type' => 'image/jpeg'}))419 end420 it "should not match if pattern has different value of header than request" do421 expect(WebMock::RequestPattern.new(:get, "www.example.com", headers: {'Content-Type' => 'image/png'})).422 not_to match(WebMock::RequestSignature.new(:get, "www.example.com", headers: {'Content-Type' => 'image/jpeg'}))423 end424 it "should not match if pattern header value regexp doesn't match request header value" do425 expect(WebMock::RequestPattern.new(:get, "www.example.com", headers: {'Content-Type' => %r{^image\/jpeg$}})).426 not_to match(WebMock::RequestSignature.new(:get, "www.example.com", headers: {'Content-Type' => 'image/jpegx'}))427 end428 it "should match if request has more headers than request pattern" do429 expect(WebMock::RequestPattern.new(:get, "www.example.com", headers: {'Content-Type' => 'image/jpeg'})).430 to match(WebMock::RequestSignature.new(:get, "www.example.com", headers: {'Content-Type' => 'image/jpeg', 'Content-Length' => '8888'}))431 end432 it "should not match if request has less headers than the request pattern" do433 expect(WebMock::RequestPattern.new(:get, "www.example.com", headers: {'Content-Type' => 'image/jpeg', 'Content-Length' => '8888'})).434 not_to match(WebMock::RequestSignature.new(:get, "www.example.com", headers: {'Content-Type' => 'image/jpeg'}))435 end436 it "should match even is header keys are declared in different form" do437 expect(WebMock::RequestPattern.new(:get, "www.example.com", headers: {'ContentLength' => '8888', 'Content-type' => 'image/png'})).438 to match(WebMock::RequestSignature.new(:get, "www.example.com", headers: {:ContentLength => 8888, 'content_type' => 'image/png'}))439 end440 it "should match is pattern doesn't have specified headers" do441 expect(WebMock::RequestPattern.new(:get, "www.example.com")).442 to match(WebMock::RequestSignature.new(:get, "www.example.com", headers: {'A' => 'a'}))443 end444 it "should not match if pattern has nil headers but request has headers" do445 expect(WebMock::RequestPattern.new(:get, "www.example.com", headers: nil)).446 not_to match(WebMock::RequestSignature.new(:get, "www.example.com", headers: {'A' => 'a'}))447 end448 it "should not match if pattern has empty headers but request has headers" do449 expect(WebMock::RequestPattern.new(:get, "www.example.com", headers: {})).450 not_to match(WebMock::RequestSignature.new(:get, "www.example.com", headers: {'A' => 'a'}))451 end452 it "should not match if pattern has specified headers but request has nil headers" do453 expect(WebMock::RequestPattern.new(:get, "www.example.com", headers: {'A'=>'a'})).454 not_to match(WebMock::RequestSignature.new(:get, "www.example.com"))455 end456 it "should not match if pattern has specified headers but request has empty headers" do457 expect(WebMock::RequestPattern.new(:get, "www.example.com", headers: {'A'=>'a'})).458 not_to match(WebMock::RequestSignature.new(:get, "www.example.com", headers: {}))459 end460 it "should match if block given in pattern evaluates request to true" do461 expect(WebMock::RequestPattern.new(:get, "www.example.com").with { |request| true }).462 to match(WebMock::RequestSignature.new(:get, "www.example.com"))463 end464 it "should not match if block given in pattrn evaluates request to false" do465 expect(WebMock::RequestPattern.new(:get, "www.example.com").with { |request| false }).466 not_to match(WebMock::RequestSignature.new(:get, "www.example.com"))467 end468 it "should yield block with request signature" do469 signature = WebMock::RequestSignature.new(:get, "www.example.com")470 expect(WebMock::RequestPattern.new(:get, "www.example.com").with { |request| request == signature }).471 to match(signature)472 end473 end474end...

Full Screen

Full Screen

http_spec.rb

Source:http_spec.rb Github

copy

Full Screen

...15 let(:callback_object) { CallerObject.new }16 let(:url) { 'https://1.2.3.4/' }17 18 after(:each) { WebMock.reset! }19 shared_examples_for "a request with callbacks" do |net_request|20 it 'runs the before_request callback before submission' do21 callback_object.should_receive(:before_request).with(an_instance_of(net_request)).and_raise(RuntimeError)22 expect {23 subject24 }.to raise_error(RuntimeError)25 WebMock.should_not have_requested(:any, url)26 end27 28 it 'runs the on_success callback after submission' do29 callback_object.should_receive(:on_success).with(an_instance_of(VaultedBilling::HTTP::Response))30 subject31 end32 it 'runs the on_complete callback after submission' do33 callback_object.should_receive(:on_complete).with(an_instance_of(VaultedBilling::HTTP::Response))34 subject35 end36 37 it 'runs the on_error callback after submission' do38 WebMock.stub_request(:any, url).to_raise(Timeout::Error.new('Test Exception'))39 callback_object.should_receive(:on_error).with(an_instance_of(VaultedBilling::HTTP::Response), an_instance_of(Timeout::Error))40 subject41 end42 end43 44 shared_examples_for "a request with timeout errors" do45 context 'with Timeout errors' do46 before(:each) do47 WebMock.stub_request(:any, url).to_raise(Timeout::Error.new('Test Exception'))48 end49 after(:each) { WebMock.reset! }50 it { should be_kind_of VaultedBilling::HTTP::Response}51 it { should_not be_success }52 it 'returns the error information' do53 subject.message.tap do |message|54 message.should match 'Timeout::Error'55 message.should match 'Test Exception'56 end57 end58 end59 end60 61 shared_examples_for 'a request with a custom user agent' do62 it 'sets the library name' do63 subject64 WebMock.should have_requested(:any, url).with(:headers => {'User-Agent' => /^vaulted_billing\b/})65 end66 67 it 'sets the library version' do68 subject69 WebMock.should have_requested(:any, url).with(:headers => {'User-Agent' => /\b#{Regexp.escape(VaultedBilling::Version)}\b/})70 end71 72 it 'sets the Ruby version' do73 subject74 WebMock.should have_requested(:any, url).with(:headers => {'User-Agent' => /\b#{Regexp.escape(RUBY_VERSION)}\b/})75 end76 77 it 'sets the Ruby platform' do78 subject79 WebMock.should have_requested(:any, url).with(:headers => {'User-Agent' => /\b#{Regexp.escape(RUBY_PLATFORM)}\b/})80 end81 end82 context '#post' do83 subject { http.post('fubar') }84 before(:each) { WebMock.stub_request(:any, url) }85 it 'posts the given data' do86 subject87 WebMock.should have_requested(:post, url).with(:body => 'fubar')88 end89 it_should_behave_like 'a request with a custom user agent'90 it_should_behave_like "a request with callbacks", Net::HTTP::Post91 it_should_behave_like "a request with timeout errors"92 end93 94 context '#get' do95 subject { http.get }96 before(:each) { WebMock.stub_request(:any, url) }97 it 'gets the given url' do98 subject99 WebMock.should have_requested(:get, url)100 end101 it_should_behave_like 'a request with a custom user agent'102 it_should_behave_like "a request with callbacks", Net::HTTP::Get103 it_should_behave_like "a request with timeout errors"104 end105 106 context '#put' do107 subject { http.put('bar') }108 before(:each) { WebMock.stub_request(:any, url) }109 it 'posts the given data' do110 subject111 WebMock.should have_requested(:put, url).with(:body => 'bar')112 end113 it_should_behave_like 'a request with a custom user agent'114 it_should_behave_like "a request with callbacks", Net::HTTP::Put115 it_should_behave_like "a request with timeout errors"116 end117 118 it 'fails over to subsequent URIs with HTTP ERRORs raised' do119 WebMock.stub_request(:any, /.*/).to_timeout120 121 VaultedBilling::HTTP.new(callback_object, ['https://example1.com', 'https://example2.com']).tap do |http|122 http.get123 WebMock.should have_requested(:get, 'https://example1.com/')124 WebMock.should have_requested(:get, 'https://example2.com/')125 end126 end127 128 it 'returns connection error response when all URIs return HTTP ERRORs' do129 WebMock.stub_request(:any, /.*/).to_timeout130 131 VaultedBilling::HTTP.new(callback_object, ['https://example1.com', 'https://example2.com']).tap do |http|132 http.get.should be_connection_error...

Full Screen

Full Screen

webmock.rb

Source:webmock.rb Github

copy

Full Screen

...8 module WebMock9 extend self10 attr_accessor :global_hook_disabled11 alias global_hook_disabled? global_hook_disabled12 def with_global_hook_disabled13 self.global_hook_disabled = true14 begin15 yield16 ensure17 self.global_hook_disabled = false18 end19 end20 # @private21 module Helpers22 def vcr_request_for(webmock_request)23 VCR::Request.new \24 webmock_request.method,25 webmock_request.uri.to_s,26 webmock_request.body,27 request_headers_for(webmock_request)28 end29 # @private30 def vcr_response_for(webmock_response)31 VCR::Response.new \32 VCR::ResponseStatus.new(*webmock_response.status),33 webmock_response.headers,34 webmock_response.body,35 nil36 end37 if defined?(::Excon)38 # @private39 def request_headers_for(webmock_request)40 return nil unless webmock_request.headers41 # WebMock hooks deeply into a Excon at a place where it manually adds a "Host"42 # header, but this isn't a header we actually care to store...43 webmock_request.headers.dup.tap do |headers|44 headers.delete("Host")45 end46 end47 else48 # @private49 def request_headers_for(webmock_request)50 webmock_request.headers51 end52 end53 def typed_request_for(webmock_request, remove = false)54 if webmock_request.instance_variables.find { |v| v.to_sym == :@__typed_vcr_request }55 meth = remove ? :remove_instance_variable : :instance_variable_get56 return webmock_request.send(meth, :@__typed_vcr_request)57 end58 warn <<-EOS.gsub(/^\s+\|/, '')59 |WARNING: There appears to be a bug in WebMock's after_request hook60 | and VCR is attempting to work around it. Some VCR features61 | may not work properly.62 EOS63 Request::Typed.new(vcr_request_for(webmock_request), :unknown)64 end65 end66 class RequestHandler < ::VCR::RequestHandler67 include Helpers68 attr_reader :request69 def initialize(request)70 @request = request71 end72 private73 def externally_stubbed?74 # prevent infinite recursion...75 VCR::LibraryHooks::WebMock.with_global_hook_disabled do76 ::WebMock.registered_request?(request)77 end78 end79 def set_typed_request_for_after_hook(*args)80 super81 request.instance_variable_set(:@__typed_vcr_request, @after_hook_typed_request)82 end83 def vcr_request84 @vcr_request ||= vcr_request_for(request)85 end86 def on_externally_stubbed_request87 # nil allows WebMock to handle the request88 nil89 end90 def on_unhandled_request91 invoke_after_request_hook(nil)92 super93 end94 def on_stubbed_by_vcr_request95 {96 :body => stubbed_response.body,97 :status => [stubbed_response.status.code.to_i, stubbed_response.status.message],98 :headers => stubbed_response.headers99 }100 end101 end102 extend Helpers103 ::WebMock.globally_stub_request do |req|104 global_hook_disabled? ? nil : RequestHandler.new(req).handle105 end106 ::WebMock.after_request(:real_requests_only => true) do |request, response|107 unless VCR.library_hooks.disabled?(:webmock)108 http_interaction = VCR::HTTPInteraction.new \109 typed_request_for(request), vcr_response_for(response)110 VCR.record_http_interaction(http_interaction)111 end112 end113 ::WebMock.after_request do |request, response|114 unless VCR.library_hooks.disabled?(:webmock)115 VCR.configuration.invoke_hook \116 :after_http_request,117 typed_request_for(request, :remove),118 vcr_response_for(response)119 end120 end121 end122 end123end124# @private125module WebMock126 class << self127 # ensure HTTP requests are always allowed; VCR takes care of disallowing128 # them at the appropriate times in its hook129 def net_connect_allowed_with_vcr?(*args)130 VCR.turned_on? ? true : net_connect_allowed_without_vcr?(*args)131 end132 alias net_connect_allowed_without_vcr? net_connect_allowed?133 alias net_connect_allowed? net_connect_allowed_with_vcr?134 end unless respond_to?(:net_connect_allowed_with_vcr?)135end...

Full Screen

Full Screen

with

Using AI Code Generation

copy

Full Screen

1 to_return(:status => 200, :body => "success", :headers => {})2 expect(ApiClient.get('/').body).to eq('success')3 to_return(:status => 200, :body => "success", :headers => {})4 expect(ApiClient.get('/').body).to eq('success')

Full Screen

Full Screen

with

Using AI Code Generation

copy

Full Screen

1 to_return(:status => 200, :body => "Hello World")2 to_return(:status => 200, :body => "Hello World")3 to_return(:status => 200, :body => "Hello World")4 to_return(:status => 200, :body => "Hello World")5 to_return(:status => 200, :body => "Hello World")6 to_return(:status => 200, :body => "Hello World")7 to_return(:status => 200, :body => "Hello World")8 to_return(:status => 200, :body => "Hello World")9WebMock.stub_request(:get,

Full Screen

Full Screen

with

Using AI Code Generation

copy

Full Screen

1 to_return(:body => "Hello world!")2 expect(WebMock).to have_requested(:get, "www.example.com")3 to_return(:body => "Hello world!")4 expect(WebMock).to have_requested(:get, "www.example.com")5 to_return(:body => "Hello world!")6 expect(WebMock).to have_requested(:get, "www.example.com")7 to_return(:body => "Hello world!")8 expect(WebMock).to have_requested(:get, "www.example.com")9 to_return(:body => "Hello world!")10 expect(WebMock).to have_requested(:get, "www.example.com")11 to_return(:body => "Hello world!")12 expect(WebMock).to have_requested(:get, "www.example.com")

Full Screen

Full Screen

with

Using AI Code Generation

copy

Full Screen

1WebMock.stub_request(:get, "www.example.com").with(:headers => {'Accept'=>'*/*', 'User-Agent'=>'Ruby'}).to_return(:status => 200, :body => "Hello World", :headers => {})2WebMock.stub_request(:get, "www.example.com").with(:headers => {'Accept'=>'*/*', 'User-Agent'=>'Ruby'}).to_return(:status => 200, :body => "Hello World", :headers => {})3WebMock.stub_request(:get, "www.example.com").with(:headers => {'Accept'=>'*/*', 'User-Agent'=>'Ruby'}).to_return(:status => 200, :body => "Hello World", :headers => {})4WebMock.stub_request(:get, "www.example.com").with(:headers => {'Accept'=>'*/*', 'User-Agent'=>'Ruby'}).to_return(:status => 200, :body => "Hello World", :headers => {})5WebMock.stub_request(:get, "www.example.com").with(:headers => {'Accept'=>'*/*', 'User-Agent'=>'Ruby'}).to_return(:status => 200, :body => "Hello World", :headers => {})6WebMock.stub_request(:get, "www.example.com").with(:headers => {'Accept'=>'*/*', 'User-Agent'=>'Ruby'}).to_return(:status => 200, :body => "Hello World", :headers => {})7WebMock.stub_request(:get, "www.example.com").with(:headers => {'Accept'=>'*/*

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.

Most used method in

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful