webmock 3.7.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (142) hide show
  1. checksums.yaml +7 -0
  2. data/.gemtest +0 -0
  3. data/.gitignore +34 -0
  4. data/.rspec-tm +2 -0
  5. data/.travis.yml +19 -0
  6. data/CHANGELOG.md +1698 -0
  7. data/Gemfile +9 -0
  8. data/LICENSE +20 -0
  9. data/README.md +1125 -0
  10. data/Rakefile +28 -0
  11. data/lib/webmock.rb +59 -0
  12. data/lib/webmock/api.rb +109 -0
  13. data/lib/webmock/assertion_failure.rb +11 -0
  14. data/lib/webmock/callback_registry.rb +35 -0
  15. data/lib/webmock/config.rb +18 -0
  16. data/lib/webmock/cucumber.rb +10 -0
  17. data/lib/webmock/deprecation.rb +9 -0
  18. data/lib/webmock/errors.rb +17 -0
  19. data/lib/webmock/http_lib_adapters/async_http_client_adapter.rb +214 -0
  20. data/lib/webmock/http_lib_adapters/curb_adapter.rb +347 -0
  21. data/lib/webmock/http_lib_adapters/em_http_request_adapter.rb +228 -0
  22. data/lib/webmock/http_lib_adapters/excon_adapter.rb +162 -0
  23. data/lib/webmock/http_lib_adapters/http_lib_adapter.rb +7 -0
  24. data/lib/webmock/http_lib_adapters/http_lib_adapter_registry.rb +19 -0
  25. data/lib/webmock/http_lib_adapters/http_rb/client.rb +14 -0
  26. data/lib/webmock/http_lib_adapters/http_rb/request.rb +16 -0
  27. data/lib/webmock/http_lib_adapters/http_rb/response.rb +43 -0
  28. data/lib/webmock/http_lib_adapters/http_rb/streamer.rb +29 -0
  29. data/lib/webmock/http_lib_adapters/http_rb/webmock.rb +68 -0
  30. data/lib/webmock/http_lib_adapters/http_rb_adapter.rb +37 -0
  31. data/lib/webmock/http_lib_adapters/httpclient_adapter.rb +242 -0
  32. data/lib/webmock/http_lib_adapters/manticore_adapter.rb +130 -0
  33. data/lib/webmock/http_lib_adapters/net_http.rb +361 -0
  34. data/lib/webmock/http_lib_adapters/net_http_response.rb +34 -0
  35. data/lib/webmock/http_lib_adapters/patron_adapter.rb +130 -0
  36. data/lib/webmock/http_lib_adapters/typhoeus_hydra_adapter.rb +174 -0
  37. data/lib/webmock/matchers/any_arg_matcher.rb +13 -0
  38. data/lib/webmock/matchers/hash_argument_matcher.rb +21 -0
  39. data/lib/webmock/matchers/hash_excluding_matcher.rb +15 -0
  40. data/lib/webmock/matchers/hash_including_matcher.rb +17 -0
  41. data/lib/webmock/minitest.rb +41 -0
  42. data/lib/webmock/rack_response.rb +69 -0
  43. data/lib/webmock/request_body_diff.rb +64 -0
  44. data/lib/webmock/request_execution_verifier.rb +77 -0
  45. data/lib/webmock/request_pattern.rb +370 -0
  46. data/lib/webmock/request_registry.rb +35 -0
  47. data/lib/webmock/request_signature.rb +54 -0
  48. data/lib/webmock/request_signature_snippet.rb +61 -0
  49. data/lib/webmock/request_stub.rb +100 -0
  50. data/lib/webmock/response.rb +153 -0
  51. data/lib/webmock/responses_sequence.rb +40 -0
  52. data/lib/webmock/rspec.rb +41 -0
  53. data/lib/webmock/rspec/matchers.rb +27 -0
  54. data/lib/webmock/rspec/matchers/request_pattern_matcher.rb +78 -0
  55. data/lib/webmock/rspec/matchers/webmock_matcher.rb +67 -0
  56. data/lib/webmock/stub_registry.rb +67 -0
  57. data/lib/webmock/stub_request_snippet.rb +38 -0
  58. data/lib/webmock/test_unit.rb +22 -0
  59. data/lib/webmock/util/hash_counter.rb +39 -0
  60. data/lib/webmock/util/hash_keys_stringifier.rb +25 -0
  61. data/lib/webmock/util/hash_validator.rb +17 -0
  62. data/lib/webmock/util/headers.rb +64 -0
  63. data/lib/webmock/util/json.rb +67 -0
  64. data/lib/webmock/util/query_mapper.rb +281 -0
  65. data/lib/webmock/util/uri.rb +110 -0
  66. data/lib/webmock/util/values_stringifier.rb +20 -0
  67. data/lib/webmock/util/version_checker.rb +111 -0
  68. data/lib/webmock/version.rb +3 -0
  69. data/lib/webmock/webmock.rb +161 -0
  70. data/minitest/test_helper.rb +34 -0
  71. data/minitest/test_webmock.rb +9 -0
  72. data/minitest/webmock_spec.rb +60 -0
  73. data/spec/acceptance/async_http_client/async_http_client_spec.rb +349 -0
  74. data/spec/acceptance/async_http_client/async_http_client_spec_helper.rb +73 -0
  75. data/spec/acceptance/curb/curb_spec.rb +492 -0
  76. data/spec/acceptance/curb/curb_spec_helper.rb +147 -0
  77. data/spec/acceptance/em_http_request/em_http_request_spec.rb +406 -0
  78. data/spec/acceptance/em_http_request/em_http_request_spec_helper.rb +77 -0
  79. data/spec/acceptance/excon/excon_spec.rb +77 -0
  80. data/spec/acceptance/excon/excon_spec_helper.rb +50 -0
  81. data/spec/acceptance/http_rb/http_rb_spec.rb +82 -0
  82. data/spec/acceptance/http_rb/http_rb_spec_helper.rb +54 -0
  83. data/spec/acceptance/httpclient/httpclient_spec.rb +217 -0
  84. data/spec/acceptance/httpclient/httpclient_spec_helper.rb +57 -0
  85. data/spec/acceptance/manticore/manticore_spec.rb +56 -0
  86. data/spec/acceptance/manticore/manticore_spec_helper.rb +35 -0
  87. data/spec/acceptance/net_http/net_http_shared.rb +153 -0
  88. data/spec/acceptance/net_http/net_http_spec.rb +331 -0
  89. data/spec/acceptance/net_http/net_http_spec_helper.rb +64 -0
  90. data/spec/acceptance/net_http/real_net_http_spec.rb +20 -0
  91. data/spec/acceptance/patron/patron_spec.rb +125 -0
  92. data/spec/acceptance/patron/patron_spec_helper.rb +54 -0
  93. data/spec/acceptance/shared/allowing_and_disabling_net_connect.rb +313 -0
  94. data/spec/acceptance/shared/callbacks.rb +148 -0
  95. data/spec/acceptance/shared/complex_cross_concern_behaviors.rb +36 -0
  96. data/spec/acceptance/shared/enabling_and_disabling_webmock.rb +95 -0
  97. data/spec/acceptance/shared/precedence_of_stubs.rb +15 -0
  98. data/spec/acceptance/shared/request_expectations.rb +930 -0
  99. data/spec/acceptance/shared/returning_declared_responses.rb +409 -0
  100. data/spec/acceptance/shared/stubbing_requests.rb +643 -0
  101. data/spec/acceptance/typhoeus/typhoeus_hydra_spec.rb +135 -0
  102. data/spec/acceptance/typhoeus/typhoeus_hydra_spec_helper.rb +60 -0
  103. data/spec/acceptance/webmock_shared.rb +41 -0
  104. data/spec/fixtures/test.txt +1 -0
  105. data/spec/quality_spec.rb +84 -0
  106. data/spec/spec_helper.rb +48 -0
  107. data/spec/support/example_curl_output.txt +22 -0
  108. data/spec/support/failures.rb +9 -0
  109. data/spec/support/my_rack_app.rb +53 -0
  110. data/spec/support/network_connection.rb +19 -0
  111. data/spec/support/webmock_server.rb +70 -0
  112. data/spec/unit/api_spec.rb +175 -0
  113. data/spec/unit/errors_spec.rb +129 -0
  114. data/spec/unit/http_lib_adapters/http_lib_adapter_registry_spec.rb +17 -0
  115. data/spec/unit/http_lib_adapters/http_lib_adapter_spec.rb +12 -0
  116. data/spec/unit/matchers/hash_excluding_matcher_spec.rb +61 -0
  117. data/spec/unit/matchers/hash_including_matcher_spec.rb +87 -0
  118. data/spec/unit/rack_response_spec.rb +112 -0
  119. data/spec/unit/request_body_diff_spec.rb +90 -0
  120. data/spec/unit/request_execution_verifier_spec.rb +208 -0
  121. data/spec/unit/request_pattern_spec.rb +601 -0
  122. data/spec/unit/request_registry_spec.rb +95 -0
  123. data/spec/unit/request_signature_snippet_spec.rb +89 -0
  124. data/spec/unit/request_signature_spec.rb +155 -0
  125. data/spec/unit/request_stub_spec.rb +199 -0
  126. data/spec/unit/response_spec.rb +282 -0
  127. data/spec/unit/stub_registry_spec.rb +103 -0
  128. data/spec/unit/stub_request_snippet_spec.rb +115 -0
  129. data/spec/unit/util/hash_counter_spec.rb +39 -0
  130. data/spec/unit/util/hash_keys_stringifier_spec.rb +27 -0
  131. data/spec/unit/util/headers_spec.rb +28 -0
  132. data/spec/unit/util/json_spec.rb +33 -0
  133. data/spec/unit/util/query_mapper_spec.rb +157 -0
  134. data/spec/unit/util/uri_spec.rb +361 -0
  135. data/spec/unit/util/version_checker_spec.rb +65 -0
  136. data/spec/unit/webmock_spec.rb +19 -0
  137. data/test/http_request.rb +24 -0
  138. data/test/shared_test.rb +108 -0
  139. data/test/test_helper.rb +23 -0
  140. data/test/test_webmock.rb +6 -0
  141. data/webmock.gemspec +45 -0
  142. metadata +496 -0
