1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708
|
shared_examples_for "stubbing requests" do |*adapter_info|
describe "when requests are stubbed" do
describe "based on uri" do
it "should return stubbed response even if request have escaped parameters" do
stub_request(:get, "www.example.com/hello+/?#{NOT_ESCAPED_PARAMS}").to_return(body: "abc")
expect(http_request(:get, "http://www.example.com/hello%2B/?#{ESCAPED_PARAMS}").body).to eq("abc")
end
it "should return stubbed response even if query params have integer values" do
stub_request(:get, "www.example.com").with(query: {"a" => 1}).to_return(body: "abc")
expect(http_request(:get, "http://www.example.com/?a=1").body).to eq("abc")
end
it "should return stubbed response even if request has non escaped params" do
stub_request(:get, "www.example.com/hello%2B/?#{ESCAPED_PARAMS}").to_return(body: "abc")
expect(http_request(:get, "http://www.example.com/hello+/?#{NOT_ESCAPED_PARAMS}").body).to eq("abc")
end
it "should return stubbed response for url with non utf query params" do
param = 'aäoöuü'.encode('iso-8859-1')
param = CGI.escape(param)
stub_request(:get, "www.example.com/?#{param}").to_return(body: "abc")
expect(http_request(:get, "http://www.example.com/?#{param}").body).to eq("abc")
end
it "should return stubbed response even if stub uri is declared as regexp and request params are escaped" do
stub_request(:get, /.*x=ab c.*/).to_return(body: "abc")
expect(http_request(:get, "http://www.example.com/hello/?#{ESCAPED_PARAMS}").body).to eq("abc")
end
it "should raise error specifying stubbing instructions with escaped characters in params if there is no matching stub" do
begin
http_request(:get, "http://www.example.com/hello+/?#{NOT_ESCAPED_PARAMS}")
rescue WebMock::NetConnectNotAllowedError => e
expect(e.message).to match(/Unregistered request: GET http:\/\/www\.example\.com\/hello\+\/\?x=ab%20c&z='Stop!'%20said%20Fred%20m/m)
expect(e.message).to match(/stub_request\(:get, "http:\/\/www\.example\.com\/hello\+\/\?x=ab%20c&z='Stop!'%20said%20Fred%20m"\)/m)
end
stub_request(:get, "http://www.example.com/hello+/?x=ab%20c&z='Stop!'%20said%20Fred%20m")
http_request(:get, "http://www.example.com/hello+/?#{NOT_ESCAPED_PARAMS}")
end
end
describe "based on query params" do
it "should return stubbed response when stub declares query params as a hash" do
stub_request(:get, "www.example.com").with(query: {"a" => ["b x", "c d"]}).to_return(body: "abc")
expect(http_request(:get, "http://www.example.com/?a[]=b+x&a[]=c%20d").body).to eq("abc")
end
it "should return stubbed response when stub declares query params as a hash" do
stub_request(:get, "www.example.com").with(query: "a[]=b&a[]=c").to_return(body: "abc")
expect(http_request(:get, "http://www.example.com/?a[]=b&a[]=c").body).to eq("abc")
end
it "should return stubbed response when stub declares query params both in uri and as a hash" do
stub_request(:get, "www.example.com/?x=3").with(query: {"a" => ["b", "c"]}).to_return(body: "abc")
expect(http_request(:get, "http://www.example.com/?x=3&a[]=b&a[]=c").body).to eq("abc")
end
it "should return stubbed response when stub expects only part of query params" do
stub_request(:get, "www.example.com").with(query: hash_including({"a" => ["b", "c"]})).to_return(body: "abc")
expect(http_request(:get, "http://www.example.com/?a[]=b&a[]=c&b=1").body).to eq("abc")
end
it 'should return stubbed response when stub expects exclude part of query params' do
stub_request(:get, 'www.example.com').with(query: hash_excluding(a: ['b', 'c'])).to_return(body: 'abc')
expect(http_request(:get, 'http://www.example.com/?a[]=c&a[]=d&b=1').body).to eq('abc')
end
it "should return stubbed response when stub expects an empty array" do
stub_request(:get, 'www.example.com').with(query: { a: [] }).to_return(body: 'abc')
expect(http_request(:get, 'http://www.example.com/?a[]').body).to eq('abc')
end
end
describe "based on method" do
it "should return stubbed response" do
stub_request(:get, "www.example.com")
expect(http_request(:get, "http://www.example.com/").status).to eq("200")
end
it "should match stubbed request when http request was made with method given as string" do
stub_request(:get, "www.example.com")
expect(http_request('get', "http://www.example.com/").status).to eq("200")
end
it "should raise error if stubbed request has different method" do
stub_request(:get, "www.example.com")
expect(http_request(:get, "http://www.example.com/").status).to eq("200")
expect {
http_request(:delete, "http://www.example.com/")
}.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: DELETE http://www.example.com/)
)
end
end
describe "based on body" do
it "should match requests if body is the same" do
stub_request(:post, "www.example.com").with(body: "abc")
expect(http_request(
:post, "http://www.example.com/",
body: "abc").status).to eq("200")
end
it "should match requests if body is not set in the stub" do
stub_request(:post, "www.example.com")
expect(http_request(
:post, "http://www.example.com/",
body: "abc").status).to eq("200")
end
it "should not match requests if body is different" do
stub_request(:post, "www.example.com").with(body: "abc")
expect {
http_request(:post, "http://www.example.com/", body: "def")
}.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: POST http://www.example.com/ with body 'def'))
end
describe "with regular expressions" do
it "should match requests if body matches regexp" do
stub_request(:post, "www.example.com").with(body: /\d+abc$/)
expect(http_request(
:post, "http://www.example.com/",
body: "123abc").status).to eq("200")
end
it "should not match requests if body doesn't match regexp" do
stub_request(:post, "www.example.com").with(body: /^abc/)
expect {
http_request(:post, "http://www.example.com/", body: "xabc")
}.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: POST http://www.example.com/ with body 'xabc'))
end
end
describe "when body is declared as a hash" do
before(:each) do
stub_request(:post, "www.example.com").
with(body: {:a => '1', :b => 'five x', 'c' => {'d' => ['e', 'f']} })
end
describe "for request with url encoded body" do
it "should match request if hash matches body" do
expect(http_request(
:post, "http://www.example.com/",
body: 'a=1&c[d][]=e&c[d][]=f&b=five+x').status).to eq("200")
end
it "should match request if hash matches body in different order of params" do
expect(http_request(
:post, "http://www.example.com/",
body: 'a=1&c[d][]=e&b=five+x&c[d][]=f').status).to eq("200")
end
it "should not match if hash doesn't match url encoded body" do
expect {
http_request(
:post, "http://www.example.com/",
body: 'c[d][]=f&a=1&c[d][]=e')
}.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: POST http://www.example.com/ with body 'c\[d\]\[\]=f&a=1&c\[d\]\[\]=e'))
end
describe "for request with form url encoded body and content type" do
it "should match if stubbed request body hash has string values matching string values in request body" do
WebMock.reset!
stub_request(:post, "www.example.com").with(body: {"foo" => '1'})
expect(http_request(
:post, "http://www.example.com/", headers: {'Content-Type' => 'application/x-www-form-urlencoded'},
body: "foo=1").status).to eq("200")
end
it "should match if stubbed request body hash has NON string values matching string values in request body" do
WebMock.reset!
stub_request(:post, "www.example.com").with(body: {"foo" => 1})
expect(http_request(
:post, "http://www.example.com/", headers: {'Content-Type' => 'application/x-www-form-urlencoded'},
body: "foo=1").status).to eq("200")
end
context "when using hash_including to match the request body" do
it "should match if stubbed request body is including hash" do
WebMock.reset!
stub_request(:post, "www.example.com").with(body: {"foo" => hash_including("bar" => '1')})
expect(http_request(
:post, "http://www.example.com/", headers: {'Content-Type' => 'application/x-www-form-urlencoded'},
body: "foo[bar]=1").status).to eq("200")
end
it "should not match if stubbed request body is excluding hash" do
WebMock.reset!
stub_request(:post, "www.example.com").with(body: {"foo" => hash_including("bar" => '1')})
expect {
http_request(:post, "http://www.example.com/",
headers: {'Content-Type' => 'application/x-www-form-urlencoded'},
body: "foo[xyz]=1")
}.to raise_error(WebMock::NetConnectNotAllowedError)
end
end
context "when using hash_excluding to match the request body" do
it "should match if stubbed request body is excluding hash" do
WebMock.reset!
stub_request(:post, "www.example.com").with(body: {"foo" => hash_excluding("bar" => '1')})
expect(http_request(
:post, "http://www.example.com/", headers: {'Content-Type' => 'application/x-www-form-urlencoded'},
body: "foo[xyz]=1").status).to eq("200")
end
it "should not match if stubbed request body is including hash" do
WebMock.reset!
stub_request(:post, "www.example.com").with(body: {"foo" => hash_excluding("bar" => '1')})
expect {
http_request(:post, "http://www.example.com/",
headers: {'Content-Type' => 'application/x-www-form-urlencoded'},
body: "foo[bar]=1")
}.to raise_error(WebMock::NetConnectNotAllowedError)
end
end
end
end
describe "for request with json body and content type is set to json" do
it "should match if hash matches body" do
expect(http_request(
:post, "http://www.example.com/", headers: {'Content-Type' => 'application/json'},
body: "{\"a\":\"1\",\"c\":{\"d\":[\"e\",\"f\"]},\"b\":\"five x\"}").status).to eq("200")
end
it "should match if hash matches body in different form" do
expect(http_request(
:post, "http://www.example.com/", headers: {'Content-Type' => 'application/json'},
body: "{\"a\":\"1\",\"b\":\"five x\",\"c\":{\"d\":[\"e\",\"f\"]}}").status).to eq("200")
end
it "should match if hash contains date string" do #Crack creates date object
WebMock.reset!
stub_request(:post, "www.example.com").
with(body: {"foo" => "2010-01-01"})
expect(http_request(
:post, "http://www.example.com/", headers: {'Content-Type' => 'application/json'},
body: "{\"foo\":\"2010-01-01\"}").status).to eq("200")
end
it "should match if any of the strings have spaces" do
WebMock.reset!
stub_request(:post, "www.example.com").with(body: {"foo" => "a b c"})
expect(http_request(
:post, "http://www.example.com/", headers: {'Content-Type' => 'application/json'},
body: "{\"foo\":\"a b c\"}").status).to eq("200")
end
it "should match if stubbed request body hash has NON string values matching NON string values in request body" do
WebMock.reset!
stub_request(:post, "www.example.com").with(body: {"foo" => 1})
expect(http_request(
:post, "http://www.example.com/", headers: {'Content-Type' => 'application/json'},
body: "{\"foo\":1}").status).to eq("200")
end
it "should not match if stubbed request body hash has string values matching NON string values in request body" do
WebMock.reset!
stub_request(:post, "www.example.com").with(body: {"foo" => '1'})
expect{http_request(
:post, "http://www.example.com/", headers: {'Content-Type' => 'application/json'},
body: "{\"foo\":1}") }.to raise_error(WebMock::NetConnectNotAllowedError)
end
it "should not match if stubbed request body hash has NON string values matching string values in request body" do
WebMock.reset!
stub_request(:post, "www.example.com").with(body: {"foo" => 1})
expect{http_request(
:post, "http://www.example.com/", headers: {'Content-Type' => 'application/json'},
body: "{\"foo\":\"1\"}") }.to raise_error(WebMock::NetConnectNotAllowedError)
end
end
describe "for request with xml body and content type is set to xml" do
before(:each) do
WebMock.reset!
stub_request(:post, "www.example.com").
with(body: { "opt" => {:a => '1', :b => 'five', 'c' => {'d' => ['e', 'f']} }})
end
it "should match if hash matches body" do
expect(http_request(
:post, "http://www.example.com/", headers: {'Content-Type' => 'application/xml'},
body: "<opt a=\"1\" b=\"five\">\n <c>\n <d>e</d>\n <d>f</d>\n </c>\n</opt>\n").status).to eq("200")
end
it "should match if hash matches body in different form" do
expect(http_request(
:post, "http://www.example.com/", headers: {'Content-Type' => 'application/xml'},
body: "<opt b=\"five\" a=\"1\">\n <c>\n <d>e</d>\n <d>f</d>\n </c>\n</opt>\n").status).to eq("200")
end
it "should match if hash contains date string" do #Crack creates date object
WebMock.reset!
stub_request(:post, "www.example.com").
with(body: {"opt" => {"foo" => "2010-01-01"}})
expect(http_request(
:post, "http://www.example.com/", headers: {'Content-Type' => 'application/xml'},
body: "<opt foo=\"2010-01-01\">\n</opt>\n").status).to eq("200")
end
end
end
describe "when body is declared as partial hash matcher" do
subject(:request) { http_request( :post, "http://www.example.com/",
body: 'a=1&c[d][]=e&c[d][]=f&b=five') }
subject(:wrong_request) { http_request(:post, "http://www.example.com/",
body: 'c[d][]=f&a=1&c[d][]=e').status }
describe "when using 'RSpec:Mocks::ArgumentMatchers#hash_including'" do
before(:each) do
stub_request(:post, "www.example.com").
with(body: hash_including(:a, c: {'d' => ['e', 'f']} ))
end
describe "for request with url encoded body" do
it "should match request if hash matches body" do
expect(request.status).to eq("200")
end
it "should not match if hash doesn't match url encoded body" do
expect { wrong_request }.to raise_error(WebMock::NetConnectNotAllowedError)
end
end
describe "for request with json body and content type is set to json" do
it "should match if hash matches body" do
expect(http_request(
:post, "http://www.example.com/", headers: {'Content-Type' => 'application/json'},
body: "{\"a\":\"1\",\"c\":{\"d\":[\"e\",\"f\"]},\"b\":\"five\"}").status).to eq("200")
end
end
end
describe "when using 'WebMock::API#hash_including'" do
before(:each) do
stub_request(:post, "www.example.com").
with(body: WebMock::API.hash_including(:a, c: {'d' => ['e', 'f']} ))
end
describe "for request with url encoded body" do
it "should match request if hash matches body" do
expect(request.status).to eq("200")
end
it "should not match if hash doesn't match url encoded body" do
expect { wrong_request }.to raise_error(WebMock::NetConnectNotAllowedError)
end
end
describe "for request with json body and content type is set to json" do
it "should match if hash matches body" do
expect(http_request(
:post, "http://www.example.com/", headers: {'Content-Type' => 'application/json'},
body: "{\"a\":\"1\",\"c\":{\"d\":[\"e\",\"f\"]},\"b\":\"five\"}").status).to eq("200")
end
end
end
end
end
describe "based on headers" do
it "should match requests if headers are the same" do
stub_request(:get, "www.example.com").with(headers: SAMPLE_REQUEST_HEADERS )
expect(http_request(
:get, "http://www.example.com/",
headers: SAMPLE_REQUEST_HEADERS).status).to eq("200")
end
it "should match requests if headers are the same and declared as array" do
stub_request(:get, "www.example.com").with(headers: {"a" => ["b"]} )
expect(http_request(
:get, "http://www.example.com/",
headers: {"a" => "b"}).status).to eq("200")
end
describe "when multiple headers with the same key are used" do
it "should match requests if headers are the same" do
stub_request(:get, "www.example.com").with(headers: {"a" => ["b", "c"]} )
expect(http_request(
:get, "http://www.example.com/",
headers: {"a" => ["b", "c"]}).status).to eq("200")
end
it "should match requests if headers are the same but in different order" do
stub_request(:get, "www.example.com").with(headers: {"a" => ["b", "c"]} )
expect(http_request(
:get, "http://www.example.com/",
headers: {"a" => ["c", "b"]}).status).to eq("200")
end
it "should not match requests if headers are different" do
stub_request(:get, "www.example.com").with(headers: {"a" => ["b", "c"]})
expect {
http_request(
:get, "http://www.example.com/",
headers: {"a" => ["b", "d"]})
}.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/ with headers))
end
end
it "should match requests if request headers are not stubbed" do
stub_request(:get, "www.example.com")
expect(http_request(
:get, "http://www.example.com/",
headers: SAMPLE_REQUEST_HEADERS).status).to eq("200")
end
it "should not match requests if headers are different" do
stub_request(:get, "www.example.com").with(headers: { "Accept" => "application/json" })
expect {
http_request(
:get, "http://www.example.com/",
headers: { "Accept" => "text/html" })
}.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/ with headers))
end
it "should not match if accept header is different" do
stub_request(:get, "www.example.com").
with(headers: { 'Accept' => 'application/json'})
expect {
http_request(
:get, "http://www.example.com/",
headers: { 'Accept' => 'application/xml'})
}.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/ with headers))
end
describe "declared as regular expressions" do
it "should match requests if header values match regular expression" do
stub_request(:get, "www.example.com").with(headers: { some_header: /^MyAppName$/ })
expect(http_request(
:get, "http://www.example.com/",
headers: { 'some-header' => 'MyAppName' }).status).to eq("200")
end
it "should not match requests if headers values do not match regular expression" do
stub_request(:get, "www.example.com").with(headers: { some_header: /^MyAppName$/ })
expect {
http_request(
:get, "http://www.example.com/",
headers: { 'some-header' => 'xMyAppName' })
}.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/ with headers))
end
end
end
describe "when stubbing request with userinfo in url", unless: (adapter_info.include?(:no_url_auth)) do
it "should match if credentials are the same" do
stub_request(:get, "user:pass@www.example.com")
expect(http_request(:get, "http://user:pass@www.example.com/").status).to eq("200")
end
it "should not match if credentials are different" do
stub_request(:get, "user:pass@www.example.com")
expect {
expect(http_request(:get, "http://user:pazz@www.example.com/").status).to eq("200")
}.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://user:pazz@www.example.com/))
end
it "should not match if credentials are stubbed but not provided in the request" do
stub_request(:get, "user:pass@www.example.com")
expect {
expect(http_request(:get, "http://www.example.com/").status).to eq("200")
}.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/))
end
it "should not match if credentials are not stubbed but exist in the request" do
stub_request(:get, "www.example.com")
expect {
expect(http_request(:get, "http://user:pazz@www.example.com/").status).to eq("200")
}.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://user:pazz@www.example.com/))
end
it "should not match if request has credentials provided in basic authentication herader" do
stub_request(:get, "user:pass@www.example.com")
expect {
expect(http_request(:get, "http://www.example.com/", basic_auth: ['user', 'pass']).status).to eq("200")
}.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/ with headers))
end
end
describe "when stubbing request with basic authentication header" do
it "should match if credentials are the same" do
stub_request(:get, "www.example.com").with(basic_auth: ['user', 'pass'])
expect(http_request(:get, "http://www.example.com/", basic_auth: ['user', 'pass']).status).to eq("200")
end
it "should match if credentials are the same and encode to more than one line" do
stub_request(:get, "www.example.com").with(basic_auth: ['user' * 5, 'pass' * 5])
expect(http_request(:get, "http://www.example.com/", basic_auth: ['user' * 5, 'pass' * 5]).status).to eq("200")
end
it "should match if credentials are the same and were provided directly as authentication headers in request" do
stub_request(:get, "www.example.com").with(basic_auth: ['user', 'pass'])
expect(http_request(:get, "http://www.example.com/", headers: {'Authorization'=>'Basic dXNlcjpwYXNz'}).status).to eq("200")
end
it "should match if credentials are the same and have been declared in the stub as encoded header" do
stub_request(:get, "www.example.com").with(headers: {'Authorization'=>'Basic dXNlcjpwYXNz'})
expect(http_request(:get, "http://www.example.com/", basic_auth: ['user', 'pass']).status).to eq("200")
end
it "should not match if credentials are different" do
stub_request(:get, "www.example.com").with(basic_auth: ['user', 'pass'])
expect {
expect(http_request(:get, "http://www.example.com/", basic_auth: ['user', 'pazz']).status).to eq("200")
}.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/ with headers))
end
it "should not match if credentials are stubbed but not provided in the request" do
stub_request(:get, "www.example.com").with(basic_auth: ['user', 'pass'])
expect {
expect(http_request(:get, "http://www.example.com/").status).to eq("200")
}.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/))
end
it "should match if credentials are not stubbed but exist in the request" do
stub_request(:get, "www.example.com")
expect(http_request(:get, "http://www.example.com/", basic_auth: ['user', 'pass']).status).to eq("200")
end
it "should not match if request has credentials provides in userinfo", unless: (adapter_info.include?(:no_url_auth)) do
stub_request(:get, "www.example.com").with(basic_auth: ['user', 'pass'])
expect {
expect(http_request(:get, "http://user:pass@www.example.com/").status).to eq("200")
}.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://user:pass@www.example.com/))
end
end
describe "when stubbing request with a global hook" do
after(:each) do
WebMock::StubRegistry.instance.global_stubs.clear
end
it 'returns the response returned by the hook' do
WebMock.globally_stub_request do |request|
{ body: "global stub body" }
end
expect(http_request(:get, "http://www.example.com/").body).to eq("global stub body")
end
it 'does not get cleared when a user calls WebMock.reset!' do
WebMock.globally_stub_request do |request|
{ body: "global stub body" }
end
WebMock.reset!
expect(http_request(:get, "http://www.example.com/").body).to eq("global stub body")
end
it "does not stub the request if the hook does not return anything" do
WebMock.globally_stub_request { |r| }
expect {
http_request(:get, "http://www.example.com/")
}.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/))
end
it "passes the request to the block" do
passed_request = nil
WebMock.globally_stub_request do |request|
passed_request = request
{ body: "global stub body" }
end
http_request(:get, "http://www.example.com:456/bar")
expect(passed_request.uri.to_s).to eq("http://www.example.com:456/bar")
end
it "should call the block only once per request" do
call_count = 0
WebMock.globally_stub_request do |request|
call_count += 1
{ body: "global stub body" }
end
http_request(:get, "http://www.example.com/")
expect(call_count).to eq(1)
end
it 'supports multiple global stubs; the first registered one that returns a non-nil value determines the stub' do
stub_invocation_order = []
WebMock.globally_stub_request do |request|
stub_invocation_order << :nil_stub
nil
end
WebMock.globally_stub_request do |request|
stub_invocation_order << :hash_stub
{ body: "global stub body" }
end
expect(http_request(:get, "http://www.example.com/").body).to eq("global stub body")
expect(stub_invocation_order).to eq([:nil_stub, :hash_stub])
end
[:before, :after].each do |before_or_after|
context "when there is also a non-global registered stub #{before_or_after} the global stub" do
def stub_non_globally
stub_request(:get, "www.example.com").to_return(body: 'non-global stub body')
end
define_method :register_stubs do |block|
stub_non_globally if before_or_after == :before
WebMock.globally_stub_request(&block)
stub_non_globally if before_or_after == :after
end
it 'uses the response from the global stub if the block returns a non-nil value' do
register_stubs(lambda { |req| { body: 'global stub body' } })
expect(http_request(:get, "http://www.example.com/").body).to eq("global stub body")
end
it 'uses the response from the non-global stub if the block returns a nil value' do
register_stubs(lambda { |req| nil })
expect(http_request(:get, "http://www.example.com/").body).to eq("non-global stub body")
end
end
end
context "when global stub should be invoked last" do
before do
WebMock.globally_stub_request(:after_local_stubs) do
{ body: "global stub body" }
end
end
it "uses global stub when non-global stub is not defined" do
expect(http_request(:get, "http://www.example.com/").body).to eq("global stub body")
end
it "uses non-global stub first" do
stub_request(:get, "www.example.com").to_return(body: 'non-global stub body')
expect(http_request(:get, "http://www.example.com/").body).to eq("non-global stub body")
end
end
end
describe "when stubbing request with a block evaluated on request" do
it "should match if block returns true" do
stub_request(:get, "www.example.com").with { |request| true }
expect(http_request(:get, "http://www.example.com/").status).to eq("200")
end
it "should not match if block returns false" do
stub_request(:get, "www.example.com").with { |request| false }
expect {
http_request(:get, "http://www.example.com/")
}.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/))
end
it "should pass the request to the block" do
stub_request(:post, "www.example.com").with { |request| request.body == "wadus" }
expect(http_request(
:post, "http://www.example.com/",
body: "wadus").status).to eq("200")
expect {
http_request(:post, "http://www.example.com/", body: "jander")
}.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: POST http://www.example.com/ with body 'jander'))
end
it "should call the block only once per request" do
call_count = 0
stub_request(:get, "www.example.com").with { |request| call_count += 1; true }
expect(http_request(:get, "http://www.example.com/").status).to eq("200")
expect(call_count).to eq(1)
end
end
end
describe "when request stub was removed" do
it "should raise an error on request" do
stub = stub_request(:get, "www.example.com")
http_request(:get, "http://www.example.com/")
remove_request_stub(stub)
expect {
http_request(:get, "http://www.example.com/")
}.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/))
end
end
describe "in Rspec around(:each) hook" do
# order goes
# around(:each)
# before(:each)
# after(:each)
# anything after example.run in around(:each)
around(:each) do |example|
example.run
expect {
http_request(:get, "http://www.example.com/")
}.to_not raise_error # WebMock::NetConnectNotAllowedError
end
it "should still allow me to make a mocked request" do
stub_request(:get, "www.example.com")
end
end
end
|