webmock 3.5.0 → 3.25.0

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