@@ -0,0 +1,148 @@
1
+ shared_context "callbacks" do |*adapter_info|
2
+ describe "when after_request callback is declared" do
3
+ before(:each) do
4
+ @called = nil
5
+ WebMock.reset_callbacks
6
+ stub_request(:get, "http://www.example.com")
7
+ end
8
+
9
+ it "should not invoke callback unless request is made" do
10
+ WebMock.after_request {
11
+ @called = true
12
+ }
13
+ expect(@called).to eq(nil)
14
+ end
15
+
16
+ it "should invoke a callback after request is made" do
17
+ WebMock.after_request {
18
+ @called = true
19
+ }
20
+ http_request(:get, "http://www.example.com/")
21
+ expect(@called).to eq(true)
22
+ end
23
+
24
+ it "should not invoke a callback if this http library should be ignored" do
25
+ WebMock.after_request(except: [http_library()]) {
26
+ @called = true
27
+ }
28
+ http_request(:get, "http://www.example.com/")
29
+ expect(@called).to eq(nil)
30
+ end
31
+
32
+ it "should invoke a callback even if other http libraries should be ignored" do
33
+ WebMock.after_request(except: [:other_lib]) {
34
+ @called = true
35
+ }
36
+ http_request(:get, "http://www.example.com/")
37
+ expect(@called).to eq(true)
38
+ end
39
+
40
+ it "should pass request signature to the callback" do
41
+ WebMock.after_request(except: [:other_lib]) do |request_signature, _|
42
+ @request_signature = request_signature
43
+ end
44
+ http_request(:get, "http://www.example.com/")
45
+ expect(@request_signature.uri.to_s).to eq("http://www.example.com:80/")
46
+ end
47
+
48
+ after(:each) do
49
+ WebMock::StubRegistry.instance.global_stubs.clear
50
+ end
51
+
52
+ it 'passes the same request signature instance to the callback that was passed to the global stub callback' do
53
+ global_stub_request_sig = after_request_request_sig = nil
54
+ WebMock.globally_stub_request do |request_sig|
55
+ global_stub_request_sig = request_sig
56
+ nil
57
+ end
58
+
59
+ WebMock.after_request do |request_sig, _|
60
+ after_request_request_sig = request_sig
61
+ end
62
+
63
+ http_request(:get, "http://www.example.com/")
64
+ expect(global_stub_request_sig).to be(after_request_request_sig)
65
+ end
66
+
67
+ context "passing response to callback" do
68
+ context "when request is stubbed" do
69
+ before(:each) do
70
+ stub_request(:get, "http://www.example.com").
71
+ to_return(
72
+ status: [200, "hello"],
73
+ headers: {'Content-Length' => '666', 'Hello' => 'World'},
74
+ body: "foo bar"
75
+ )
76
+ WebMock.after_request(except: [:other_lib]) do |_, response|
77
+ @response = response
78
+ end
79
+ http_request(:get, "http://www.example.com/")
80
+ end
81
+
82
+ it "should pass response to callback with the status and message" do
83
+ expect(@response.status).to eq([200, "hello"])
84
+ end
85
+
86
+ it "should pass response to callback with headers" do
87
+ expect(@response.headers).to eq({
88
+ 'Content-Length' => '666',
89
+ 'Hello' => 'World'
90
+ })
91
+ end
92
+
93
+ it "should pass response to callback with body" do
94
+ expect(@response.body).to eq("foo bar")
95
+ end
96
+ end
97
+
98
+ describe "when request is not stubbed", net_connect: true do
99
+ before(:each) do
100
+ WebMock.reset!
101
+ WebMock.allow_net_connect!
102
+ WebMock.after_request(except: [:other_lib]) do |_, response|
103
+ @response = response
104
+ end
105
+ http_request(:get, "http://httpstat.us/201", headers: { "Accept" => "*" })
106
+ end
107
+
108
+ it "should pass real response to callback with status and message" do
109
+ expect(@response.status[0]).to eq(201)
110
+ expect(@response.status[1]).to eq("Created") unless adapter_info.include?(:no_status_message)
111
+ end
112
+
113
+ it "should pass real response to callback with headers" do
114
+ expect(@response.headers["X-Powered-By"]).to eq( "ASP.NET")
115
+ expect(@response.headers["Content-Length"]).to eq("11") unless adapter_info.include?(:no_content_length_header)
116
+ end
117
+
118
+ it "should pass response to callback with body" do
119
+ expect(@response.body.size).to eq(11)
120
+ end
121
+ end
122
+ end
123
+
124
+ it "should invoke multiple callbacks in order of their declarations" do
125
+ WebMock.after_request { @called = 1 }
126
+ WebMock.after_request { @called += 1 }
127
+ http_request(:get, "http://www.example.com/")
128
+ expect(@called).to eq(2)
129
+ end
130
+
131
+ it "should invoke callbacks only for real requests if requested", net_connect: true do
132
+ WebMock.after_request(real_requests_only: true) { @called = true }
133
+ http_request(:get, "http://www.example.com/")
134
+ expect(@called).to eq(nil)
135
+ WebMock.allow_net_connect!
136
+ http_request(:get, "http://www.example.net/")
137
+ expect(@called).to eq(true)
138
+ end
139
+
140
+ it "should not invoke any callbacks after callbacks were reset" do
141
+ WebMock.after_request { @called = 1 }
142
+ WebMock.reset_callbacks
143
+ stub_request(:get, "http://www.example.com/")
144
+ http_request(:get, "http://www.example.com/")
145
+ expect(@called).to eq(nil)
146
+ end
147
+ end
148
+ end
@@ -0,0 +1,36 @@
1
+ shared_context "complex cross-concern behaviors" do |*adapter_info|
2
+ it 'allows a response with multiple values for the same header to be recorded and played back exactly as-is' do
3
+ WebMock.allow_net_connect!
4
+
5
+ recorded_response = nil
6
+ WebMock.after_request { |_,r| recorded_response = r }
7
+ real_response = http_request(:get, webmock_server_url)
8
+
9
+ stub_request(:get, webmock_server_url).to_return(
10
+ status: recorded_response.status,
11
+ body: recorded_response.body,
12
+ headers: recorded_response.headers
13
+ )
14
+
15
+ played_back_response = http_request(:get, webmock_server_url)
16
+
17
+ expect(played_back_response.headers.keys).to include('Set-Cookie')
18
+ expect(played_back_response).to eq(real_response)
19
+ end
20
+
21
+ let(:no_content_url) { 'http://httpstat.us/204' }
22
+ [nil, ''].each do |stub_val|
23
+ it "returns the same value (nil or "") for a request stubbed as #{stub_val.inspect} that a real empty response has", net_connect: true do
24
+ unless http_library == :curb
25
+ WebMock.allow_net_connect!
26
+
27
+ real_response = http_request(:get, no_content_url)
28
+ stub_request(:get, no_content_url).to_return(status: 204, body: stub_val)
29
+ stubbed_response = http_request(:get, no_content_url)
30
+
31
+ expect(stubbed_response.body).to eq(real_response.body)
32
+ end
33
+ end
34
+ end
35
+ end
36
+
@@ -0,0 +1,95 @@
1
+ shared_context "enabled and disabled webmock" do |*adapter_info|
2
+ describe "when webmock is disabled" do
3
+ before(:each) do
4
+ WebMock.disable!
5
+ end
6
+ after(:each) do
7
+ WebMock.enable!
8
+ end
9
+ include_context "disabled WebMock"
10
+ end
11
+
12
+ describe "when webmock is enabled again" do
13
+ before(:each) do
14
+ WebMock.disable!
15
+ WebMock.enable!
16
+ end
17
+ include_context "enabled WebMock"
18
+ end
19
+
20
+ describe "when webmock is disabled except this lib" do
21
+ before(:each) do
22
+ WebMock.disable!(except: [http_library])
23
+ end
24
+ after(:each) do
25
+ WebMock.enable!
26
+ end
27
+ include_context "enabled WebMock"
28
+ end
29
+
30
+ describe "when webmock is enabled except this lib" do
31
+ before(:each) do
32
+ WebMock.disable!
33
+ WebMock.enable!(except: [http_library])
34
+ end
35
+ after(:each) do
36
+ WebMock.enable!
37
+ end
38
+ include_context "disabled WebMock"
39
+ end
40
+ end
41
+
42
+ shared_context "disabled WebMock" do
43
+ it "should not register executed requests" do
44
+ http_request(:get, webmock_server_url)
45
+ expect(a_request(:get, webmock_server_url)).not_to have_been_made
46
+ end
47
+
48
+ it "should not block unstubbed requests" do
49
+ expect {
50
+ http_request(:get, webmock_server_url)
51
+ }.not_to raise_error
52
+ end
53
+
54
+ it "should return real response even if there are stubs" do
55
+ stub_request(:get, /.*/).to_return(body: "x")
56
+ expect(http_request(:get, webmock_server_url).body).to eq("hello world")
57
+ end
58
+
59
+ it "should not invoke any callbacks" do
60
+ WebMock.reset_callbacks
61
+ stub_request(:get, webmock_server_url)
62
+ @called = nil
63
+ WebMock.after_request { @called = 1 }
64
+ http_request(:get, webmock_server_url)
65
+ expect(@called).to eq(nil)
66
+ end
67
+ end
68
+
69
+ shared_context "enabled WebMock" do
70
+ it "should register executed requests" do
71
+ WebMock.allow_net_connect!
72
+ http_request(:get, webmock_server_url)
73
+ expect(a_request(:get, webmock_server_url)).to have_been_made
74
+ end
75
+
76
+ it "should block unstubbed requests" do
77
+ expect {
78
+ http_request(:get, "http://www.example.com/")
79
+ }.to raise_error(WebMock::NetConnectNotAllowedError)
80
+ end
81
+
82
+ it "should return stubbed response" do
83
+ stub_request(:get, /.*/).to_return(body: "x")
84
+ expect(http_request(:get, "http://www.example.com/").body).to eq("x")
85
+ end
86
+
87
+ it "should invoke callbacks" do
88
+ WebMock.allow_net_connect!
89
+ WebMock.reset_callbacks
90
+ @called = nil
91
+ WebMock.after_request { @called = 1 }
92
+ http_request(:get, webmock_server_url)
93
+ expect(@called).to eq(1)
94
+ end
95
+ end
@@ -0,0 +1,15 @@
1
+ shared_context "precedence of stubs" do |*adapter_info|
2
+ describe "when choosing a matching request stub" do
3
+ it "should use the last declared matching request stub" do
4
+ stub_request(:get, "www.example.com").to_return(body: "abc")
5
+ stub_request(:get, "www.example.com").to_return(body: "def")
6
+ expect(http_request(:get, "http://www.example.com/").body).to eq("def")
7
+ end
8
+
9
+ it "should not be affected by the type of uri or request method" do
10
+ stub_request(:get, "www.example.com").to_return(body: "abc")
11
+ stub_request(:any, /.*example.*/).to_return(body: "def")
12
+ expect(http_request(:get, "http://www.example.com/").body).to eq("def")
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,930 @@
1
+ shared_context "request expectations" do |*adapter_info|
2
+ describe "when request expectations are set" do
3
+ describe "when net connect is not allowed" do
4
+ before(:each) do
5
+ WebMock.disable_net_connect!
6
+ stub_request(:any, "http://www.example.com")
7
+ stub_request(:any, "https://www.example.com")
8
+ end
9
+
10
+ it "should satisfy expectation if request was executed with the same uri and method" do
11
+ expect {
12
+ http_request(:get, "http://www.example.com/")
13
+ expect(a_request(:get, "http://www.example.com")).to have_been_made.once
14
+ }.not_to raise_error
15
+ end
16
+
17
+ it "should satisfy expectation declared on WebMock.resuest" do
18
+ expect {
19
+ http_request(:get, "http://www.example.com/")
20
+ expect(WebMock.request(:get, "http://www.example.com")).to have_been_made.once
21
+ }.not_to raise_error
22
+ end
23
+
24
+ it "should satisfy expectation if request was not expected and not executed" do
25
+ expect {
26
+ expect(a_request(:get, "http://www.example.com")).not_to have_been_made
27
+ }.not_to raise_error
28
+ end
29
+
30
+ it "should fail if request was not expected but executed" do
31
+ expect {
32
+ http_request(:get, "http://www.example.com/")
33
+ expect(a_request(:get, "http://www.example.com")).not_to have_been_made
34
+ }.to fail_with(%r(The request GET http://www.example.com/ was not expected to execute but it executed 1 time))
35
+ end
36
+
37
+ it "should fail resulting with failure with a message and executed requests listed" do
38
+ expect {
39
+ http_request(:get, "http://www.example.com/")
40
+ expect(a_request(:get, "http://www.example.com")).not_to have_been_made
41
+ }.to fail_with(%r{The following requests were made:\n\nGET http://www.example.com/.+was made 1 time})
42
+ end
43
+
44
+ it "should fail if request was not executed" do
45
+ expect {
46
+ expect(a_request(:get, "http://www.example.com")).to have_been_made
47
+ }.to fail_with(%r(The request GET http://www.example.com/ was expected to execute 1 time but it executed 0 times))
48
+ end
49
+
50
+ it "should fail if request was executed to different uri" do
51
+ expect {
52
+ http_request(:get, "http://www.example.com/")
53
+ expect(a_request(:get, "http://www.example.org")).to have_been_made
54
+ }.to fail_with(%r(The request GET http://www.example.org/ was expected to execute 1 time but it executed 0 times))
55
+ end
56
+
57
+ it "should fail if request was executed with different method" do
58
+ expect {
59
+ http_request(:post, "http://www.example.com/", body: "abc")
60
+ expect(a_request(:get, "http://www.example.com")).to have_been_made
61
+ }.to fail_with(%r(The request GET http://www.example.com/ was expected to execute 1 time but it executed 0 times))
62
+ end
63
+
64
+ it "should satisfy expectation if request was executed with different form of uri" do
65
+ expect {
66
+ http_request(:get, "http://www.example.com/")
67
+ expect(a_request(:get, "www.example.com")).to have_been_made
68
+ }.not_to raise_error
69
+ end
70
+
71
+ it "should satisfy expectation if request was executed with different form of uri without port " do
72
+ expect {
73
+ http_request(:get, "http://www.example.com/")
74
+ expect(a_request(:get, "www.example.com:80")).to have_been_made
75
+ }.not_to raise_error
76
+ end
77
+
78
+ it "should satisfy expectation if request was executed with different form of uri with port" do
79
+ expect {
80
+ http_request(:get, "http://www.example.com/")
81
+ expect(a_request(:get, "www.example.com:80")).to have_been_made
82
+ }.not_to raise_error
83
+ end
84
+
85
+ it "should fail if request was executed to a different port" do
86
+ expect {
87
+ http_request(:get, "http://www.example.com:80/")
88
+ expect(a_request(:get, "www.example.com:90")).to have_been_made
89
+ }.to fail_with(%r(The request GET http://www.example.com:90/ was expected to execute 1 time but it executed 0 times))
90
+ end
91
+
92
+ it "should satisfy expectation if request was executed with different form of uri with https port" do
93
+ expect {
94
+ http_request(:get, "https://www.example.com/")
95
+ expect(a_request(:get, "https://www.example.com:443/")).to have_been_made
96
+ }.not_to raise_error
97
+ end
98
+
99
+ it "should satisfy expectations even if requests were executed in different order than expectations were declared" do
100
+ stub_request(:post, "http://www.example.com")
101
+ http_request(:post, "http://www.example.com/", body: "def")
102
+ http_request(:post, "http://www.example.com/", body: "abc")
103
+ expect(WebMock).to have_requested(:post, "www.example.com").with(body: "abc")
104
+ expect(WebMock).to have_requested(:post, "www.example.com").with(body: "def")
105
+ end
106
+
107
+ describe "when matching requests with escaped or unescaped uris" do
108
+ before(:each) do
109
+ WebMock.disable_net_connect!
110
+ stub_request(:any, "http://www.example.com/?#{NOT_ESCAPED_PARAMS}")
111
+ end
112
+
113
+ it "should satisfy expectation if request was executed with escaped params" do
114
+ expect {
115
+ http_request(:get, "http://www.example.com/?#{ESCAPED_PARAMS}")
116
+ expect(a_request(:get, "http://www.example.com/?#{NOT_ESCAPED_PARAMS}")).to have_been_made
117
+ }.not_to raise_error
118
+ end
119
+
120
+ it "should satisfy expectation if request was executed with non escaped params" do
121
+ expect {
122
+ http_request(:get, "http://www.example.com/?#{NOT_ESCAPED_PARAMS}")
123
+ expect(a_request(:get, "http://www.example.com/?#{ESCAPED_PARAMS}")).to have_been_made
124
+ }.not_to raise_error
125
+ end
126
+
127
+ it "should satisfy expectation if request was executed with escaped params and uri matching regexp was expected" do
128
+ expect {
129
+ http_request(:get, "http://www.example.com/?#{ESCAPED_PARAMS}")
130
+ expect(a_request(:get, /.*example.*/)).to have_been_made
131
+ }.not_to raise_error
132
+ end
133
+
134
+ end
135
+
136
+ describe "when matching requests with query params" do
137
+ before(:each) do
138
+ stub_request(:any, /.*example.*/)
139
+ end
140
+
141
+ it "should satisfy expectation if the request was executed with query params declared as a hash in a query option" do
142
+ expect {
143
+ http_request(:get, "http://www.example.com/?a[]=b&a[]=c")
144
+ expect(a_request(:get, "www.example.com").with(query: {"a" => ["b", "c"]})).to have_been_made
145
+ }.not_to raise_error
146
+ end
147
+
148
+ it "should satisfy expectation if the request was executed with query params declared as string in query option" do
149
+ expect {
150
+ http_request(:get, "http://www.example.com/?a[]=b&a[]=c")
151
+ expect(a_request(:get, "www.example.com").with(query: "a[]=b&a[]=c")).to have_been_made
152
+ }.not_to raise_error
153
+ end
154
+
155
+ it "should satisfy expectation if the request was executed with query params both in uri and in query option" do
156
+ expect {
157
+ http_request(:get, "http://www.example.com/?x=3&a[]=b&a[]=c")
158
+ expect(a_request(:get, "www.example.com/?x=3").with(query: {"a" => ["b", "c"]})).to have_been_made
159
+ }.not_to raise_error
160
+ end
161
+
162
+ it "should satisfy expectation if the request was executed with only part query params declared as a hash in a query option" do
163
+ expect {
164
+ http_request(:get, "http://www.example.com/?a[]=b&a[]=c&b=1")
165
+ expect(a_request(:get, "www.example.com").with(query: hash_including({"a" => ["b", "c"]}))).to have_been_made
166
+ }.not_to raise_error
167
+ end
168
+
169
+ it 'should satisfy expectation if the request was executed with excluding part of query params declared as a hash in a query option' do
170
+ expect {
171
+ http_request(:get, "http://www.example.com/?a[]=d&b[]=e&b=1")
172
+ expect(a_request(:get, "www.example.com").with(query: hash_excluding(a: ['b', 'c']))).to have_been_made
173
+ }.not_to raise_error
174
+ end
175
+
176
+ it 'should satisfy expectation if the request was executed with an empty array in the query params' do
177
+ expect {
178
+ http_request(:get, "http://www.example.com/?a[]")
179
+ expect(a_request(:get, "www.example.com").with(query: hash_including(a: []))).to have_been_made
180
+ }.not_to raise_error
181
+ end
182
+ end
183
+
184
+ context "when using flat array notation" do
185
+ before :all do
186
+ WebMock::Config.instance.query_values_notation = :flat_array
187
+ end
188
+
189
+ it "should satisfy expectation if request includes different repeated query params in flat array notation" do
190
+ expect {
191
+ stub_request(:get, "http://www.example.com/?a=1&a=2")
192
+ http_request(:get, "http://www.example.com/?a=1&a=2")
193
+ expect(a_request(:get, "http://www.example.com/?a=1&a=2")).to have_been_made
194
+ }.not_to raise_error
195
+ end
196
+
197
+ after :all do
198
+ WebMock::Config.instance.query_values_notation = nil
199
+ end
200
+ end
201
+
202
+
203
+
204
+ describe "at_most_times" do
205
+ it "fails if request was made more times than maximum" do
206
+ expect {
207
+ http_request(:get, "http://www.example.com/")
208
+ http_request(:get, "http://www.example.com/")
209
+ http_request(:get, "http://www.example.com/")
210
+ expect(a_request(:get, "http://www.example.com")).to have_been_made.at_most_times(2)
211
+ }.to fail_with(%r(The request GET http://www.example.com/ was expected to execute at most 2 times but it executed 3 times))
212
+ end
213
+
214
+ it "passes if request was made the maximum number of times" do
215
+ expect {
216
+ http_request(:get, "http://www.example.com/")
217
+ http_request(:get, "http://www.example.com/")
218
+ expect(a_request(:get, "http://www.example.com")).to have_been_made.at_most_times(2)
219
+ }.not_to raise_error
220
+ end
221
+
222
+ it "passes if request was made fewer than the maximum number of times" do
223
+ expect {
224
+ http_request(:get, "http://www.example.com/")
225
+ expect(a_request(:get, "http://www.example.com")).to have_been_made.at_most_times(2)
226
+ }.not_to raise_error
227
+ end
228
+
229
+ it "passes if request was not made at all" do
230
+ expect {
231
+ expect(a_request(:get, "http://www.example.com")).to have_been_made.at_most_times(2)
232
+ }.not_to raise_error
233
+ end
234
+ end
235
+
236
+
237
+ describe "at_least_times" do
238
+ it "fails if request was made fewer times than minimum" do
239
+ expect {
240
+ http_request(:get, "http://www.example.com/")
241
+ expect(a_request(:get, "http://www.example.com")).to have_been_made.at_least_times(2)
242
+ }.to fail_with(%r(The request GET http://www.example.com/ was expected to execute at least 2 times but it executed 1 time))
243
+ end
244
+
245
+ it "passes if request was made the minimum number of times" do
246
+ expect {
247
+ http_request(:get, "http://www.example.com/")
248
+ http_request(:get, "http://www.example.com/")
249
+ expect(a_request(:get, "http://www.example.com")).to have_been_made.at_least_times(2)
250
+ }.not_to raise_error
251
+ end
252
+
253
+ it "passes if request was made more than the minimum number of times" do
254
+ expect {
255
+ http_request(:get, "http://www.example.com/")
256
+ http_request(:get, "http://www.example.com/")
257
+ http_request(:get, "http://www.example.com/")
258
+ expect(a_request(:get, "http://www.example.com")).to have_been_made.at_least_times(2)
259
+ }.not_to raise_error
260
+ end
261
+
262
+ context "descriptive at_most_ matcher" do
263
+ context "at_most_once" do
264
+ it "succeeds if no request was executed" do
265
+ expect {
266
+ expect(a_request(:get, "http://www.example.com")).to have_been_made.at_most_once
267
+ }.not_to raise_error
268
+ end
269
+
270
+ it "satisfies expectation if request was executed with the same uri and method once" do
271
+ expect {
272
+ http_request(:get, "http://www.example.com/")
273
+ expect(a_request(:get, "http://www.example.com")).to have_been_made.at_most_once
274
+ }.not_to raise_error
275
+ end
276
+
277
+ it "fails if request was executed with the same uri and method twice" do
278
+ expect {
279
+ http_request(:get, "http://www.example.com/")
280
+ http_request(:get, "http://www.example.com/")
281
+ expect(a_request(:get, "http://www.example.com")).to have_been_made.at_most_once
282
+ }.to fail
283
+ end
284
+ end
285
+
286
+ context "at_most_twice" do
287
+ it "succeeds if no request was executed" do
288
+ expect {
289
+ expect(a_request(:get, "http://www.example.com")).to have_been_made.at_most_twice
290
+ }.not_to raise_error
291
+ end
292
+
293
+ it "succeeds if too few requests were executed" do
294
+ expect {
295
+ http_request(:get, "http://www.example.com/")
296
+ expect(a_request(:get, "http://www.example.com")).to have_been_made.at_most_twice
297
+ }.not_to raise_error
298
+ end
299
+
300
+ it "satisfies expectation if request was executed with the same uri and method twice" do
301
+ expect {
302
+ http_request(:get, "http://www.example.com/")
303
+ http_request(:get, "http://www.example.com/")
304
+ expect(a_request(:get, "http://www.example.com")).to have_been_made.at_most_twice
305
+ }.not_to raise_error
306
+ end
307
+
308
+ it "fails if request was executed with the same uri and method three times" do
309
+ expect {
310
+ http_request(:get, "http://www.example.com/")
311
+ http_request(:get, "http://www.example.com/")
312
+ http_request(:get, "http://www.example.com/")
313
+ expect(a_request(:get, "http://www.example.com")).to have_been_made.at_most_twice
314
+ }.to fail
315
+ end
316
+ end
317
+ end
318
+
319
+ context "descriptive at_least_ matcher" do
320
+ context "at_least_once" do
321
+ it "fails if no request was executed" do
322
+ expect {
323
+ expect(a_request(:get, "http://www.example.com")).to have_been_made.at_least_once
324
+ }.to fail
325
+ end
326
+
327
+ it "satisfies expectation if request was executed with the same uri and method once" do
328
+ expect {
329
+ http_request(:get, "http://www.example.com/")
330
+ expect(a_request(:get, "http://www.example.com")).to have_been_made.at_least_once
331
+ }.not_to raise_error
332
+ end
333
+
334
+ it "satisfies expectation if request was executed with the same uri and method twice" do
335
+ expect {
336
+ http_request(:get, "http://www.example.com/")
337
+ http_request(:get, "http://www.example.com/")
338
+ expect(a_request(:get, "http://www.example.com")).to have_been_made.at_least_once
339
+ }.not_to raise_error
340
+ end
341
+ end
342
+
343
+ context "at_least_twice" do
344
+ it "fails if no request was executed" do
345
+ expect {
346
+ expect(a_request(:get, "http://www.example.com")).to have_been_made.at_least_twice
347
+ }.to fail
348
+ end
349
+
350
+ it "fails if too few requests were executed" do
351
+ expect {
352
+ http_request(:get, "http://www.example.com/")
353
+ expect(a_request(:get, "http://www.example.com")).to have_been_made.at_least_twice
354
+ }.to fail
355
+ end
356
+
357
+ it "satisfies expectation if request was executed with the same uri and method twice" do
358
+ expect {
359
+ http_request(:get, "http://www.example.com/")
360
+ http_request(:get, "http://www.example.com/")
361
+ expect(a_request(:get, "http://www.example.com")).to have_been_made.at_least_twice
362
+ }.not_to raise_error
363
+ end
364
+
365
+ it "satisfies expectation if request was executed with the same uri and method three times" do
366
+ expect {
367
+ http_request(:get, "http://www.example.com/")
368
+ http_request(:get, "http://www.example.com/")
369
+ http_request(:get, "http://www.example.com/")
370
+ expect(a_request(:get, "http://www.example.com")).to have_been_made.at_least_twice
371
+ }.not_to raise_error
372
+ end
373
+ end
374
+ end
375
+ end
376
+
377
+ it "should fail if request was made more times than expected" do
378
+ expect {
379
+ http_request(:get, "http://www.example.com/")
380
+ http_request(:get, "http://www.example.com/")
381
+ expect(a_request(:get, "http://www.example.com")).to have_been_made
382
+ }.to fail_with(%r(The request GET http://www.example.com/ was expected to execute 1 time but it executed 2 times))
383
+ end
384
+
385
+ it "should fail if request was made less times than expected" do
386
+ expect {
387
+ http_request(:get, "http://www.example.com/")
388
+ expect(a_request(:get, "http://www.example.com")).to have_been_made.twice
389
+ }.to fail_with(%r(The request GET http://www.example.com/ was expected to execute 2 times but it executed 1 time))
390
+ end
391
+
392
+ it "should fail if request was made less times than expected when 3 times expected" do
393
+ expect {
394
+ http_request(:get, "http://www.example.com/")
395
+ expect(a_request(:get, "http://www.example.com")).to have_been_made.times(3)
396
+ }.to fail_with(%r(The request GET http://www.example.com/ was expected to execute 3 times but it executed 1 time))
397
+ end
398
+
399
+ it "should satisfy expectation if request was executed with the same body" do
400
+ expect {
401
+ http_request(:post, "http://www.example.com/", body: "abc")
402
+ expect(a_request(:post, "www.example.com").with(body: "abc")).to have_been_made
403
+ }.not_to raise_error
404
+ end
405
+
406
+ it "should fail if request was executed with different body" do
407
+ expect {
408
+ http_request(:post, "http://www.example.com/", body: "abc")
409
+ expect(a_request(:post, "www.example.com").
410
+ with(body: "def")).to have_been_made
411
+ }.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))
412
+ end
413
+
414
+ describe "when expected request body is declared as a regexp" do
415
+ it "should satisfy expectation if request was executed with body matching regexp" do
416
+ expect {
417
+ http_request(:post, "http://www.example.com/", body: "abc")
418
+ expect(a_request(:post, "www.example.com").with(body: /^abc$/)).to have_been_made
419
+ }.not_to raise_error
420
+ end
421
+
422
+ it "should fail if request was executed with body not matching regexp" do
423
+ expect {
424
+ http_request(:post, "http://www.example.com/", body: "abc")
425
+ expect(a_request(:post, "www.example.com").
426
+ with(body: /^xabc/)).to have_been_made
427
+ }.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))
428
+ end
429
+
430
+ end
431
+
432
+ describe "when expected reqest body is declared as a hash" do
433
+ let(:body_hash) { {:a => '1', :b => 'five', 'c' => {'d' => ['e', 'f']}} }
434
+ let(:fail_message) {%r(The request POST http://www.example.com/ with body .+ was expected to execute 1 time but it executed 0 times)}
435
+
436
+ describe "when request is made with url encoded body matching hash" do
437
+ it "should satisfy expectation" do
438
+ expect {
439
+ http_request(:post, "http://www.example.com/", body: 'a=1&c[d][]=e&c[d][]=f&b=five')
440
+ expect(a_request(:post, "www.example.com").with(body: body_hash)).to have_been_made
441
+ }.not_to raise_error
442
+ end
443
+
444
+ it "should satisfy expectation even if url encoded params have different order" do
445
+ expect {
446
+ http_request(:post, "http://www.example.com/", body: 'a=1&c[d][]=e&b=five&c[d][]=f')
447
+ expect(a_request(:post, "www.example.com").with(body: body_hash)).to have_been_made
448
+ }.not_to raise_error
449
+ end
450
+
451
+ it "should fail if request is executed with url encoded body not matching hash" do
452
+ expect {
453
+ http_request(:post, "http://www.example.com/", body: 'c[d][]=f&a=1&c[d][]=e')
454
+ expect(a_request(:post, "www.example.com").with(body: body_hash)).to have_been_made
455
+ }.to fail_with(fail_message)
456
+ end
457
+ end
458
+
459
+ describe "when request is executed with json body matching hash and Content-Type is set to json" do
460
+ it "should satisfy expectation" do
461
+ expect {
462
+ http_request(:post, "http://www.example.com/", headers: {'Content-Type' => 'application/json'},
463
+ body: "{\"a\":\"1\",\"c\":{\"d\":[\"e\",\"f\"]},\"b\":\"five\"}")
464
+ expect(a_request(:post, "www.example.com").with(body: body_hash)).to have_been_made
465
+ }.not_to raise_error
466
+ end
467
+
468
+ it "should satisfy expectation even if json body is in different form" do
469
+ expect {
470
+ http_request(:post, "http://www.example.com/", headers: {'Content-Type' => 'application/json'},
471
+ body: "{\"a\":\"1\",\"b\":\"five\",\"c\":{\"d\":[\"e\",\"f\"]}}")
472
+ expect(a_request(:post, "www.example.com").with(body: body_hash)).to have_been_made
473
+ }.not_to raise_error
474
+ end
475
+
476
+ it "should satisfy expectation even if json body contains date string" do
477
+ expect {
478
+ http_request(:post, "http://www.example.com/", headers: {'Content-Type' => 'application/json'},
479
+ body: "{\"foo\":\"2010-01-01\"}")
480
+ expect(a_request(:post, "www.example.com").with(body: {"foo" => "2010-01-01"})).to have_been_made
481
+ }.not_to raise_error
482
+ end
483
+ end
484
+
485
+
486
+ describe "when request is executed with xml body matching hash and content type is set to xml" do
487
+ let(:body_hash) { { "opt" => {:a => "1", :b => 'five', 'c' => {'d' => ['e', 'f']}} }}
488
+
489
+ it "should satisfy expectation" do
490
+ expect {
491
+ http_request(:post, "http://www.example.com/", headers: {'Content-Type' => 'application/xml'},
492
+ body: "<opt a=\"1\" b=\"five\">\n <c>\n <d>e</d>\n <d>f</d>\n </c>\n</opt>\n")
493
+ expect(a_request(:post, "www.example.com").with(body: body_hash)).to have_been_made
494
+ }.not_to raise_error
495
+ end
496
+
497
+ it "should satisfy expectation even if xml body is in different form" do
498
+ expect {
499
+ http_request(:post, "http://www.example.com/", headers: {'Content-Type' => 'application/xml'},
500
+ body: "<opt b=\"five\" a=\"1\">\n <c>\n <d>e</d>\n <d>f</d>\n </c>\n</opt>\n")
501
+ expect(a_request(:post, "www.example.com").with(body: body_hash)).to have_been_made
502
+ }.not_to raise_error
503
+ end
504
+
505
+ it "should satisfy expectation even if xml body contains date string" do
506
+ expect {
507
+ http_request(:post, "http://www.example.com/", headers: {'Content-Type' => 'application/xml'},
508
+ body: "<opt foo=\"2010-01-01\">\n</opt>\n")
509
+ expect(a_request(:post, "www.example.com").with(body: {"opt" => {"foo" => "2010-01-01"}})).to have_been_made
510
+ }.not_to raise_error
511
+ end
512
+ end
513
+ end
514
+
515
+ describe "when expected reqest body is declared as a partial hash matcher" do
516
+ let(:body_hash) { hash_including({:a => '1', 'c' => {'d' => ['e', 'f']}}) }
517
+ 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)}
518
+
519
+ describe "when request is made with url encoded body matching hash" do
520
+ it "should satisfy expectation" do
521
+ expect {
522
+ http_request(:post, "http://www.example.com/", body: 'a=1&c[d][]=e&c[d][]=f&b=five')
523
+ expect(a_request(:post, "www.example.com").with(body: body_hash)).to have_been_made
524
+ }.not_to raise_error
525
+ end
526
+
527
+ it "should fail if request is executed with url encoded body not matching hash" do
528
+ expect {
529
+ http_request(:post, "http://www.example.com/", body: 'c[d][]=f&a=1&c[d][]=e')
530
+ expect(a_request(:post, "www.example.com").with(body: body_hash)).to have_been_made
531
+ }.to fail_with(fail_message)
532
+ end
533
+ end
534
+ end
535
+
536
+ describe "when request with headers is expected" do
537
+ it "should satisfy expectation if request was executed with the same headers" do
538
+ expect {
539
+ http_request(:get, "http://www.example.com/", headers: SAMPLE_HEADERS)
540
+ expect(a_request(:get, "www.example.com").
541
+ with(headers: SAMPLE_HEADERS)).to have_been_made
542
+ }.not_to raise_error
543
+ end
544
+
545
+ it "should satisfy expectation if request was executed with the same headers but with header value declared as array" do
546
+ expect {
547
+ http_request(:get, "http://www.example.com/", headers: {"a" => "b"})
548
+ expect(a_request(:get, "www.example.com").
549
+ with(headers: {"a" => ["b"]})).to have_been_made
550
+ }.not_to raise_error
551
+ end
552
+
553
+ describe "when multiple headers with the same key are passed" do
554
+ it "should satisfy expectation" do
555
+ expect {
556
+ http_request(:get, "http://www.example.com/", headers: {"a" => ["b", "c"]})
557
+ expect(a_request(:get, "www.example.com").
558
+ with(headers: {"a" => ["b", "c"]})).to have_been_made
559
+ }.not_to raise_error
560
+ end
561
+
562
+ it "should satisfy expectation even if request was executed with the same headers but different order" do
563
+ expect {
564
+ http_request(:get, "http://www.example.com/", headers: {"a" => ["b", "c"]})
565
+ expect(a_request(:get, "www.example.com").
566
+ with(headers: {"a" => ["c", "b"]})).to have_been_made
567
+ }.not_to raise_error
568
+ end
569
+
570
+ it "should fail if request was executed with different headers" do
571
+ expect {
572
+ http_request(:get, "http://www.example.com/", headers: {"a" => ["b", "c"]})
573
+ expect(a_request(:get, "www.example.com").
574
+ with(headers: {"a" => ["b", "d"]})).to have_been_made
575
+ }.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))
576
+ end
577
+ end
578
+
579
+ it "should fail if request was executed with different headers" do
580
+ expect {
581
+ http_request(:get, "http://www.example.com/", headers: SAMPLE_HEADERS)
582
+ expect(a_request(:get, "www.example.com").
583
+ with(headers: { 'Content-Length' => '9999'})).to have_been_made
584
+ }.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))
585
+ end
586
+
587
+ it "should fail if request was executed with less headers" do
588
+ expect {
589
+ http_request(:get, "http://www.example.com/", headers: {'A' => 'a'})
590
+ expect(a_request(:get, "www.example.com").
591
+ with(headers: {'A' => 'a', 'B' => 'b'})).to have_been_made
592
+ }.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))
593
+ end
594
+
595
+ it "should satisfy expectation if request was executed with more headers" do
596
+ expect {
597
+ http_request(:get, "http://www.example.com/",
598
+ headers: {'A' => 'a', 'B' => 'b'}
599
+ )
600
+ expect(a_request(:get, "www.example.com").
601
+ with(headers: {'A' => 'a'})).to have_been_made
602
+ }.not_to raise_error
603
+ end
604
+
605
+ it "should satisfy expectation if request was executed with body and headers but they were not specified in expectantion" do
606
+ expect {
607
+ http_request(:post, "http://www.example.com/",
608
+ body: "abc",
609
+ headers: SAMPLE_HEADERS
610
+ )
611
+ expect(a_request(:post, "www.example.com")).to have_been_made
612
+ }.not_to raise_error
613
+ end
614
+
615
+ it "should satisfy expectation if request was executed with headers matching regular expressions" do
616
+ expect {
617
+ http_request(:get, "http://www.example.com/", headers: { 'some-header' => 'MyAppName' })
618
+ expect(a_request(:get, "www.example.com").
619
+ with(headers: { some_header: /^MyAppName$/ })).to have_been_made
620
+ }.not_to raise_error
621
+ end
622
+
623
+ it "should fail if request was executed with headers not matching regular expression" do
624
+ expect {
625
+ http_request(:get, "http://www.example.com/", headers: { 'some-header' => 'xMyAppName' })
626
+ expect(a_request(:get, "www.example.com").
627
+ with(headers: { some_header: /^MyAppName$/ })).to have_been_made
628
+ }.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))
629
+ end
630
+ end
631
+
632
+ describe "when expectation contains a request matching block" do
633
+ it "should satisfy expectation if request was executed and block evaluated to true" do
634
+ expect {
635
+ http_request(:post, "http://www.example.com/", body: "wadus")
636
+ expect(a_request(:post, "www.example.com").with { |req| req.body == "wadus" }).to have_been_made
637
+ }.not_to raise_error
638
+ end
639
+
640
+ it "should fail if request was executed and block evaluated to false" do
641
+ expect {
642
+ http_request(:post, "http://www.example.com/", body: "abc")
643
+ expect(a_request(:post, "www.example.com").with { |req| req.body == "wadus" }).to have_been_made
644
+ }.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))
645
+ end
646
+
647
+ it "should fail if request was not expected but it executed and block matched request" do
648
+ expect {
649
+ http_request(:post, "http://www.example.com/", body: "wadus")
650
+ expect(a_request(:post, "www.example.com").with { |req| req.body == "wadus" }).not_to have_been_made
651
+ }.to fail_with(%r(The request POST http://www.example.com/ with given block was not expected to execute but it executed 1 time))
652
+ end
653
+ end
654
+
655
+ describe "with userinfo", unless: (adapter_info.include?(:no_url_auth)) do
656
+ before(:each) do
657
+ stub_request(:any, "http://user:pass@www.example.com")
658
+ stub_request(:any, "http://user:pazz@www.example.com")
659
+ end
660
+
661
+ it "should satisfy expectation if request was executed with expected credentials" do
662
+ expect {
663
+ http_request(:get, "http://user:pass@www.example.com/")
664
+ expect(a_request(:get, "http://user:pass@www.example.com")).to have_been_made.once
665
+ }.not_to raise_error
666
+ end
667
+
668
+ it "should fail if request was executed with different credentials than expected" do
669
+ expect {
670
+ http_request(:get, "http://user:pass@www.example.com/")
671
+ expect(a_request(:get, "http://user:pazz@www.example.com")).to have_been_made.once
672
+ }.to fail_with(%r(The request GET http://user:pazz@www.example.com/ was expected to execute 1 time but it executed 0 times))
673
+ end
674
+
675
+ it "should fail if request was executed without credentials and credentials were expected" do
676
+ expect {
677
+ http_request(:get, "http://www.example.com/")
678
+ expect(a_request(:get, "http://user:pass@www.example.com")).to have_been_made.once
679
+ }.to fail_with(%r(The request GET http://user:pass@www.example.com/ was expected to execute 1 time but it executed 0 times))
680
+ end
681
+
682
+ it "should fail if request was executed with credentials but expected without credentials" do
683
+ expect {
684
+ http_request(:get, "http://user:pass@www.example.com/")
685
+ expect(a_request(:get, "http://www.example.com")).to have_been_made.once
686
+ }.to fail_with(%r(The request GET http://www.example.com/ was expected to execute 1 time but it executed 0 times))
687
+ end
688
+
689
+ it "should fail if request was executed with basic auth header but expected with credentials in userinfo" do
690
+ expect {
691
+ http_request(:get, "http://www.example.com/", basic_auth: ['user', 'pass'])
692
+ expect(a_request(:get, "http://user:pass@www.example.com")).to have_been_made.once
693
+ }.to fail_with(%r(The request GET http://user:pass@www.example.com/ was expected to execute 1 time but it executed 0 times))
694
+ end
695
+ end
696
+
697
+ describe "with basic authentication header" do
698
+ before(:each) do
699
+ stub_request(:any, "http://www.example.com").with(basic_auth: ['user', 'pass'])
700
+ stub_request(:any, "http://www.example.com").with(basic_auth: ['user', 'pazz'])
701
+ end
702
+
703
+ it "should satisfy expectation if request was executed with expected credentials" do
704
+ expect {
705
+ http_request(:get, "http://www.example.com/", basic_auth: ['user', 'pass'])
706
+ expect(a_request(:get, "http://www.example.com").with(basic_auth: ['user', 'pass'])).to have_been_made.once
707
+ }.not_to raise_error
708
+ end
709
+
710
+ it "should satisfy expectation if request was executed with expected credentials passed directly as header" do
711
+ expect {
712
+ http_request(:get, "http://www.example.com/", headers: {'Authorization'=>'Basic dXNlcjpwYXNz'})
713
+ expect(a_request(:get, "http://www.example.com").with(basic_auth: ['user', 'pass'])).to have_been_made.once
714
+ }.not_to raise_error
715
+ end
716
+
717
+ it "should fail if request was executed with different credentials than expected" do
718
+ expect {
719
+ http_request(:get, "http://www.example.com/", basic_auth: ['user', 'pass'])
720
+ expect(a_request(:get, "http://www.example.com").with(basic_auth: ['user', 'pazz'])).to have_been_made.once
721
+ }.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))
722
+ end
723
+
724
+ it "should fail if request was executed without credentials and credentials were expected" do
725
+ expect {
726
+ http_request(:get, "http://www.example.com/")
727
+ expect(a_request(:get, "http://www.example.com").with(basic_auth: ['user', 'pass'])).to have_been_made.once
728
+ }.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))
729
+ end
730
+
731
+ it "should not fail if request was executed with credentials but expected despite credentials" do
732
+ expect {
733
+ http_request(:get, "http://www.example.com/", basic_auth: ['user', 'pass'])
734
+ expect(a_request(:get, "http://www.example.com")).to have_been_made.once
735
+ }.not_to raise_error
736
+ end
737
+
738
+ 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
739
+ expect {
740
+ stub_request(:any, "http://user:pass@www.example.com")
741
+ http_request(:get, "http://user:pass@www.example.com/")
742
+ expect(a_request(:get, "http://www.example.com").with(basic_auth: ['user', 'pass'])).to have_been_made.once
743
+ }.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))
744
+ end
745
+ end
746
+
747
+ describe "when expectations were set on WebMock object" do
748
+ it "should satisfy expectation if expected request was made" do
749
+ expect {
750
+ http_request(:get, "http://www.example.com/")
751
+ expect(WebMock).to have_requested(:get, "http://www.example.com").once
752
+ }.not_to raise_error
753
+ end
754
+
755
+ it "should satisfy expectation if request with body and headers was expected and request was made" do
756
+ expect {
757
+ http_request(:post, "http://www.example.com/", body: "abc", headers: {'A' => 'a'})
758
+ expect(WebMock).to have_requested(:post, "http://www.example.com").with(body: "abc", headers: {'A' => 'a'}).once
759
+ }.not_to raise_error
760
+ end
761
+
762
+ it "should fail if request expected not to be made was made" do
763
+ expect {
764
+ http_request(:get, "http://www.example.com/")
765
+ expect(WebMock).not_to have_requested(:get, "http://www.example.com")
766
+ }.to fail_with(%r(The request GET http://www.example.com/ was not expected to execute but it executed 1 time))
767
+ end
768
+
769
+ it "should satisfy expectation if request was executed and expectation had block which evaluated to true" do
770
+ expect {
771
+ http_request(:post, "http://www.example.com/", body: "wadus")
772
+ expect(WebMock).to have_requested(:post, "www.example.com").with { |req| req.body == "wadus" }
773
+ }.not_to raise_error
774
+ end
775
+
776
+ it "should fail if request was executed and expectation had block which evaluated to false" do
777
+ expect {
778
+ http_request(:post, "http://www.example.com/", body: "abc")
779
+ expect(WebMock).to have_requested(:post, "www.example.com").with { |req| req.body == "wadus" }
780
+ }.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))
781
+ end
782
+
783
+ it "should fail if request was expected not to be made but was made and block matched request" do
784
+ expect {
785
+ http_request(:post, "http://www.example.com/", body: "wadus")
786
+ expect(WebMock).not_to have_requested(:post, "www.example.com").with { |req| req.body == "wadus" }
787
+ }.to fail_with(%r(The request POST http://www.example.com/ with given block was not expected to execute but it executed 1 time))
788
+ end
789
+ end
790
+
791
+ describe "when expectation is declared using assert_requested" do
792
+ it "should satisfy expectation if requests was made" do
793
+ expect {
794
+ http_request(:get, "http://www.example.com/")
795
+ assert_requested(:get, "http://www.example.com", times: 1)
796
+ assert_requested(:get, "http://www.example.com")
797
+ }.not_to raise_error
798
+ end
799
+
800
+ it "should satisfy expectation if request was made with body and headers" do
801
+ expect {
802
+ http_request(:post, "http://www.example.com/", body: "abc", headers: {'A' => 'a'})
803
+ assert_requested(:post, "http://www.example.com", body: "abc", headers: {'A' => 'a'})
804
+ }.not_to raise_error
805
+ end
806
+
807
+ it "should fail if request expected not to be made was not wade" do
808
+ expect {
809
+ http_request(:get, "http://www.example.com/")
810
+ assert_not_requested(:get, "http://www.example.com")
811
+ }.to fail_with(%r(The request GET http://www.example.com/ was not expected to execute but it executed 1 time))
812
+ end
813
+
814
+ it "should fail if request expected not to be made was made and expectation block evaluated to true" do
815
+ expect {
816
+ http_request(:post, "http://www.example.com/", body: "wadus")
817
+ assert_not_requested(:post, "www.example.com") { |req| req.body == "wadus" }
818
+ }.to fail_with(%r(The request POST http://www.example.com/ with given block was not expected to execute but it executed 1 time))
819
+ end
820
+
821
+ it "should satisfy expectation if request was made and expectation block evaluated to true" do
822
+ expect {
823
+ http_request(:post, "http://www.example.com/", body: "wadus")
824
+ assert_requested(:post, "www.example.com") { |req| req.body == "wadus" }
825
+ }.not_to raise_error
826
+ end
827
+
828
+ it "should fail if request was made and expectation block evaluated to false" do
829
+ expect {
830
+ http_request(:post, "http://www.example.com/", body: "abc")
831
+ assert_requested(:post, "www.example.com") { |req| req.body == "wadus" }
832
+ }.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))
833
+ end
834
+ end
835
+
836
+ describe "when expectation is declared using assert_requested" do
837
+ it "should satisfy expectation if requests was made" do
838
+ stub_http = stub_http_request(:get, "http://www.example.com")
839
+ expect {
840
+ http_request(:get, "http://www.example.com/")
841
+ assert_requested(stub_http, times: 1)
842
+ assert_requested(stub_http)
843
+ }.not_to raise_error
844
+ end
845
+
846
+ it "should fail if request expected not to be made was not wade" do
847
+ stub_http = stub_http_request(:get, "http://www.example.com")
848
+ expect {
849
+ http_request(:get, "http://www.example.com/")
850
+ assert_not_requested(stub_http)
851
+ }.to fail_with(%r(The request GET http://www.example.com/ was not expected to execute but it executed 1 time))
852
+ end
853
+ end
854
+ end
855
+
856
+
857
+ describe "expectation is set on the request stub" do
858
+ it "should satisfy expectation if expected request was made" do
859
+ stub = stub_request(:get, "http://www.example.com/")
860
+ http_request(:get, "http://www.example.com/")
861
+ expect(stub).to have_been_requested.once
862
+ end
863
+
864
+ it "should satisfy expectations if subsequent requests were made" do
865
+ stub = stub_request(:get, "http://www.example.com/")
866
+ http_request(:get, "http://www.example.com/")
867
+ expect(stub).to have_been_requested.once
868
+ http_request(:get, "http://www.example.com/")
869
+ expect(stub).to have_been_requested.twice
870
+ end
871
+
872
+ it "should satisfy expectation if expected request with body and headers was made" do
873
+ stub = stub_request(:post, "http://www.example.com").with(body: "abc", headers: {'A' => 'a'})
874
+ http_request(:post, "http://www.example.com/", body: "abc", headers: {'A' => 'a'})
875
+ expect(stub).to have_been_requested.once
876
+ end
877
+
878
+ it "should fail if request not expected to be made was made" do
879
+ expect {
880
+ stub = stub_request(:get, "http://www.example.com")
881
+ http_request(:get, "http://www.example.com/")
882
+ expect(stub).not_to have_been_requested
883
+ }.to fail_with(%r(The request GET http://www.example.com/ was not expected to execute but it executed 1 time))
884
+ end
885
+
886
+ it "should fail request not expected to be made was made and expectation block evaluated to true" do
887
+ expect {
888
+ stub = stub_request(:post, "www.example.com").with { |req| req.body == "wadus" }
889
+ http_request(:post, "http://www.example.com/", body: "wadus")
890
+ expect(stub).not_to have_been_requested
891
+ }.to fail_with(%r(The request POST http://www.example.com/ with given block was not expected to execute but it executed 1 time))
892
+ end
893
+
894
+ it "should satisfy expectation if request was made and expectation block evaluated to true" do
895
+ stub = stub_request(:post, "www.example.com").with { |req| req.body == "wadus" }
896
+ http_request(:post, "http://www.example.com/", body: "wadus")
897
+ expect(stub).to have_been_requested
898
+ end
899
+
900
+ it "should satisfy expectation if request was made and expectation block evaluated to false" do
901
+ expect {
902
+ stub_request(:any, /.+/) #stub any request
903
+ stub = stub_request(:post, "www.example.com").with { |req| req.body == "wadus" }
904
+ http_request(:post, "http://www.example.com/", body: "abc")
905
+ expect(stub).to have_been_requested
906
+ }.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))
907
+ end
908
+ end
909
+
910
+ describe "when net connect is allowed", net_connect: true do
911
+ before(:each) do
912
+ WebMock.allow_net_connect!
913
+ end
914
+
915
+ it "should satisfy expectation if expected requests was made" do
916
+ expect {
917
+ http_request(:get, "http://www.example.com/")
918
+ expect(a_request(:get, "http://www.example.com")).to have_been_made
919
+ }.not_to raise_error
920
+ end
921
+
922
+ it "should fail request expected not to be made, was made" do
923
+ expect {
924
+ http_request(:get, "http://www.example.com/")
925
+ expect(a_request(:get, "http://www.example.com")).not_to have_been_made
926
+ }.to fail_with(%r(The request GET http://www.example.com/ was not expected to execute but it executed 1 time))
927
+ end
928
+ end
929
+ end
930
+ end