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 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930
|
shared_context "request expectations" do |*adapter_info|
describe "when request expectations are set" do
describe "when net connect is not allowed" do
before(:each) do
WebMock.disable_net_connect!
stub_request(:any, "http://www.example.com")
stub_request(:any, "https://www.example.com")
end
it "should satisfy expectation if request was executed with the same uri and method" do
expect {
http_request(:get, "http://www.example.com/")
expect(a_request(:get, "http://www.example.com")).to have_been_made.once
}.not_to raise_error
end
it "should satisfy expectation declared on WebMock.resuest" do
expect {
http_request(:get, "http://www.example.com/")
expect(WebMock.request(:get, "http://www.example.com")).to have_been_made.once
}.not_to raise_error
end
it "should satisfy expectation if request was not expected and not executed" do
expect {
expect(a_request(:get, "http://www.example.com")).not_to have_been_made
}.not_to raise_error
end
it "should fail if request was not expected but executed" do
expect {
http_request(:get, "http://www.example.com/")
expect(a_request(:get, "http://www.example.com")).not_to have_been_made
}.to fail_with(%r(The request GET http://www.example.com/ was not expected to execute but it executed 1 time))
end
it "should fail resulting with failure with a message and executed requests listed" do
expect {
http_request(:get, "http://www.example.com/")
expect(a_request(:get, "http://www.example.com")).not_to have_been_made
}.to fail_with(%r{The following requests were made:\n\nGET http://www.example.com/.+was made 1 time})
end
it "should fail if request was not executed" do
expect {
expect(a_request(:get, "http://www.example.com")).to have_been_made
}.to fail_with(%r(The request GET http://www.example.com/ was expected to execute 1 time but it executed 0 times))
end
it "should fail if request was executed to different uri" do
expect {
http_request(:get, "http://www.example.com/")
expect(a_request(:get, "http://www.example.org")).to have_been_made
}.to fail_with(%r(The request GET http://www.example.org/ was expected to execute 1 time but it executed 0 times))
end
it "should fail if request was executed with different method" do
expect {
http_request(:post, "http://www.example.com/", body: "abc")
expect(a_request(:get, "http://www.example.com")).to have_been_made
}.to fail_with(%r(The request GET http://www.example.com/ was expected to execute 1 time but it executed 0 times))
end
it "should satisfy expectation if request was executed with different form of uri" do
expect {
http_request(:get, "http://www.example.com/")
expect(a_request(:get, "www.example.com")).to have_been_made
}.not_to raise_error
end
it "should satisfy expectation if request was executed with different form of uri without port " do
expect {
http_request(:get, "http://www.example.com/")
expect(a_request(:get, "www.example.com:80")).to have_been_made
}.not_to raise_error
end
it "should satisfy expectation if request was executed with different form of uri with port" do
expect {
http_request(:get, "http://www.example.com/")
expect(a_request(:get, "www.example.com:80")).to have_been_made
}.not_to raise_error
end
it "should fail if request was executed to a different port" do
expect {
http_request(:get, "http://www.example.com:80/")
expect(a_request(:get, "www.example.com:90")).to have_been_made
}.to fail_with(%r(The request GET http://www.example.com:90/ was expected to execute 1 time but it executed 0 times))
end
it "should satisfy expectation if request was executed with different form of uri with https port" do
expect {
http_request(:get, "https://www.example.com/")
expect(a_request(:get, "https://www.example.com:443/")).to have_been_made
}.not_to raise_error
end
it "should satisfy expectations even if requests were executed in different order than expectations were declared" do
stub_request(:post, "http://www.example.com")
http_request(:post, "http://www.example.com/", body: "def")
http_request(:post, "http://www.example.com/", body: "abc")
expect(WebMock).to have_requested(:post, "www.example.com").with(body: "abc")
expect(WebMock).to have_requested(:post, "www.example.com").with(body: "def")
end
describe "when matching requests with escaped or unescaped uris" do
before(:each) do
WebMock.disable_net_connect!
stub_request(:any, "http://www.example.com/?#{NOT_ESCAPED_PARAMS}")
end
it "should satisfy expectation if request was executed with escaped params" do
expect {
http_request(:get, "http://www.example.com/?#{ESCAPED_PARAMS}")
expect(a_request(:get, "http://www.example.com/?#{NOT_ESCAPED_PARAMS}")).to have_been_made
}.not_to raise_error
end
it "should satisfy expectation if request was executed with non escaped params" do
expect {
http_request(:get, "http://www.example.com/?#{NOT_ESCAPED_PARAMS}")
expect(a_request(:get, "http://www.example.com/?#{ESCAPED_PARAMS}")).to have_been_made
}.not_to raise_error
end
it "should satisfy expectation if request was executed with escaped params and uri matching regexp was expected" do
expect {
http_request(:get, "http://www.example.com/?#{ESCAPED_PARAMS}")
expect(a_request(:get, /.*example.*/)).to have_been_made
}.not_to raise_error
end
end
describe "when matching requests with query params" do
before(:each) do
stub_request(:any, /.*example.*/)
end
it "should satisfy expectation if the request was executed with query params declared as a hash in a query option" do
expect {
http_request(:get, "http://www.example.com/?a[]=b&a[]=c")
expect(a_request(:get, "www.example.com").with(query: {"a" => ["b", "c"]})).to have_been_made
}.not_to raise_error
end
it "should satisfy expectation if the request was executed with query params declared as string in query option" do
expect {
http_request(:get, "http://www.example.com/?a[]=b&a[]=c")
expect(a_request(:get, "www.example.com").with(query: "a[]=b&a[]=c")).to have_been_made
}.not_to raise_error
end
it "should satisfy expectation if the request was executed with query params both in uri and in query option" do
expect {
http_request(:get, "http://www.example.com/?x=3&a[]=b&a[]=c")
expect(a_request(:get, "www.example.com/?x=3").with(query: {"a" => ["b", "c"]})).to have_been_made
}.not_to raise_error
end
it "should satisfy expectation if the request was executed with only part query params declared as a hash in a query option" do
expect {
http_request(:get, "http://www.example.com/?a[]=b&a[]=c&b=1")
expect(a_request(:get, "www.example.com").with(query: hash_including({"a" => ["b", "c"]}))).to have_been_made
}.not_to raise_error
end
it 'should satisfy expectation if the request was executed with excluding part of query params declared as a hash in a query option' do
expect {
http_request(:get, "http://www.example.com/?a[]=d&b[]=e&b=1")
expect(a_request(:get, "www.example.com").with(query: hash_excluding(a: ['b', 'c']))).to have_been_made
}.not_to raise_error
end
it 'should satisfy expectation if the request was executed with an empty array in the query params' do
expect {
http_request(:get, "http://www.example.com/?a[]")
expect(a_request(:get, "www.example.com").with(query: hash_including(a: []))).to have_been_made
}.not_to raise_error
end
end
context "when using flat array notation" do
before :all do
WebMock::Config.instance.query_values_notation = :flat_array
end
it "should satisfy expectation if request includes different repeated query params in flat array notation" do
expect {
stub_request(:get, "http://www.example.com/?a=1&a=2")
http_request(:get, "http://www.example.com/?a=1&a=2")
expect(a_request(:get, "http://www.example.com/?a=1&a=2")).to have_been_made
}.not_to raise_error
end
after :all do
WebMock::Config.instance.query_values_notation = nil
end
end
describe "at_most_times" do
it "fails if request was made more times than maximum" do
expect {
http_request(:get, "http://www.example.com/")
http_request(:get, "http://www.example.com/")
http_request(:get, "http://www.example.com/")
expect(a_request(:get, "http://www.example.com")).to have_been_made.at_most_times(2)
}.to fail_with(%r(The request GET http://www.example.com/ was expected to execute at most 2 times but it executed 3 times))
end
it "passes if request was made the maximum number of times" do
expect {
http_request(:get, "http://www.example.com/")
http_request(:get, "http://www.example.com/")
expect(a_request(:get, "http://www.example.com")).to have_been_made.at_most_times(2)
}.not_to raise_error
end
it "passes if request was made fewer than the maximum number of times" do
expect {
http_request(:get, "http://www.example.com/")
expect(a_request(:get, "http://www.example.com")).to have_been_made.at_most_times(2)
}.not_to raise_error
end
it "passes if request was not made at all" do
expect {
expect(a_request(:get, "http://www.example.com")).to have_been_made.at_most_times(2)
}.not_to raise_error
end
end
describe "at_least_times" do
it "fails if request was made fewer times than minimum" do
expect {
http_request(:get, "http://www.example.com/")
expect(a_request(:get, "http://www.example.com")).to have_been_made.at_least_times(2)
}.to fail_with(%r(The request GET http://www.example.com/ was expected to execute at least 2 times but it executed 1 time))
end
it "passes if request was made the minimum number of times" do
expect {
http_request(:get, "http://www.example.com/")
http_request(:get, "http://www.example.com/")
expect(a_request(:get, "http://www.example.com")).to have_been_made.at_least_times(2)
}.not_to raise_error
end
it "passes if request was made more than the minimum number of times" do
expect {
http_request(:get, "http://www.example.com/")
http_request(:get, "http://www.example.com/")
http_request(:get, "http://www.example.com/")
expect(a_request(:get, "http://www.example.com")).to have_been_made.at_least_times(2)
}.not_to raise_error
end
context "descriptive at_most_ matcher" do
context "at_most_once" do
it "succeeds if no request was executed" do
expect {
expect(a_request(:get, "http://www.example.com")).to have_been_made.at_most_once
}.not_to raise_error
end
it "satisfies expectation if request was executed with the same uri and method once" do
expect {
http_request(:get, "http://www.example.com/")
expect(a_request(:get, "http://www.example.com")).to have_been_made.at_most_once
}.not_to raise_error
end
it "fails if request was executed with the same uri and method twice" do
expect {
http_request(:get, "http://www.example.com/")
http_request(:get, "http://www.example.com/")
expect(a_request(:get, "http://www.example.com")).to have_been_made.at_most_once
}.to fail
end
end
context "at_most_twice" do
it "succeeds if no request was executed" do
expect {
expect(a_request(:get, "http://www.example.com")).to have_been_made.at_most_twice
}.not_to raise_error
end
it "succeeds if too few requests were executed" do
expect {
http_request(:get, "http://www.example.com/")
expect(a_request(:get, "http://www.example.com")).to have_been_made.at_most_twice
}.not_to raise_error
end
it "satisfies expectation if request was executed with the same uri and method twice" do
expect {
http_request(:get, "http://www.example.com/")
http_request(:get, "http://www.example.com/")
expect(a_request(:get, "http://www.example.com")).to have_been_made.at_most_twice
}.not_to raise_error
end
it "fails if request was executed with the same uri and method three times" do
expect {
http_request(:get, "http://www.example.com/")
http_request(:get, "http://www.example.com/")
http_request(:get, "http://www.example.com/")
expect(a_request(:get, "http://www.example.com")).to have_been_made.at_most_twice
}.to fail
end
end
end
context "descriptive at_least_ matcher" do
context "at_least_once" do
it "fails if no request was executed" do
expect {
expect(a_request(:get, "http://www.example.com")).to have_been_made.at_least_once
}.to fail
end
it "satisfies expectation if request was executed with the same uri and method once" do
expect {
http_request(:get, "http://www.example.com/")
expect(a_request(:get, "http://www.example.com")).to have_been_made.at_least_once
}.not_to raise_error
end
it "satisfies expectation if request was executed with the same uri and method twice" do
expect {
http_request(:get, "http://www.example.com/")
http_request(:get, "http://www.example.com/")
expect(a_request(:get, "http://www.example.com")).to have_been_made.at_least_once
}.not_to raise_error
end
end
context "at_least_twice" do
it "fails if no request was executed" do
expect {
expect(a_request(:get, "http://www.example.com")).to have_been_made.at_least_twice
}.to fail
end
it "fails if too few requests were executed" do
expect {
http_request(:get, "http://www.example.com/")
expect(a_request(:get, "http://www.example.com")).to have_been_made.at_least_twice
}.to fail
end
it "satisfies expectation if request was executed with the same uri and method twice" do
expect {
http_request(:get, "http://www.example.com/")
http_request(:get, "http://www.example.com/")
expect(a_request(:get, "http://www.example.com")).to have_been_made.at_least_twice
}.not_to raise_error
end
it "satisfies expectation if request was executed with the same uri and method three times" do
expect {
http_request(:get, "http://www.example.com/")
http_request(:get, "http://www.example.com/")
http_request(:get, "http://www.example.com/")
expect(a_request(:get, "http://www.example.com")).to have_been_made.at_least_twice
}.not_to raise_error
end
end
end
end
it "should fail if request was made more times than expected" do
expect {
http_request(:get, "http://www.example.com/")
http_request(:get, "http://www.example.com/")
expect(a_request(:get, "http://www.example.com")).to have_been_made
}.to fail_with(%r(The request GET http://www.example.com/ was expected to execute 1 time but it executed 2 times))
end
it "should fail if request was made less times than expected" do
expect {
http_request(:get, "http://www.example.com/")
expect(a_request(:get, "http://www.example.com")).to have_been_made.twice
}.to fail_with(%r(The request GET http://www.example.com/ was expected to execute 2 times but it executed 1 time))
end
it "should fail if request was made less times than expected when 3 times expected" do
expect {
http_request(:get, "http://www.example.com/")
expect(a_request(:get, "http://www.example.com")).to have_been_made.times(3)
}.to fail_with(%r(The request GET http://www.example.com/ was expected to execute 3 times but it executed 1 time))
end
it "should satisfy expectation if request was executed with the same body" do
expect {
http_request(:post, "http://www.example.com/", body: "abc")
expect(a_request(:post, "www.example.com").with(body: "abc")).to have_been_made
}.not_to raise_error
end
it "should fail if request was executed with different body" do
expect {
http_request(:post, "http://www.example.com/", body: "abc")
expect(a_request(:post, "www.example.com").
with(body: "def")).to have_been_made
}.to fail_with(%r(The request POST http://www.example.com/ with body "def" was expected to execute 1 time but it executed 0 times))
end
describe "when expected request body is declared as a regexp" do
it "should satisfy expectation if request was executed with body matching regexp" do
expect {
http_request(:post, "http://www.example.com/", body: "abc")
expect(a_request(:post, "www.example.com").with(body: /^abc$/)).to have_been_made
}.not_to raise_error
end
it "should fail if request was executed with body not matching regexp" do
expect {
http_request(:post, "http://www.example.com/", body: "abc")
expect(a_request(:post, "www.example.com").
with(body: /^xabc/)).to have_been_made
}.to fail_with(%r(The request POST http://www.example.com/ with body /\^xabc/ was expected to execute 1 time but it executed 0 times))
end
end
describe "when expected reqest body is declared as a hash" do
let(:body_hash) { {:a => '1', :b => 'five', 'c' => {'d' => ['e', 'f']}} }
let(:fail_message) {%r(The request POST http://www.example.com/ with body .+ was expected to execute 1 time but it executed 0 times)}
describe "when request is made with url encoded body matching hash" do
it "should satisfy expectation" do
expect {
http_request(:post, "http://www.example.com/", body: 'a=1&c[d][]=e&c[d][]=f&b=five')
expect(a_request(:post, "www.example.com").with(body: body_hash)).to have_been_made
}.not_to raise_error
end
it "should satisfy expectation even if url encoded params have different order" do
expect {
http_request(:post, "http://www.example.com/", body: 'a=1&c[d][]=e&b=five&c[d][]=f')
expect(a_request(:post, "www.example.com").with(body: body_hash)).to have_been_made
}.not_to raise_error
end
it "should fail if request is executed with url encoded body not matching hash" do
expect {
http_request(:post, "http://www.example.com/", body: 'c[d][]=f&a=1&c[d][]=e')
expect(a_request(:post, "www.example.com").with(body: body_hash)).to have_been_made
}.to fail_with(fail_message)
end
end
describe "when request is executed with json body matching hash and Content-Type is set to json" do
it "should satisfy expectation" do
expect {
http_request(:post, "http://www.example.com/", headers: {'Content-Type' => 'application/json'},
body: "{\"a\":\"1\",\"c\":{\"d\":[\"e\",\"f\"]},\"b\":\"five\"}")
expect(a_request(:post, "www.example.com").with(body: body_hash)).to have_been_made
}.not_to raise_error
end
it "should satisfy expectation even if json body is in different form" do
expect {
http_request(:post, "http://www.example.com/", headers: {'Content-Type' => 'application/json'},
body: "{\"a\":\"1\",\"b\":\"five\",\"c\":{\"d\":[\"e\",\"f\"]}}")
expect(a_request(:post, "www.example.com").with(body: body_hash)).to have_been_made
}.not_to raise_error
end
it "should satisfy expectation even if json body contains date string" do
expect {
http_request(:post, "http://www.example.com/", headers: {'Content-Type' => 'application/json'},
body: "{\"foo\":\"2010-01-01\"}")
expect(a_request(:post, "www.example.com").with(body: {"foo" => "2010-01-01"})).to have_been_made
}.not_to raise_error
end
end
describe "when request is executed with xml body matching hash and content type is set to xml" do
let(:body_hash) { { "opt" => {:a => "1", :b => 'five', 'c' => {'d' => ['e', 'f']}} }}
it "should satisfy expectation" 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")
expect(a_request(:post, "www.example.com").with(body: body_hash)).to have_been_made
}.not_to raise_error
end
it "should satisfy expectation even if xml body is 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")
expect(a_request(:post, "www.example.com").with(body: body_hash)).to have_been_made
}.not_to raise_error
end
it "should satisfy expectation even if xml body contains date string" do
expect {
http_request(:post, "http://www.example.com/", headers: {'Content-Type' => 'application/xml'},
body: "<opt foo=\"2010-01-01\">\n</opt>\n")
expect(a_request(:post, "www.example.com").with(body: {"opt" => {"foo" => "2010-01-01"}})).to have_been_made
}.not_to raise_error
end
end
end
describe "when expected reqest body is declared as a partial hash matcher" do
let(:body_hash) { hash_including({:a => '1', 'c' => {'d' => ['e', 'f']}}) }
let(:fail_message) {%r(The request POST http://www.example.com/ with body hash_including(.+) was expected to execute 1 time but it executed 0 times)}
describe "when request is made with url encoded body matching hash" do
it "should satisfy expectation" do
expect {
http_request(:post, "http://www.example.com/", body: 'a=1&c[d][]=e&c[d][]=f&b=five')
expect(a_request(:post, "www.example.com").with(body: body_hash)).to have_been_made
}.not_to raise_error
end
it "should fail if request is executed with url encoded body not matching hash" do
expect {
http_request(:post, "http://www.example.com/", body: 'c[d][]=f&a=1&c[d][]=e')
expect(a_request(:post, "www.example.com").with(body: body_hash)).to have_been_made
}.to fail_with(fail_message)
end
end
end
describe "when request with headers is expected" do
it "should satisfy expectation if request was executed with the same headers" do
expect {
http_request(:get, "http://www.example.com/", headers: SAMPLE_HEADERS)
expect(a_request(:get, "www.example.com").
with(headers: SAMPLE_HEADERS)).to have_been_made
}.not_to raise_error
end
it "should satisfy expectation if request was executed with the same headers but with header value declared as array" do
expect {
http_request(:get, "http://www.example.com/", headers: {"a" => "b"})
expect(a_request(:get, "www.example.com").
with(headers: {"a" => ["b"]})).to have_been_made
}.not_to raise_error
end
describe "when multiple headers with the same key are passed" do
it "should satisfy expectation" do
expect {
http_request(:get, "http://www.example.com/", headers: {"a" => ["b", "c"]})
expect(a_request(:get, "www.example.com").
with(headers: {"a" => ["b", "c"]})).to have_been_made
}.not_to raise_error
end
it "should satisfy expectation even if request was executed with the same headers but different order" do
expect {
http_request(:get, "http://www.example.com/", headers: {"a" => ["b", "c"]})
expect(a_request(:get, "www.example.com").
with(headers: {"a" => ["c", "b"]})).to have_been_made
}.not_to raise_error
end
it "should fail if request was executed with different headers" do
expect {
http_request(:get, "http://www.example.com/", headers: {"a" => ["b", "c"]})
expect(a_request(:get, "www.example.com").
with(headers: {"a" => ["b", "d"]})).to have_been_made
}.to fail_with(%r(The request GET http://www.example.com/ with headers \{'A'=>\['b', 'd'\]\} was expected to execute 1 time but it executed 0 times))
end
end
it "should fail if request was executed with different headers" do
expect {
http_request(:get, "http://www.example.com/", headers: SAMPLE_HEADERS)
expect(a_request(:get, "www.example.com").
with(headers: { 'Content-Length' => '9999'})).to have_been_made
}.to fail_with(%r(The request GET http://www.example.com/ with headers \{'Content-Length'=>'9999'\} was expected to execute 1 time but it executed 0 times))
end
it "should fail if request was executed with less headers" do
expect {
http_request(:get, "http://www.example.com/", headers: {'A' => 'a'})
expect(a_request(:get, "www.example.com").
with(headers: {'A' => 'a', 'B' => 'b'})).to have_been_made
}.to fail_with(%r(The request GET http://www.example.com/ with headers \{'A'=>'a', 'B'=>'b'\} was expected to execute 1 time but it executed 0 times))
end
it "should satisfy expectation if request was executed with more headers" do
expect {
http_request(:get, "http://www.example.com/",
headers: {'A' => 'a', 'B' => 'b'}
)
expect(a_request(:get, "www.example.com").
with(headers: {'A' => 'a'})).to have_been_made
}.not_to raise_error
end
it "should satisfy expectation if request was executed with body and headers but they were not specified in expectantion" do
expect {
http_request(:post, "http://www.example.com/",
body: "abc",
headers: SAMPLE_HEADERS
)
expect(a_request(:post, "www.example.com")).to have_been_made
}.not_to raise_error
end
it "should satisfy expectation if request was executed with headers matching regular expressions" do
expect {
http_request(:get, "http://www.example.com/", headers: { 'some-header' => 'MyAppName' })
expect(a_request(:get, "www.example.com").
with(headers: { some_header: /^MyAppName$/ })).to have_been_made
}.not_to raise_error
end
it "should fail if request was executed with headers not matching regular expression" do
expect {
http_request(:get, "http://www.example.com/", headers: { 'some-header' => 'xMyAppName' })
expect(a_request(:get, "www.example.com").
with(headers: { some_header: /^MyAppName$/ })).to have_been_made
}.to fail_with(%r(The request GET http://www.example.com/ with headers \{'Some-Header'=>/\^MyAppName\$/\} was expected to execute 1 time but it executed 0 times))
end
end
describe "when expectation contains a request matching block" do
it "should satisfy expectation if request was executed and block evaluated to true" do
expect {
http_request(:post, "http://www.example.com/", body: "wadus")
expect(a_request(:post, "www.example.com").with { |req| req.body == "wadus" }).to have_been_made
}.not_to raise_error
end
it "should fail if request was executed and block evaluated to false" do
expect {
http_request(:post, "http://www.example.com/", body: "abc")
expect(a_request(:post, "www.example.com").with { |req| req.body == "wadus" }).to have_been_made
}.to fail_with(%r(The request POST http://www.example.com/ with given block was expected to execute 1 time but it executed 0 times))
end
it "should fail if request was not expected but it executed and block matched request" do
expect {
http_request(:post, "http://www.example.com/", body: "wadus")
expect(a_request(:post, "www.example.com").with { |req| req.body == "wadus" }).not_to have_been_made
}.to fail_with(%r(The request POST http://www.example.com/ with given block was not expected to execute but it executed 1 time))
end
end
describe "with userinfo", unless: (adapter_info.include?(:no_url_auth)) do
before(:each) do
stub_request(:any, "http://user:pass@www.example.com")
stub_request(:any, "http://user:pazz@www.example.com")
end
it "should satisfy expectation if request was executed with expected credentials" do
expect {
http_request(:get, "http://user:pass@www.example.com/")
expect(a_request(:get, "http://user:pass@www.example.com")).to have_been_made.once
}.not_to raise_error
end
it "should fail if request was executed with different credentials than expected" do
expect {
http_request(:get, "http://user:pass@www.example.com/")
expect(a_request(:get, "http://user:pazz@www.example.com")).to have_been_made.once
}.to fail_with(%r(The request GET http://user:pazz@www.example.com/ was expected to execute 1 time but it executed 0 times))
end
it "should fail if request was executed without credentials and credentials were expected" do
expect {
http_request(:get, "http://www.example.com/")
expect(a_request(:get, "http://user:pass@www.example.com")).to have_been_made.once
}.to fail_with(%r(The request GET http://user:pass@www.example.com/ was expected to execute 1 time but it executed 0 times))
end
it "should fail if request was executed with credentials but expected without credentials" do
expect {
http_request(:get, "http://user:pass@www.example.com/")
expect(a_request(:get, "http://www.example.com")).to have_been_made.once
}.to fail_with(%r(The request GET http://www.example.com/ was expected to execute 1 time but it executed 0 times))
end
it "should fail if request was executed with basic auth header but expected with credentials in userinfo" do
expect {
http_request(:get, "http://www.example.com/", basic_auth: ['user', 'pass'])
expect(a_request(:get, "http://user:pass@www.example.com")).to have_been_made.once
}.to fail_with(%r(The request GET http://user:pass@www.example.com/ was expected to execute 1 time but it executed 0 times))
end
end
describe "with basic authentication header" do
before(:each) do
stub_request(:any, "http://www.example.com").with(basic_auth: ['user', 'pass'])
stub_request(:any, "http://www.example.com").with(basic_auth: ['user', 'pazz'])
end
it "should satisfy expectation if request was executed with expected credentials" do
expect {
http_request(:get, "http://www.example.com/", basic_auth: ['user', 'pass'])
expect(a_request(:get, "http://www.example.com").with(basic_auth: ['user', 'pass'])).to have_been_made.once
}.not_to raise_error
end
it "should satisfy expectation if request was executed with expected credentials passed directly as header" do
expect {
http_request(:get, "http://www.example.com/", headers: {'Authorization'=>'Basic dXNlcjpwYXNz'})
expect(a_request(:get, "http://www.example.com").with(basic_auth: ['user', 'pass'])).to have_been_made.once
}.not_to raise_error
end
it "should fail if request was executed with different credentials than expected" do
expect {
http_request(:get, "http://www.example.com/", basic_auth: ['user', 'pass'])
expect(a_request(:get, "http://www.example.com").with(basic_auth: ['user', 'pazz'])).to have_been_made.once
}.to fail_with(%r(The request GET http://www.example.com/ with headers {'Authorization'=>'Basic dXNlcjpwYXp6'} was expected to execute 1 time but it executed 0 times))
end
it "should fail if request was executed without credentials and credentials were expected" do
expect {
http_request(:get, "http://www.example.com/")
expect(a_request(:get, "http://www.example.com").with(basic_auth: ['user', 'pass'])).to have_been_made.once
}.to fail_with(%r(The request GET http://www.example.com/ with headers {'Authorization'=>'Basic dXNlcjpwYXNz'} was expected to execute 1 time but it executed 0 times))
end
it "should not fail if request was executed with credentials but expected despite credentials" do
expect {
http_request(:get, "http://www.example.com/", basic_auth: ['user', 'pass'])
expect(a_request(:get, "http://www.example.com")).to have_been_made.once
}.not_to raise_error
end
it "should fail if request was executed with basic auth header and credentials were provided in url", unless: (adapter_info.include?(:no_url_auth)) do
expect {
stub_request(:any, "http://user:pass@www.example.com")
http_request(:get, "http://user:pass@www.example.com/")
expect(a_request(:get, "http://www.example.com").with(basic_auth: ['user', 'pass'])).to have_been_made.once
}.to fail_with(%r(The request GET http://www.example.com/ with headers {'Authorization'=>'Basic dXNlcjpwYXNz'} was expected to execute 1 time but it executed 0 times))
end
end
describe "when expectations were set on WebMock object" do
it "should satisfy expectation if expected request was made" do
expect {
http_request(:get, "http://www.example.com/")
expect(WebMock).to have_requested(:get, "http://www.example.com").once
}.not_to raise_error
end
it "should satisfy expectation if request with body and headers was expected and request was made" do
expect {
http_request(:post, "http://www.example.com/", body: "abc", headers: {'A' => 'a'})
expect(WebMock).to have_requested(:post, "http://www.example.com").with(body: "abc", headers: {'A' => 'a'}).once
}.not_to raise_error
end
it "should fail if request expected not to be made was made" do
expect {
http_request(:get, "http://www.example.com/")
expect(WebMock).not_to have_requested(:get, "http://www.example.com")
}.to fail_with(%r(The request GET http://www.example.com/ was not expected to execute but it executed 1 time))
end
it "should satisfy expectation if request was executed and expectation had block which evaluated to true" do
expect {
http_request(:post, "http://www.example.com/", body: "wadus")
expect(WebMock).to have_requested(:post, "www.example.com").with { |req| req.body == "wadus" }
}.not_to raise_error
end
it "should fail if request was executed and expectation had block which evaluated to false" do
expect {
http_request(:post, "http://www.example.com/", body: "abc")
expect(WebMock).to have_requested(:post, "www.example.com").with { |req| req.body == "wadus" }
}.to fail_with(%r(The request POST http://www.example.com/ with given block was expected to execute 1 time but it executed 0 times))
end
it "should fail if request was expected not to be made but was made and block matched request" do
expect {
http_request(:post, "http://www.example.com/", body: "wadus")
expect(WebMock).not_to have_requested(:post, "www.example.com").with { |req| req.body == "wadus" }
}.to fail_with(%r(The request POST http://www.example.com/ with given block was not expected to execute but it executed 1 time))
end
end
describe "when expectation is declared using assert_requested" do
it "should satisfy expectation if requests was made" do
expect {
http_request(:get, "http://www.example.com/")
assert_requested(:get, "http://www.example.com", times: 1)
assert_requested(:get, "http://www.example.com")
}.not_to raise_error
end
it "should satisfy expectation if request was made with body and headers" do
expect {
http_request(:post, "http://www.example.com/", body: "abc", headers: {'A' => 'a'})
assert_requested(:post, "http://www.example.com", body: "abc", headers: {'A' => 'a'})
}.not_to raise_error
end
it "should fail if request expected not to be made was not wade" do
expect {
http_request(:get, "http://www.example.com/")
assert_not_requested(:get, "http://www.example.com")
}.to fail_with(%r(The request GET http://www.example.com/ was not expected to execute but it executed 1 time))
end
it "should fail if request expected not to be made was made and expectation block evaluated to true" do
expect {
http_request(:post, "http://www.example.com/", body: "wadus")
assert_not_requested(:post, "www.example.com") { |req| req.body == "wadus" }
}.to fail_with(%r(The request POST http://www.example.com/ with given block was not expected to execute but it executed 1 time))
end
it "should satisfy expectation if request was made and expectation block evaluated to true" do
expect {
http_request(:post, "http://www.example.com/", body: "wadus")
assert_requested(:post, "www.example.com") { |req| req.body == "wadus" }
}.not_to raise_error
end
it "should fail if request was made and expectation block evaluated to false" do
expect {
http_request(:post, "http://www.example.com/", body: "abc")
assert_requested(:post, "www.example.com") { |req| req.body == "wadus" }
}.to fail_with(%r(The request POST http://www.example.com/ with given block was expected to execute 1 time but it executed 0 times))
end
end
describe "when expectation is declared using assert_requested" do
it "should satisfy expectation if requests was made" do
stub_http = stub_http_request(:get, "http://www.example.com")
expect {
http_request(:get, "http://www.example.com/")
assert_requested(stub_http, times: 1)
assert_requested(stub_http)
}.not_to raise_error
end
it "should fail if request expected not to be made was not wade" do
stub_http = stub_http_request(:get, "http://www.example.com")
expect {
http_request(:get, "http://www.example.com/")
assert_not_requested(stub_http)
}.to fail_with(%r(The request GET http://www.example.com/ was not expected to execute but it executed 1 time))
end
end
end
describe "expectation is set on the request stub" do
it "should satisfy expectation if expected request was made" do
stub = stub_request(:get, "http://www.example.com/")
http_request(:get, "http://www.example.com/")
expect(stub).to have_been_requested.once
end
it "should satisfy expectations if subsequent requests were made" do
stub = stub_request(:get, "http://www.example.com/")
http_request(:get, "http://www.example.com/")
expect(stub).to have_been_requested.once
http_request(:get, "http://www.example.com/")
expect(stub).to have_been_requested.twice
end
it "should satisfy expectation if expected request with body and headers was made" do
stub = stub_request(:post, "http://www.example.com").with(body: "abc", headers: {'A' => 'a'})
http_request(:post, "http://www.example.com/", body: "abc", headers: {'A' => 'a'})
expect(stub).to have_been_requested.once
end
it "should fail if request not expected to be made was made" do
expect {
stub = stub_request(:get, "http://www.example.com")
http_request(:get, "http://www.example.com/")
expect(stub).not_to have_been_requested
}.to fail_with(%r(The request GET http://www.example.com/ was not expected to execute but it executed 1 time))
end
it "should fail request not expected to be made was made and expectation block evaluated to true" do
expect {
stub = stub_request(:post, "www.example.com").with { |req| req.body == "wadus" }
http_request(:post, "http://www.example.com/", body: "wadus")
expect(stub).not_to have_been_requested
}.to fail_with(%r(The request POST http://www.example.com/ with given block was not expected to execute but it executed 1 time))
end
it "should satisfy expectation if request was made and expectation block evaluated to true" do
stub = stub_request(:post, "www.example.com").with { |req| req.body == "wadus" }
http_request(:post, "http://www.example.com/", body: "wadus")
expect(stub).to have_been_requested
end
it "should satisfy expectation if request was made and expectation block evaluated to false" do
expect {
stub_request(:any, /.+/) #stub any request
stub = stub_request(:post, "www.example.com").with { |req| req.body == "wadus" }
http_request(:post, "http://www.example.com/", body: "abc")
expect(stub).to have_been_requested
}.to fail_with(%r(The request POST http://www.example.com/ with given block was expected to execute 1 time but it executed 0 times))
end
end
describe "when net connect is allowed", net_connect: true do
before(:each) do
WebMock.allow_net_connect!
end
it "should satisfy expectation if expected requests was made" do
expect {
http_request(:get, "http://www.example.com/")
expect(a_request(:get, "http://www.example.com")).to have_been_made
}.not_to raise_error
end
it "should fail request expected not to be made, was made" do
expect {
http_request(:get, "http://www.example.com/")
expect(a_request(:get, "http://www.example.com")).not_to have_been_made
}.to fail_with(%r(The request GET http://www.example.com/ was not expected to execute but it executed 1 time))
end
end
end
end
|