webmock 1.7.5 → 1.7.6

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 (65) hide show
  1. data/.rspec-tm +2 -0
  2. data/CHANGELOG.md +6 -0
  3. data/Gemfile +2 -2
  4. data/README.md +1 -0
  5. data/Rakefile +3 -3
  6. data/lib/webmock/http_lib_adapters/httpclient_adapter.rb +11 -4
  7. data/lib/webmock/version.rb +1 -1
  8. data/spec/{curb_spec.rb → acceptance/curb/curb_spec.rb} +9 -9
  9. data/spec/{curb_spec_helper.rb → acceptance/curb/curb_spec_helper.rb} +0 -0
  10. data/spec/{em_http_request_spec.rb → acceptance/em_http_request/em_http_request_spec.rb} +13 -13
  11. data/spec/{em_http_request_spec_helper.rb → acceptance/em_http_request/em_http_request_spec_helper.rb} +0 -0
  12. data/spec/acceptance/httpclient/httpclient_spec.rb +67 -0
  13. data/spec/{httpclient_spec_helper.rb → acceptance/httpclient/httpclient_spec_helper.rb} +0 -0
  14. data/spec/{net_http_shared.rb → acceptance/net_http/net_http_shared.rb} +0 -0
  15. data/spec/{net_http_spec.rb → acceptance/net_http/net_http_spec.rb} +6 -6
  16. data/spec/{net_http_spec_helper.rb → acceptance/net_http/net_http_spec_helper.rb} +0 -0
  17. data/spec/{real_net_http_spec.rb → acceptance/net_http/real_net_http_spec.rb} +1 -1
  18. data/spec/{patron_spec.rb → acceptance/patron/patron_spec.rb} +12 -12
  19. data/spec/{patron_spec_helper.rb → acceptance/patron/patron_spec_helper.rb} +0 -0
  20. data/spec/acceptance/shared/allowing_and_disabling_net_connect.rb +142 -0
  21. data/spec/acceptance/shared/callbacks.rb +130 -0
  22. data/spec/acceptance/shared/enabling_and_disabling_webmock.rb +96 -0
  23. data/spec/acceptance/shared/precedence_of_stubs.rb +15 -0
  24. data/spec/acceptance/shared/request_expectations.rb +620 -0
  25. data/spec/acceptance/shared/returning_declared_responses.rb +377 -0
  26. data/spec/acceptance/shared/stubbing_requests.rb +314 -0
  27. data/spec/{typhoeus_hydra_spec.rb → acceptance/typhoeus/typhoeus_hydra_spec.rb} +5 -5
  28. data/spec/{typhoeus_hydra_spec_helper.rb → acceptance/typhoeus/typhoeus_hydra_spec_helper.rb} +0 -0
  29. data/spec/acceptance/webmock_shared.rb +38 -0
  30. data/spec/spec_helper.rb +4 -2
  31. data/spec/{example_curl_output.txt → support/example_curl_output.txt} +0 -0
  32. data/spec/{my_rack_app.rb → support/my_rack_app.rb} +0 -0
  33. data/spec/{network_connection.rb → support/network_connection.rb} +0 -0
  34. data/spec/{errors_spec.rb → unit/errors_spec.rb} +1 -1
  35. data/spec/{http_lib_adapters → unit/http_lib_adapters}/http_lib_adapter_registry_spec.rb +1 -1
  36. data/spec/{http_lib_adapters → unit/http_lib_adapters}/http_lib_adapter_spec.rb +1 -1
  37. data/spec/{rack_response_spec.rb → unit/rack_response_spec.rb} +1 -1
  38. data/spec/{request_execution_verifier_spec.rb → unit/request_execution_verifier_spec.rb} +1 -1
  39. data/spec/{request_pattern_spec.rb → unit/request_pattern_spec.rb} +1 -1
  40. data/spec/{request_registry_spec.rb → unit/request_registry_spec.rb} +1 -1
  41. data/spec/{request_signature_spec.rb → unit/request_signature_spec.rb} +1 -1
  42. data/spec/{request_stub_spec.rb → unit/request_stub_spec.rb} +1 -1
  43. data/spec/{response_spec.rb → unit/response_spec.rb} +4 -4
  44. data/spec/{stub_registry_spec.rb → unit/stub_registry_spec.rb} +1 -1
  45. data/spec/{stub_request_snippet_spec.rb → unit/stub_request_snippet_spec.rb} +1 -1
  46. data/spec/{util → unit/util}/hash_counter_spec.rb +1 -1
  47. data/spec/{util → unit/util}/hash_keys_stringifier_spec.rb +1 -1
  48. data/spec/{util → unit/util}/headers_spec.rb +1 -1
  49. data/spec/{util → unit/util}/json_spec.rb +1 -1
  50. data/spec/{util → unit/util}/uri_spec.rb +1 -1
  51. data/spec/{webmock_spec.rb → unit/webmock_spec.rb} +1 -1
  52. data/webmock.gemspec +1 -1
  53. metadata +111 -116
  54. data/spec/httpclient_spec.rb +0 -43
  55. data/spec/vendor/addressable/lib/addressable/uri.rb +0 -8
  56. data/spec/vendor/addressable/lib/uri.rb +0 -1
  57. data/spec/vendor/crack/lib/crack.rb +0 -1
  58. data/spec/vendor/right_http_connection-1.2.4/History.txt +0 -59
  59. data/spec/vendor/right_http_connection-1.2.4/Manifest.txt +0 -7
  60. data/spec/vendor/right_http_connection-1.2.4/README.txt +0 -54
  61. data/spec/vendor/right_http_connection-1.2.4/Rakefile +0 -103
  62. data/spec/vendor/right_http_connection-1.2.4/lib/net_fix.rb +0 -160
  63. data/spec/vendor/right_http_connection-1.2.4/lib/right_http_connection.rb +0 -435
  64. data/spec/vendor/right_http_connection-1.2.4/setup.rb +0 -1585
  65. data/spec/webmock_shared.rb +0 -1770
@@ -1,1770 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
-
3
- unless defined? SAMPLE_HEADERS
4
- SAMPLE_HEADERS = { "Content-Length" => "8888", "Accept" => "application/json" }
5
- ESCAPED_PARAMS = "x=ab%20c&z=%27Stop%21%27%20said%20Fred"
6
- NOT_ESCAPED_PARAMS = "z='Stop!' said Fred&x=ab c"
7
- end
8
-
9
- class MyException < StandardError; end;
10
-
11
- shared_examples_for "WebMock" do
12
- let(:webmock_server_url) {"http://#{WebMockServer.instance.host_with_port}/"}
13
- before(:each) do
14
- WebMock.disable_net_connect!
15
- WebMock.reset!
16
- end
17
-
18
- describe "when web connect" do
19
-
20
- describe "is allowed", :net_connect => true do
21
- before(:each) do
22
- WebMock.allow_net_connect!
23
- end
24
-
25
- it "should make a real web request if request is not stubbed" do
26
- http_request(:get, webmock_server_url).status.should == "200"
27
- end
28
-
29
- it "should make a real https request if request is not stubbed" do
30
- unless http_library == :httpclient
31
- http_request(:get, "https://www.paypal.com/uk/cgi-bin/webscr").
32
- body.should =~ /.*paypal.*/
33
- end
34
- end
35
-
36
- it "should return stubbed response if request was stubbed" do
37
- stub_http_request(:get, "www.example.com").to_return(:body => "abc")
38
- http_request(:get, "http://www.example.com/").body.should == "abc"
39
- end
40
- end
41
-
42
- describe "is not allowed" do
43
- before(:each) do
44
- WebMock.disable_net_connect!
45
- end
46
-
47
- it "should return stubbed response if request was stubbed" do
48
- stub_http_request(:get, "www.example.com").to_return(:body => "abc")
49
- http_request(:get, "http://www.example.com/").body.should == "abc"
50
- end
51
-
52
- it "should return stubbed response if request with path was stubbed" do
53
- stub_http_request(:get, "www.example.com/hello_world").to_return(:body => "abc")
54
- http_request(:get, "http://www.example.com/hello_world").body.should == "abc"
55
- end
56
-
57
- it "should raise exception if request was not stubbed" do
58
- lambda {
59
- http_request(:get, "http://www.example.com/")
60
- }.should raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/))
61
- end
62
- end
63
-
64
- describe "is not allowed with exception for localhost" do
65
- before(:each) do
66
- WebMock.disable_net_connect!(:allow_localhost => true)
67
- end
68
-
69
- it "should return stubbed response if request was stubbed" do
70
- stub_http_request(:get, "www.example.com").to_return(:body => "abc")
71
- http_request(:get, "http://www.example.com/").body.should == "abc"
72
- end
73
-
74
- it "should raise exception if request was not stubbed" do
75
- lambda {
76
- http_request(:get, "http://www.example.com/")
77
- }.should raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/))
78
- end
79
-
80
- it "should allow a real request to localhost" do
81
- lambda {
82
- http_request(:get, "http://localhost:12345/")
83
- }.should raise_error(connection_refused_exception_class)
84
- end
85
-
86
- it "should allow a real request to 127.0.0.1" do
87
- lambda {
88
- http_request(:get, "http://127.0.0.1:12345/")
89
- }.should raise_error(connection_refused_exception_class)
90
- end
91
-
92
- it "should allow a real request to 0.0.0.0" do
93
- lambda {
94
- http_request(:get, "http://0.0.0.0:12345/")
95
- }.should raise_error(connection_refused_exception_class)
96
- end
97
- end
98
-
99
- describe "is not allowed with exception for allowed domains" do
100
- let(:host_with_port){ WebMockServer.instance.host_with_port }
101
-
102
- before(:each) do
103
- WebMock.disable_net_connect!(:allow => ["www.example.org", host_with_port])
104
- end
105
-
106
- context "when the host is not allowed" do
107
- it "should return stubbed response if request was stubbed" do
108
- stub_http_request(:get, "www.example.com").to_return(:body => "abc")
109
- http_request(:get, "http://www.example.com/").body.should == "abc"
110
- end
111
-
112
- it "should raise exception if request was not stubbed" do
113
- lambda {
114
- http_request(:get, "http://www.example.com/")
115
- }.should raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/))
116
- end
117
- end
118
-
119
- context "when the host with port is not allowed" do
120
- it "should return stubbed response if request was stubbed" do
121
- stub_http_request(:get, "http://localhost:2345").to_return(:body => "abc")
122
- http_request(:get, "http://localhost:2345/").body.should == "abc"
123
- end
124
-
125
- it "should raise exception if request was not stubbed" do
126
- lambda {
127
- http_request(:get, "http://localhost:2345/")
128
- }.should raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://localhost:2345/))
129
- end
130
- end
131
-
132
- context "when the host is allowed" do
133
- it "should raise exception if request was not stubbed" do
134
- lambda {
135
- http_request(:get, "http://www.example.com/")
136
- }.should raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/))
137
- end
138
-
139
- it "should allow a real request to allowed host", :net_connect => true do
140
- http_request(:get, "http://www.example.org/").status.should == "302"
141
- end
142
- end
143
-
144
- context "when the host with port is allowed" do
145
- it "should allow a real request to allowed host", :net_connect => true do
146
- http_request(:get, "http://#{host_with_port}/").status.should == "200"
147
- end
148
- end
149
-
150
- context "when the host is allowed but not port" do
151
- it "should allow a real request to allowed host", :net_connect => true do
152
- lambda {
153
- http_request(:get, "http://localhost:123/")
154
- }.should raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://localhost:123/))
155
- end
156
- end
157
- end
158
- end
159
-
160
- describe "when matching requests" do
161
-
162
- describe "on uri" do
163
-
164
- it "should match the request by uri with non escaped params if request have escaped parameters" do
165
- stub_http_request(:get, "www.example.com/hello/?#{NOT_ESCAPED_PARAMS}").to_return(:body => "abc")
166
- http_request(:get, "http://www.example.com/hello/?#{ESCAPED_PARAMS}").body.should == "abc"
167
- end
168
-
169
- it "should match the request by uri with escaped parameters even if request has non escaped params" do
170
- stub_http_request(:get, "www.example.com/hello/?#{ESCAPED_PARAMS}").to_return(:body => "abc")
171
- http_request(:get, "http://www.example.com/hello/?#{NOT_ESCAPED_PARAMS}").body.should == "abc"
172
- end
173
-
174
- it "should match the request by regexp matching non escaped params uri if request params are escaped" do
175
- stub_http_request(:get, /.*x=ab c.*/).to_return(:body => "abc")
176
- http_request(:get, "http://www.example.com/hello/?#{ESCAPED_PARAMS}").body.should == "abc"
177
- end
178
-
179
- end
180
-
181
- describe "on query params" do
182
-
183
- it "should match the request by query params declared as a hash" do
184
- stub_http_request(:get, "www.example.com").with(:query => {"a" => ["b", "c"]}).to_return(:body => "abc")
185
- http_request(:get, "http://www.example.com/?a[]=b&a[]=c").body.should == "abc"
186
- end
187
-
188
- it "should match the request by query declared as a string" do
189
- stub_http_request(:get, "www.example.com").with(:query => "a[]=b&a[]=c").to_return(:body => "abc")
190
- http_request(:get, "http://www.example.com/?a[]=b&a[]=c").body.should == "abc"
191
- end
192
-
193
- it "should match the request by query params declared both in uri and query option" do
194
- stub_http_request(:get, "www.example.com/?x=3").with(:query => {"a" => ["b", "c"]}).to_return(:body => "abc")
195
- http_request(:get, "http://www.example.com/?x=3&a[]=b&a[]=c").body.should == "abc"
196
- end
197
-
198
- end
199
-
200
- describe "on method" do
201
-
202
- it "should match the request by method if registered" do
203
- stub_http_request(:get, "www.example.com")
204
- http_request(:get, "http://www.example.com/").status.should == "200"
205
- end
206
-
207
- it "should not match requests if method is different" do
208
- stub_http_request(:get, "www.example.com")
209
- http_request(:get, "http://www.example.com/").status.should == "200"
210
- lambda {
211
- http_request(:delete, "http://www.example.com/")
212
- }.should raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: DELETE http://www.example.com/)
213
- )
214
- end
215
-
216
- end
217
-
218
- describe "on body" do
219
-
220
- it "should match requests if body is the same" do
221
- stub_http_request(:post, "www.example.com").with(:body => "abc")
222
- http_request(
223
- :post, "http://www.example.com/",
224
- :body => "abc").status.should == "200"
225
- end
226
-
227
- it "should match requests if body is not set in the stub" do
228
- stub_http_request(:post, "www.example.com")
229
- http_request(
230
- :post, "http://www.example.com/",
231
- :body => "abc").status.should == "200"
232
- end
233
-
234
- it "should not match requests if body is different" do
235
- stub_http_request(:post, "www.example.com").with(:body => "abc")
236
- lambda {
237
- http_request(:post, "http://www.example.com/", :body => "def")
238
- }.should raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: POST http://www.example.com/ with body 'def'))
239
- end
240
-
241
- describe "with regular expressions" do
242
-
243
- it "should match requests if body matches regexp" do
244
- stub_http_request(:post, "www.example.com").with(:body => /\d+abc$/)
245
- http_request(
246
- :post, "http://www.example.com/",
247
- :body => "123abc").status.should == "200"
248
- end
249
-
250
- it "should not match requests if body doesn't match regexp" do
251
- stub_http_request(:post, "www.example.com").with(:body => /^abc/)
252
- lambda {
253
- http_request(:post, "http://www.example.com/", :body => "xabc")
254
- }.should raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: POST http://www.example.com/ with body 'xabc'))
255
- end
256
-
257
- end
258
-
259
- describe "when body is declared as a hash" do
260
- before(:each) do
261
- stub_http_request(:post, "www.example.com").
262
- with(:body => {:a => '1', :b => 'five', 'c' => {'d' => ['e', 'f']} })
263
- end
264
-
265
- describe "for request with url encoded body" do
266
-
267
- it "should match request if hash matches body" do
268
- http_request(
269
- :post, "http://www.example.com/",
270
- :body => 'a=1&c[d][]=e&c[d][]=f&b=five').status.should == "200"
271
- end
272
-
273
- it "should match request if hash matches body in different order of params" do
274
- http_request(
275
- :post, "http://www.example.com/",
276
- :body => 'a=1&c[d][]=e&b=five&c[d][]=f').status.should == "200"
277
- end
278
-
279
- it "should not match if hash doesn't match url encoded body" do
280
- lambda {
281
- http_request(
282
- :post, "http://www.example.com/",
283
- :body => 'c[d][]=f&a=1&c[d][]=e').status.should == "200"
284
- }.should raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: POST http://www.example.com/ with body 'c\[d\]\[\]=f&a=1&c\[d\]\[\]=e'))
285
- end
286
-
287
- end
288
-
289
-
290
- describe "for request with json body and content type is set to json" do
291
-
292
- it "should match if hash matches body" do
293
- http_request(
294
- :post, "http://www.example.com/", :headers => {'Content-Type' => 'application/json'},
295
- :body => "{\"a\":\"1\",\"c\":{\"d\":[\"e\",\"f\"]},\"b\":\"five\"}").status.should == "200"
296
- end
297
-
298
- it "should match if hash matches body in different form" do
299
- http_request(
300
- :post, "http://www.example.com/", :headers => {'Content-Type' => 'application/json'},
301
- :body => "{\"a\":\"1\",\"b\":\"five\",\"c\":{\"d\":[\"e\",\"f\"]}}").status.should == "200"
302
- end
303
-
304
- it "should match if hash contains date string" do #Crack creates date object
305
- WebMock.reset!
306
- stub_http_request(:post, "www.example.com").
307
- with(:body => {"foo" => "2010-01-01"})
308
- http_request(
309
- :post, "http://www.example.com/", :headers => {'Content-Type' => 'application/json'},
310
- :body => "{\"foo\":\"2010-01-01\"}").status.should == "200"
311
- end
312
- end
313
-
314
- describe "for request with xml body and content type is set to xml" do
315
- before(:each) do
316
- WebMock.reset!
317
- stub_http_request(:post, "www.example.com").
318
- with(:body => { "opt" => {:a => '1', :b => 'five', 'c' => {'d' => ['e', 'f']} }})
319
- end
320
-
321
- it "should match if hash matches body" do
322
- http_request(
323
- :post, "http://www.example.com/", :headers => {'Content-Type' => 'application/xml'},
324
- :body => "<opt a=\"1\" b=\"five\">\n <c>\n <d>e</d>\n <d>f</d>\n </c>\n</opt>\n").status.should == "200"
325
- end
326
-
327
- it "should match if hash matches body in different form" do
328
- http_request(
329
- :post, "http://www.example.com/", :headers => {'Content-Type' => 'application/xml'},
330
- :body => "<opt b=\"five\" a=\"1\">\n <c>\n <d>e</d>\n <d>f</d>\n </c>\n</opt>\n").status.should == "200"
331
- end
332
-
333
- it "should match if hash contains date string" do #Crack creates date object
334
- WebMock.reset!
335
- stub_http_request(:post, "www.example.com").
336
- with(:body => {"opt" => {"foo" => "2010-01-01"}})
337
- http_request(
338
- :post, "http://www.example.com/", :headers => {'Content-Type' => 'application/xml'},
339
- :body => "<opt foo=\"2010-01-01\">\n</opt>\n").status.should == "200"
340
- end
341
-
342
- end
343
-
344
- end
345
-
346
- end
347
-
348
- describe "on headers" do
349
-
350
- it "should match requests if headers are the same" do
351
- stub_http_request(:get, "www.example.com").with(:headers => SAMPLE_HEADERS )
352
- http_request(
353
- :get, "http://www.example.com/",
354
- :headers => SAMPLE_HEADERS).status.should == "200"
355
- end
356
-
357
- it "should match requests if headers are the same and declared as array" do
358
- stub_http_request(:get, "www.example.com").with(:headers => {"a" => ["b"]} )
359
- http_request(
360
- :get, "http://www.example.com/",
361
- :headers => {"a" => "b"}).status.should == "200"
362
- end
363
-
364
- describe "when multiple headers with the same key are used" do
365
-
366
- it "should match requests if headers are the same" do
367
- stub_http_request(:get, "www.example.com").with(:headers => {"a" => ["b", "c"]} )
368
- http_request(
369
- :get, "http://www.example.com/",
370
- :headers => {"a" => ["b", "c"]}).status.should == "200"
371
- end
372
-
373
- it "should match requests if headers are the same but in different order" do
374
- stub_http_request(:get, "www.example.com").with(:headers => {"a" => ["b", "c"]} )
375
- http_request(
376
- :get, "http://www.example.com/",
377
- :headers => {"a" => ["c", "b"]}).status.should == "200"
378
- end
379
-
380
- it "should not match requests if headers are different" do
381
- stub_http_request(:get, "www.example.com").with(:headers => {"a" => ["b", "c"]})
382
-
383
- lambda {
384
- http_request(
385
- :get, "http://www.example.com/",
386
- :headers => {"a" => ["b", "d"]})
387
- }.should raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/ with headers))
388
- end
389
-
390
- end
391
-
392
- it "should match requests if request headers are not stubbed" do
393
- stub_http_request(:get, "www.example.com")
394
- http_request(
395
- :get, "http://www.example.com/",
396
- :headers => SAMPLE_HEADERS).status.should == "200"
397
- end
398
-
399
- it "should not match requests if headers are different" do
400
- stub_http_request(:get, "www.example.com").with(:headers => SAMPLE_HEADERS)
401
-
402
- lambda {
403
- http_request(
404
- :get, "http://www.example.com/",
405
- :headers => { 'Content-Length' => '9999'})
406
- }.should raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/ with headers))
407
- end
408
-
409
- it "should not match if accept header is different" do
410
- stub_http_request(:get, "www.example.com").
411
- with(:headers => { 'Accept' => 'application/json'})
412
- lambda {
413
- http_request(
414
- :get, "http://www.example.com/",
415
- :headers => { 'Accept' => 'application/xml'})
416
- }.should raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/ with headers))
417
- end
418
-
419
- describe "with regular expressions" do
420
-
421
- it "should match requests if header values match regular expression" do
422
- stub_http_request(:get, "www.example.com").with(:headers => { :some_header => /^MyAppName$/ })
423
- http_request(
424
- :get, "http://www.example.com/",
425
- :headers => { 'some-header' => 'MyAppName' }).status.should == "200"
426
- end
427
-
428
- it "should not match requests if headers values do not match regular expression" do
429
- stub_http_request(:get, "www.example.com").with(:headers => { :some_header => /^MyAppName$/ })
430
-
431
- lambda {
432
- http_request(
433
- :get, "http://www.example.com/",
434
- :headers => { 'some-header' => 'xMyAppName' })
435
- }.should raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/ with headers))
436
- end
437
-
438
- end
439
- end
440
-
441
- describe "with basic authentication" do
442
-
443
- it "should match if credentials are the same" do
444
- stub_http_request(:get, "user:pass@www.example.com")
445
- http_request(:get, "http://user:pass@www.example.com/").status.should == "200"
446
- end
447
-
448
- it "should not match if credentials are different" do
449
- stub_http_request(:get, "user:pass@www.example.com")
450
- lambda {
451
- http_request(:get, "http://user:pazz@www.example.com/").status.should == "200"
452
- }.should raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://user:pazz@www.example.com/))
453
- end
454
-
455
- it "should not match if credentials are stubbed but not provided in the request" do
456
- stub_http_request(:get, "user:pass@www.example.com")
457
- lambda {
458
- http_request(:get, "http://www.example.com/").status.should == "200"
459
- }.should raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/))
460
- end
461
-
462
- it "should not match if credentials are not stubbed but exist in the request" do
463
- stub_http_request(:get, "www.example.com")
464
- lambda {
465
- http_request(:get, "http://user:pazz@www.example.com/").status.should == "200"
466
- }.should raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://user:pazz@www.example.com/))
467
- end
468
-
469
- end
470
-
471
- describe "with block" do
472
-
473
- it "should match if block returns true" do
474
- stub_http_request(:get, "www.example.com").with { |request| true }
475
- http_request(:get, "http://www.example.com/").status.should == "200"
476
- end
477
-
478
- it "should not match if block returns false" do
479
- stub_http_request(:get, "www.example.com").with { |request| false }
480
- lambda {
481
- http_request(:get, "http://www.example.com/")
482
- }.should raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/))
483
- end
484
-
485
- it "should pass the request to the block" do
486
- stub_http_request(:post, "www.example.com").with { |request| request.body == "wadus" }
487
- http_request(
488
- :post, "http://www.example.com/",
489
- :body => "wadus").status.should == "200"
490
- lambda {
491
- http_request(:post, "http://www.example.com/", :body => "jander")
492
- }.should raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: POST http://www.example.com/ with body 'jander'))
493
- end
494
-
495
- end
496
-
497
- end
498
-
499
- describe "raising stubbed exceptions" do
500
-
501
- it "should raise exception if declared in a stubbed response" do
502
- stub_http_request(:get, "www.example.com").to_raise(MyException)
503
- lambda {
504
- http_request(:get, "http://www.example.com/")
505
- }.should raise_error(MyException, "Exception from WebMock")
506
- end
507
-
508
- it "should raise exception if declared in a stubbed response as exception instance" do
509
- stub_http_request(:get, "www.example.com").to_raise(MyException.new("hello world"))
510
- lambda {
511
- http_request(:get, "http://www.example.com/")
512
- }.should raise_error(MyException, "hello world")
513
- end
514
-
515
- it "should raise exception if declared in a stubbed response as exception instance" do
516
- stub_http_request(:get, "www.example.com").to_raise("hello world")
517
- lambda {
518
- http_request(:get, "http://www.example.com/")
519
- }.should raise_error("hello world")
520
- end
521
-
522
- it "should raise exception if declared in a stubbed response after returning declared response" do
523
- stub_http_request(:get, "www.example.com").to_return(:body => "abc").then.to_raise(MyException)
524
- http_request(:get, "http://www.example.com/").body.should == "abc"
525
- lambda {
526
- http_request(:get, "http://www.example.com/")
527
- }.should raise_error(MyException, "Exception from WebMock")
528
- end
529
-
530
- end
531
-
532
-
533
- describe "raising timeout errors" do
534
-
535
- it "should raise timeout exception if declared in a stubbed response" do
536
- stub_http_request(:get, "www.example.com").to_timeout
537
- lambda {
538
- http_request(:get, "http://www.example.com/")
539
- }.should raise_error(client_timeout_exception_class)
540
- end
541
-
542
- it "should raise exception if declared in a stubbed response after returning declared response" do
543
- stub_http_request(:get, "www.example.com").to_return(:body => "abc").then.to_timeout
544
- http_request(:get, "http://www.example.com/").body.should == "abc"
545
- lambda {
546
- http_request(:get, "http://www.example.com/")
547
- }.should raise_error(client_timeout_exception_class)
548
- end
549
-
550
- end
551
-
552
- describe "returning stubbed responses" do
553
-
554
- it "should return declared body" do
555
- stub_http_request(:get, "www.example.com").to_return(:body => "abc")
556
- http_request(:get, "http://www.example.com/").body.should == "abc"
557
- end
558
-
559
- it "should return declared headers" do
560
- stub_http_request(:get, "www.example.com").to_return(:headers => SAMPLE_HEADERS)
561
- response = http_request(:get, "http://www.example.com/")
562
- response.headers["Content-Length"].should == "8888"
563
- end
564
-
565
- it "should return declared headers when there are multiple headers with the same key" do
566
- stub_http_request(:get, "www.example.com").to_return(:headers => {"a" => ["b", "c"]})
567
- response = http_request(:get, "http://www.example.com/")
568
- response.headers["A"].should == "b, c"
569
- end
570
-
571
- it "should return declared status code" do
572
- stub_http_request(:get, "www.example.com").to_return(:status => 500)
573
- http_request(:get, "http://www.example.com/").status.should == "500"
574
- end
575
-
576
- it "should return declared status message" do
577
- stub_http_request(:get, "www.example.com").to_return(:status => [500, "Internal Server Error"])
578
- response = http_request(:get, "http://www.example.com/")
579
- # not supported by em-http-request, it always returns "unknown" for http_reason
580
- unless http_library == :em_http_request
581
- response.message.should == "Internal Server Error"
582
- end
583
- end
584
-
585
- it "should return default status code" do
586
- stub_http_request(:get, "www.example.com")
587
- http_request(:get, "http://www.example.com/").status.should == "200"
588
- end
589
-
590
- it "should return default empty message" do
591
- stub_http_request(:get, "www.example.com")
592
- response = http_request(:get, "http://www.example.com/")
593
- # not supported by em-http-request, it always returns "unknown" for http_reason
594
- unless http_library == :em_http_request
595
- response.message.should == ""
596
- end
597
- end
598
-
599
- it "should return body declared as IO" do
600
- stub_http_request(:get, "www.example.com").to_return(:body => File.new(__FILE__))
601
- http_request(:get, "http://www.example.com/").body.should == File.new(__FILE__).read
602
- end
603
-
604
- it "should return body declared as IO if requested many times" do
605
- stub_http_request(:get, "www.example.com").to_return(:body => File.new(__FILE__))
606
- 2.times do
607
- http_request(:get, "http://www.example.com/").body.should == File.new(__FILE__).read
608
- end
609
- end
610
-
611
- it "should close IO declared as response body after reading" do
612
- stub_http_request(:get, "www.example.com").to_return(:body => @file = File.new(__FILE__))
613
- @file.should be_closed
614
- end
615
-
616
- describe "dynamic response parts" do
617
-
618
- it "should return evaluated response body" do
619
- stub_http_request(:post, "www.example.com").to_return(:body => lambda { |request| request.body })
620
- http_request(:post, "http://www.example.com/", :body => "echo").body.should == "echo"
621
- end
622
-
623
- it "should return evaluated response headers" do
624
- stub_http_request(:post, "www.example.com").to_return(:headers => lambda { |request| request.headers })
625
- http_request(:post, "http://www.example.com/", :body => "abc", :headers => {'A' => 'B'}).headers['A'].should == 'B'
626
- end
627
-
628
- end
629
-
630
- describe "dynamic responses" do
631
-
632
- class Responder
633
- def call(request)
634
- {:body => request.body}
635
- end
636
- end
637
-
638
- it "should return evaluated response body" do
639
- stub_http_request(:post, "www.example.com").to_return(lambda { |request|
640
- {:body => request.body}
641
- })
642
- http_request(:post, "http://www.example.com/", :body => "echo").body.should == "echo"
643
- end
644
-
645
- it "should return evaluated response headers" do
646
- stub_http_request(:get, "www.example.com").to_return(lambda { |request|
647
- {:headers => request.headers}
648
- })
649
- http_request(:get, "http://www.example.com/", :headers => {'A' => 'B'}).headers['A'].should == 'B'
650
- end
651
-
652
- it "should create dynamic responses from blocks" do
653
- stub_http_request(:post, "www.example.com").to_return do |request|
654
- {:body => request.body}
655
- end
656
- http_request(:post, "http://www.example.com/", :body => "echo").body.should == "echo"
657
- end
658
-
659
- it "should create dynamic responses from objects responding to call" do
660
- stub_http_request(:post, "www.example.com").to_return(Responder.new)
661
- http_request(:post, "http://www.example.com/", :body => "echo").body.should == "echo"
662
- end
663
-
664
- end
665
-
666
-
667
- describe "replying raw responses from file" do
668
-
669
- before(:each) do
670
- @file = File.new(File.expand_path(File.dirname(__FILE__)) + "/example_curl_output.txt")
671
- stub_http_request(:get, "www.example.com").to_return(@file)
672
- @response = http_request(:get, "http://www.example.com/")
673
- end
674
-
675
- it "should return recorded headers" do
676
- @response.headers.should == {
677
- "Date"=>"Sat, 23 Jan 2010 01:01:05 GMT",
678
- "Content-Type"=>"text/html; charset=UTF-8",
679
- "Content-Length"=>"419",
680
- "Connection"=>"Keep-Alive",
681
- "Accept"=>"image/jpeg, image/png"
682
- }
683
- end
684
-
685
- it "should return recorded body" do
686
- @response.body.size.should == 419
687
- end
688
-
689
- it "should return recorded status" do
690
- @response.status.should == "202"
691
- end
692
-
693
- it "should return recorded status message" do
694
- # not supported by em-http-request, it always returns "unknown" for http_reason
695
- unless http_library == :em_http_request
696
- @response.message.should == "OK"
697
- end
698
- end
699
-
700
- it "should ensure file is closed" do
701
- @file.should be_closed
702
- end
703
-
704
- end
705
-
706
- describe "replying responses raw responses from string" do
707
-
708
- before(:each) do
709
- @input = File.new(File.expand_path(File.dirname(__FILE__)) + "/example_curl_output.txt").read
710
- stub_http_request(:get, "www.example.com").to_return(@input)
711
- @response = http_request(:get, "http://www.example.com/")
712
- end
713
-
714
- it "should return recorded headers" do
715
- @response.headers.should == {
716
- "Date"=>"Sat, 23 Jan 2010 01:01:05 GMT",
717
- "Content-Type"=>"text/html; charset=UTF-8",
718
- "Content-Length"=>"419",
719
- "Connection"=>"Keep-Alive",
720
- "Accept"=>"image/jpeg, image/png"
721
- }
722
- end
723
-
724
- it "should return recorded body" do
725
- @response.body.size.should == 419
726
- end
727
-
728
- it "should return recorded status" do
729
- @response.status.should == "202"
730
- end
731
-
732
- it "should return recorded status message" do
733
- # not supported by em-http-request, it always returns "unknown" for http_reason
734
- unless http_library == :em_http_request
735
- @response.message.should == "OK"
736
- end
737
- end
738
- end
739
-
740
- describe "replying raw responses evaluated dynamically" do
741
- before(:each) do
742
- @files = {
743
- "www.example.com" => File.new(File.expand_path(File.dirname(__FILE__)) + "/example_curl_output.txt")
744
- }
745
- end
746
-
747
- it "should return response from evaluated file" do
748
- stub_http_request(:get, "www.example.com").to_return(lambda {|request| @files[request.uri.host.to_s] })
749
- http_request(:get, "http://www.example.com/").body.size.should == 419
750
- end
751
-
752
- it "should return response from evaluated string" do
753
- stub_http_request(:get, "www.example.com").to_return(lambda {|request| @files[request.uri.host.to_s].read })
754
- http_request(:get, "http://www.example.com/").body.size.should == 419
755
- end
756
- end
757
-
758
- describe "rack responses" do
759
- before(:each) do
760
- stub_request(:any, "http://www.example.com/greet").to_rack(MyRackApp)
761
- end
762
-
763
- it "should return response returned by rack app" do
764
- http_request(:post, 'http://www.example.com/greet', :body => 'name=Jimmy').body.should == 'Good to meet you, Jimmy!'
765
- end
766
- end
767
-
768
- describe "sequences of responses" do
769
- it "should return responses one by one if declared in array" do
770
- stub_http_request(:get, "www.example.com").to_return([ {:body => "1"}, {:body => "2"}, {:body => "3"} ])
771
- http_request(:get, "http://www.example.com/").body.should == "1"
772
- http_request(:get, "http://www.example.com/").body.should == "2"
773
- http_request(:get, "http://www.example.com/").body.should == "3"
774
- end
775
-
776
- it "should repeat returning last declared response from a sequence after all responses were returned" do
777
- stub_http_request(:get, "www.example.com").to_return([ {:body => "1"}, {:body => "2"} ])
778
- http_request(:get, "http://www.example.com/").body.should == "1"
779
- http_request(:get, "http://www.example.com/").body.should == "2"
780
- http_request(:get, "http://www.example.com/").body.should == "2"
781
- end
782
-
783
- it "should return responses one by one if declared as comma separated params" do
784
- stub_http_request(:get, "www.example.com").to_return({:body => "1"}, {:body => "2"}, {:body => "3"})
785
- http_request(:get, "http://www.example.com/").body.should == "1"
786
- http_request(:get, "http://www.example.com/").body.should == "2"
787
- http_request(:get, "http://www.example.com/").body.should == "3"
788
- end
789
-
790
- it "should return responses one by one if declared with several to_return invokations" do
791
- stub_http_request(:get, "www.example.com").
792
- to_return({:body => "1"}).
793
- to_return({:body => "2"}).
794
- to_return({:body => "3"})
795
- http_request(:get, "http://www.example.com/").body.should == "1"
796
- http_request(:get, "http://www.example.com/").body.should == "2"
797
- http_request(:get, "http://www.example.com/").body.should == "3"
798
- end
799
-
800
- it "should return responses one by one if declared with to_return invocations separated with then syntactic sugar" do
801
- stub_http_request(:get, "www.example.com").to_return({:body => "1"}).then.
802
- to_return({:body => "2"}).then.to_return({:body => "3"})
803
- http_request(:get, "http://www.example.com/").body.should == "1"
804
- http_request(:get, "http://www.example.com/").body.should == "2"
805
- http_request(:get, "http://www.example.com/").body.should == "3"
806
- end
807
-
808
- end
809
-
810
- describe "repeating declared responses more than once" do
811
-
812
- it "should repeat one response declared number of times" do
813
- stub_http_request(:get, "www.example.com").
814
- to_return({:body => "1"}).times(2).
815
- to_return({:body => "2"})
816
- http_request(:get, "http://www.example.com/").body.should == "1"
817
- http_request(:get, "http://www.example.com/").body.should == "1"
818
- http_request(:get, "http://www.example.com/").body.should == "2"
819
- end
820
-
821
-
822
- it "should repeat sequence of response declared number of times" do
823
- stub_http_request(:get, "www.example.com").
824
- to_return({:body => "1"}, {:body => "2"}).times(2).
825
- to_return({:body => "3"})
826
- http_request(:get, "http://www.example.com/").body.should == "1"
827
- http_request(:get, "http://www.example.com/").body.should == "2"
828
- http_request(:get, "http://www.example.com/").body.should == "1"
829
- http_request(:get, "http://www.example.com/").body.should == "2"
830
- http_request(:get, "http://www.example.com/").body.should == "3"
831
- end
832
-
833
-
834
- it "should repeat infinitely last response even if number of declared times is lower" do
835
- stub_http_request(:get, "www.example.com").
836
- to_return({:body => "1"}).times(2)
837
- http_request(:get, "http://www.example.com/").body.should == "1"
838
- http_request(:get, "http://www.example.com/").body.should == "1"
839
- http_request(:get, "http://www.example.com/").body.should == "1"
840
- end
841
-
842
- it "should give error if times is declared without specifying response" do
843
- lambda {
844
- stub_http_request(:get, "www.example.com").times(3)
845
- }.should raise_error("Invalid WebMock stub declaration. times(N) can be declared only after response declaration.")
846
- end
847
-
848
- end
849
-
850
- describe "raising declared exceptions more than once" do
851
-
852
- it "should repeat raising exception declared number of times" do
853
- stub_http_request(:get, "www.example.com").
854
- to_raise(MyException).times(2).
855
- to_return({:body => "2"})
856
- lambda {
857
- http_request(:get, "http://www.example.com/")
858
- }.should raise_error(MyException, "Exception from WebMock")
859
- lambda {
860
- http_request(:get, "http://www.example.com/")
861
- }.should raise_error(MyException, "Exception from WebMock")
862
- http_request(:get, "http://www.example.com/").body.should == "2"
863
- end
864
-
865
- it "should repeat raising sequence of exceptions declared number of times" do
866
- stub_http_request(:get, "www.example.com").
867
- to_raise(MyException, ArgumentError).times(2).
868
- to_return({:body => "2"})
869
- lambda {
870
- http_request(:get, "http://www.example.com/")
871
- }.should raise_error(MyException, "Exception from WebMock")
872
- lambda {
873
- http_request(:get, "http://www.example.com/")
874
- }.should raise_error(ArgumentError)
875
- lambda {
876
- http_request(:get, "http://www.example.com/")
877
- }.should raise_error(MyException, "Exception from WebMock")
878
- lambda {
879
- http_request(:get, "http://www.example.com/")
880
- }.should raise_error(ArgumentError)
881
- http_request(:get, "http://www.example.com/").body.should == "2"
882
- end
883
- end
884
- end
885
-
886
- describe "precedence of stubs" do
887
-
888
- it "should use the last declared matching request stub" do
889
- stub_http_request(:get, "www.example.com").to_return(:body => "abc")
890
- stub_http_request(:get, "www.example.com").to_return(:body => "def")
891
- http_request(:get, "http://www.example.com/").body.should == "def"
892
- end
893
-
894
- it "should not be affected by the type of uri or request method" do
895
- stub_http_request(:get, "www.example.com").to_return(:body => "abc")
896
- stub_http_request(:any, /.*example.*/).to_return(:body => "def")
897
- http_request(:get, "http://www.example.com/").body.should == "def"
898
- end
899
-
900
- end
901
-
902
- describe "verification of request expectation" do
903
-
904
- describe "when net connect not allowed" do
905
-
906
- before(:each) do
907
- WebMock.disable_net_connect!
908
- stub_http_request(:any, "http://www.example.com")
909
- stub_http_request(:any, "https://www.example.com")
910
- end
911
-
912
- it "should pass if request was executed with the same uri and method" do
913
- lambda {
914
- http_request(:get, "http://www.example.com/")
915
- a_request(:get, "http://www.example.com").should have_been_made.once
916
- }.should_not raise_error
917
- end
918
-
919
- it "should accept verification as WebMock class method invocation" do
920
- lambda {
921
- http_request(:get, "http://www.example.com/")
922
- WebMock.request(:get, "http://www.example.com").should have_been_made.once
923
- }.should_not raise_error
924
- end
925
-
926
- it "should pass if request was not expected and not executed" do
927
- lambda {
928
- a_request(:get, "http://www.example.com").should_not have_been_made
929
- }.should_not raise_error
930
- end
931
-
932
- it "should fail if request was not expected but executed" do
933
- lambda {
934
- http_request(:get, "http://www.example.com/")
935
- a_request(:get, "http://www.example.com").should_not have_been_made
936
- }.should fail_with(%r(The request GET http://www.example.com/ was expected to execute 0 times but it executed 1 time))
937
- end
938
-
939
- it "should fail with message with executed requests listed" do
940
- lambda {
941
- http_request(:get, "http://www.example.com/")
942
- a_request(:get, "http://www.example.com").should_not have_been_made
943
- }.should fail_with(%r{The following requests were made:\n\nGET http://www.example.com/.+was made 1 time})
944
- end
945
-
946
- it "should fail if request was not executed" do
947
- lambda {
948
- a_request(:get, "http://www.example.com").should have_been_made
949
- }.should fail_with(%r(The request GET http://www.example.com/ was expected to execute 1 time but it executed 0 times))
950
- end
951
-
952
- it "should fail if request was executed to different uri" do
953
- lambda {
954
- http_request(:get, "http://www.example.com/")
955
- a_request(:get, "http://www.example.org").should have_been_made
956
- }.should fail_with(%r(The request GET http://www.example.org/ was expected to execute 1 time but it executed 0 times))
957
- end
958
-
959
- it "should fail if request was executed with different method" do
960
- lambda {
961
- http_request(:post, "http://www.example.com/", :body => "abc")
962
- a_request(:get, "http://www.example.com").should have_been_made
963
- }.should fail_with(%r(The request GET http://www.example.com/ was expected to execute 1 time but it executed 0 times))
964
- end
965
-
966
- it "should pass if request was executed with different form of uri" do
967
- lambda {
968
- http_request(:get, "http://www.example.com/")
969
- a_request(:get, "www.example.com").should have_been_made
970
- }.should_not raise_error
971
- end
972
-
973
- it "should pass if request was executed with different form of uri without port " do
974
- lambda {
975
- http_request(:get, "http://www.example.com/")
976
- a_request(:get, "www.example.com:80").should have_been_made
977
- }.should_not raise_error
978
- end
979
-
980
- it "should pass if request was executed with different form of uri with port" do
981
- lambda {
982
- http_request(:get, "http://www.example.com/")
983
- a_request(:get, "www.example.com:80").should have_been_made
984
- }.should_not raise_error
985
- end
986
-
987
- it "should fail if request was executed with different port" do
988
- lambda {
989
- http_request(:get, "http://www.example.com:80/")
990
- a_request(:get, "www.example.com:90").should have_been_made
991
- }.should fail_with(%r(The request GET http://www.example.com:90/ was expected to execute 1 time but it executed 0 times))
992
- end
993
-
994
- it "should pass if request was executed with different form of uri with https port" do
995
- lambda {
996
- http_request(:get, "https://www.example.com/")
997
- a_request(:get, "https://www.example.com:443/").should have_been_made
998
- }.should_not raise_error
999
- end
1000
-
1001
- describe "when matching requests with escaped uris" do
1002
-
1003
- before(:each) do
1004
- WebMock.disable_net_connect!
1005
- stub_http_request(:any, "http://www.example.com/?#{NOT_ESCAPED_PARAMS}")
1006
- end
1007
-
1008
- it "should pass if request was executed with escaped params" do
1009
- lambda {
1010
- http_request(:get, "http://www.example.com/?#{ESCAPED_PARAMS}")
1011
- a_request(:get, "http://www.example.com/?#{NOT_ESCAPED_PARAMS}").should have_been_made
1012
- }.should_not raise_error
1013
- end
1014
-
1015
- it "should pass if request was executed with non escaped params but escaped expected" do
1016
- lambda {
1017
- http_request(:get, "http://www.example.com/?#{NOT_ESCAPED_PARAMS}")
1018
- a_request(:get, "http://www.example.com/?#{ESCAPED_PARAMS}").should have_been_made
1019
- }.should_not raise_error
1020
- end
1021
-
1022
- it "should pass if request was executed with escaped params but uri matichg regexp expected" do
1023
- lambda {
1024
- http_request(:get, "http://www.example.com/?#{ESCAPED_PARAMS}")
1025
- a_request(:get, /.*example.*/).should have_been_made
1026
- }.should_not raise_error
1027
- end
1028
-
1029
- end
1030
-
1031
- describe "when matching requests with query params" do
1032
- before(:each) do
1033
- stub_http_request(:any, /.*example.*/)
1034
- end
1035
-
1036
- it "should pass if the request was executed with query params declared in a hash in query option" do
1037
- lambda {
1038
- http_request(:get, "http://www.example.com/?a[]=b&a[]=c")
1039
- a_request(:get, "www.example.com").with(:query => {"a" => ["b", "c"]}).should have_been_made
1040
- }.should_not raise_error
1041
- end
1042
-
1043
- it "should pass if the request was executed with query params declared as string in query option" do
1044
- lambda {
1045
- http_request(:get, "http://www.example.com/?a[]=b&a[]=c")
1046
- a_request(:get, "www.example.com").with(:query => "a[]=b&a[]=c").should have_been_made
1047
- }.should_not raise_error
1048
- end
1049
-
1050
- it "should pass if the request was executed with query params both in uri and in query option" do
1051
- lambda {
1052
- http_request(:get, "http://www.example.com/?x=3&a[]=b&a[]=c")
1053
- a_request(:get, "www.example.com/?x=3").with(:query => {"a" => ["b", "c"]}).should have_been_made
1054
- }.should_not raise_error
1055
- end
1056
-
1057
- end
1058
-
1059
- it "should fail if requested more times than expected" do
1060
- lambda {
1061
- http_request(:get, "http://www.example.com/")
1062
- http_request(:get, "http://www.example.com/")
1063
- a_request(:get, "http://www.example.com").should have_been_made
1064
- }.should fail_with(%r(The request GET http://www.example.com/ was expected to execute 1 time but it executed 2 times))
1065
- end
1066
-
1067
- it "should fail if requested less times than expected" do
1068
- lambda {
1069
- http_request(:get, "http://www.example.com/")
1070
- a_request(:get, "http://www.example.com").should have_been_made.twice
1071
- }.should fail_with(%r(The request GET http://www.example.com/ was expected to execute 2 times but it executed 1 time))
1072
- end
1073
-
1074
- it "should fail if requested less times than expected when 3 times expected" do
1075
- lambda {
1076
- http_request(:get, "http://www.example.com/")
1077
- a_request(:get, "http://www.example.com").should have_been_made.times(3)
1078
- }.should fail_with(%r(The request GET http://www.example.com/ was expected to execute 3 times but it executed 1 time))
1079
- end
1080
-
1081
- it "should succeed if request was executed with the same body" do
1082
- lambda {
1083
- http_request(:post, "http://www.example.com/", :body => "abc")
1084
- a_request(:post, "www.example.com").with(:body => "abc").should have_been_made
1085
- }.should_not raise_error
1086
- end
1087
-
1088
- it "should fail if request was executed with different body" do
1089
- lambda {
1090
- http_request(:get, "http://www.example.com/", :body => "abc")
1091
- a_request(:get, "www.example.com").
1092
- with(:body => "def").should have_been_made
1093
- }.should fail_with(%r(The request GET http://www.example.com/ with body "def" was expected to execute 1 time but it executed 0 times))
1094
- end
1095
-
1096
- describe "when expected body is declared as regexp" do
1097
-
1098
- it "should succeed if request was executed with the same body" do
1099
- lambda {
1100
- http_request(:post, "http://www.example.com/", :body => "abc")
1101
- a_request(:post, "www.example.com").with(:body => /^abc$/).should have_been_made
1102
- }.should_not raise_error
1103
- end
1104
-
1105
- it "should fail if request was executed with different body" do
1106
- lambda {
1107
- http_request(:get, "http://www.example.com/", :body => "abc")
1108
- a_request(:get, "www.example.com").
1109
- with(:body => /^xabc/).should have_been_made
1110
- }.should fail_with(%r(The request GET http://www.example.com/ with body /\^xabc/ was expected to execute 1 time but it executed 0 times))
1111
- end
1112
-
1113
- end
1114
-
1115
- describe "when expected body is declared as a hash" do
1116
- let(:body_hash) { {:a => '1', :b => 'five', 'c' => {'d' => ['e', 'f']}} }
1117
- let(:fail_message) {%r(The request POST http://www.example.com/ with body \{"a"=>"1", "b"=>"five", "c"=>\{"d"=>\["e", "f"\]\}\} was expected to execute 1 time but it executed 0 times)}
1118
-
1119
- describe "when request is executed with url encoded body matching hash" do
1120
-
1121
- it "should succeed" do
1122
- lambda {
1123
- http_request(:post, "http://www.example.com/", :body => 'a=1&c[d][]=e&c[d][]=f&b=five')
1124
- a_request(:post, "www.example.com").with(:body => body_hash).should have_been_made
1125
- }.should_not raise_error
1126
- end
1127
-
1128
- it "should succeed if url encoded params have different order" do
1129
- lambda {
1130
- http_request(:post, "http://www.example.com/", :body => 'a=1&c[d][]=e&b=five&c[d][]=f')
1131
- a_request(:post, "www.example.com").with(:body => body_hash).should have_been_made
1132
- }.should_not raise_error
1133
- end
1134
-
1135
- it "should fail if request is executed with url encoded body not matching hash" do
1136
- lambda {
1137
- http_request(:post, "http://www.example.com/", :body => 'c[d][]=f&a=1&c[d][]=e')
1138
- a_request(:post, "www.example.com").with(:body => body_hash).should have_been_made
1139
- }.should fail_with(fail_message)
1140
- end
1141
-
1142
- end
1143
-
1144
- describe "when request is executed with json body matching hash and content type is set to json" do
1145
-
1146
- it "should succeed" do
1147
- lambda {
1148
- http_request(:post, "http://www.example.com/", :headers => {'Content-Type' => 'application/json'},
1149
- :body => "{\"a\":\"1\",\"c\":{\"d\":[\"e\",\"f\"]},\"b\":\"five\"}")
1150
- a_request(:post, "www.example.com").with(:body => body_hash).should have_been_made
1151
- }.should_not raise_error
1152
- end
1153
-
1154
- it "should succeed if json body is in different form" do
1155
- lambda {
1156
- http_request(:post, "http://www.example.com/", :headers => {'Content-Type' => 'application/json'},
1157
- :body => "{\"a\":\"1\",\"b\":\"five\",\"c\":{\"d\":[\"e\",\"f\"]}}")
1158
- a_request(:post, "www.example.com").with(:body => body_hash).should have_been_made
1159
- }.should_not raise_error
1160
- end
1161
-
1162
- it "should succeed if json body contains date string" do
1163
- lambda {
1164
- http_request(:post, "http://www.example.com/", :headers => {'Content-Type' => 'application/json'},
1165
- :body => "{\"foo\":\"2010-01-01\"}")
1166
- a_request(:post, "www.example.com").with(:body => {"foo" => "2010-01-01"}).should have_been_made
1167
- }.should_not raise_error
1168
- end
1169
- end
1170
-
1171
-
1172
- describe "when request is executed with xml body matching hash and content type is set to xml" do
1173
- let(:body_hash) { { "opt" => {:a => "1", :b => 'five', 'c' => {'d' => ['e', 'f']}} }}
1174
-
1175
- it "should succeed" do
1176
- lambda {
1177
- http_request(:post, "http://www.example.com/", :headers => {'Content-Type' => 'application/xml'},
1178
- :body => "<opt a=\"1\" b=\"five\">\n <c>\n <d>e</d>\n <d>f</d>\n </c>\n</opt>\n")
1179
- a_request(:post, "www.example.com").with(:body => body_hash).should have_been_made
1180
- }.should_not raise_error
1181
- end
1182
-
1183
- it "should succeed if xml body is in different form" do
1184
- lambda {
1185
- http_request(:post, "http://www.example.com/", :headers => {'Content-Type' => 'application/xml'},
1186
- :body => "<opt b=\"five\" a=\"1\">\n <c>\n <d>e</d>\n <d>f</d>\n </c>\n</opt>\n")
1187
- a_request(:post, "www.example.com").with(:body => body_hash).should have_been_made
1188
- }.should_not raise_error
1189
- end
1190
-
1191
- it "should succeed if xml body contains date string" do
1192
- lambda {
1193
- http_request(:post, "http://www.example.com/", :headers => {'Content-Type' => 'application/xml'},
1194
- :body => "<opt foo=\"2010-01-01\">\n</opt>\n")
1195
- a_request(:post, "www.example.com").with(:body => {"opt" => {"foo" => "2010-01-01"}}).should have_been_made
1196
- }.should_not raise_error
1197
- end
1198
-
1199
- end
1200
-
1201
- end
1202
-
1203
- it "should succeed if request was executed with the same headers" do
1204
- lambda {
1205
- http_request(:get, "http://www.example.com/", :headers => SAMPLE_HEADERS)
1206
- a_request(:get, "www.example.com").
1207
- with(:headers => SAMPLE_HEADERS).should have_been_made
1208
- }.should_not raise_error
1209
- end
1210
-
1211
- it "should succeed if request was executed with the same headers with value declared as array" do
1212
- lambda {
1213
- http_request(:get, "http://www.example.com/", :headers => {"a" => "b"})
1214
- a_request(:get, "www.example.com").
1215
- with(:headers => {"a" => ["b"]}).should have_been_made
1216
- }.should_not raise_error
1217
- end
1218
-
1219
- describe "when multiple headers with the same key are passed" do
1220
-
1221
- it "should succeed if request was executed with the same headers" do
1222
- lambda {
1223
- http_request(:get, "http://www.example.com/", :headers => {"a" => ["b", "c"]})
1224
- a_request(:get, "www.example.com").
1225
- with(:headers => {"a" => ["b", "c"]}).should have_been_made
1226
- }.should_not raise_error
1227
- end
1228
-
1229
- it "should succeed if request was executed with the same headers but different order" do
1230
- lambda {
1231
- http_request(:get, "http://www.example.com/", :headers => {"a" => ["b", "c"]})
1232
- a_request(:get, "www.example.com").
1233
- with(:headers => {"a" => ["c", "b"]}).should have_been_made
1234
- }.should_not raise_error
1235
- end
1236
-
1237
- it "should fail if request was executed with different headers" do
1238
- lambda {
1239
- http_request(:get, "http://www.example.com/", :headers => {"a" => ["b", "c"]})
1240
- a_request(:get, "www.example.com").
1241
- with(:headers => {"a" => ["b", "d"]}).should have_been_made
1242
- }.should 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))
1243
- end
1244
-
1245
- end
1246
-
1247
- it "should fail if request was executed with different headers" do
1248
- lambda {
1249
- http_request(:get, "http://www.example.com/", :headers => SAMPLE_HEADERS)
1250
- a_request(:get, "www.example.com").
1251
- with(:headers => { 'Content-Length' => '9999'}).should have_been_made
1252
- }.should 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))
1253
- end
1254
-
1255
- it "should fail if request was executed with less headers" do
1256
- lambda {
1257
- http_request(:get, "http://www.example.com/", :headers => {'A' => 'a'})
1258
- a_request(:get, "www.example.com").
1259
- with(:headers => {'A' => 'a', 'B' => 'b'}).should have_been_made
1260
- }.should 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))
1261
- end
1262
-
1263
- it "should succeed if request was executed with more headers" do
1264
- lambda {
1265
- http_request(:get, "http://www.example.com/",
1266
- :headers => {'A' => 'a', 'B' => 'b'}
1267
- )
1268
- a_request(:get, "www.example.com").
1269
- with(:headers => {'A' => 'a'}).should have_been_made
1270
- }.should_not raise_error
1271
- end
1272
-
1273
- it "should succeed if request was executed with body and headers but they were not specified in expectantion" do
1274
- lambda {
1275
- http_request(:get, "http://www.example.com/",
1276
- :body => "abc",
1277
- :headers => SAMPLE_HEADERS
1278
- )
1279
- a_request(:get, "www.example.com").should have_been_made
1280
- }.should_not raise_error
1281
- end
1282
-
1283
- it "should succeed if request was executed with headers matching regular expressions" do
1284
- lambda {
1285
- http_request(:get, "http://www.example.com/", :headers => { 'some-header' => 'MyAppName' })
1286
- a_request(:get, "www.example.com").
1287
- with(:headers => { :some_header => /^MyAppName$/ }).should have_been_made
1288
- }.should_not raise_error
1289
- end
1290
-
1291
- it "should fail if request was executed with headers not matching regular expression" do
1292
- lambda {
1293
- http_request(:get, "http://www.example.com/", :headers => { 'some-header' => 'xMyAppName' })
1294
- a_request(:get, "www.example.com").
1295
- with(:headers => { :some_header => /^MyAppName$/ }).should have_been_made
1296
- }.should 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))
1297
- end
1298
-
1299
- it "should suceed if request was executed and block evaluated to true" do
1300
- lambda {
1301
- http_request(:post, "http://www.example.com/", :body => "wadus")
1302
- a_request(:post, "www.example.com").with { |req| req.body == "wadus" }.should have_been_made
1303
- }.should_not raise_error
1304
- end
1305
-
1306
- it "should fail if request was executed and block evaluated to false" do
1307
- lambda {
1308
- http_request(:post, "http://www.example.com/", :body => "abc")
1309
- a_request(:post, "www.example.com").with { |req| req.body == "wadus" }.should have_been_made
1310
- }.should fail_with(%r(The request POST http://www.example.com/ with given block was expected to execute 1 time but it executed 0 times))
1311
- end
1312
-
1313
- it "should fail if request was not expected but it executed and block matched request" do
1314
- lambda {
1315
- http_request(:post, "http://www.example.com/", :body => "wadus")
1316
- a_request(:post, "www.example.com").with { |req| req.body == "wadus" }.should_not have_been_made
1317
- }.should fail_with(%r(The request POST http://www.example.com/ with given block was expected to execute 0 times but it executed 1 time))
1318
- end
1319
-
1320
- describe "with authentication" do
1321
- before(:each) do
1322
- stub_http_request(:any, "http://user:pass@www.example.com")
1323
- stub_http_request(:any, "http://user:pazz@www.example.com")
1324
- end
1325
-
1326
- it "should succeed if succeed if request was executed with expected credentials" do
1327
- lambda {
1328
- http_request(:get, "http://user:pass@www.example.com/")
1329
- a_request(:get, "http://user:pass@www.example.com").should have_been_made.once
1330
- }.should_not raise_error
1331
- end
1332
-
1333
- it "should fail if request was executed with different credentials than expected" do
1334
- lambda {
1335
- http_request(:get, "http://user:pass@www.example.com/")
1336
- a_request(:get, "http://user:pazz@www.example.com").should have_been_made.once
1337
- }.should fail_with(%r(The request GET http://user:pazz@www.example.com/ was expected to execute 1 time but it executed 0 times))
1338
- end
1339
-
1340
- it "should fail if request was executed without credentials but credentials were expected" do
1341
- lambda {
1342
- http_request(:get, "http://www.example.com/")
1343
- a_request(:get, "http://user:pass@www.example.com").should have_been_made.once
1344
- }.should fail_with(%r(The request GET http://user:pass@www.example.com/ was expected to execute 1 time but it executed 0 times))
1345
- end
1346
-
1347
- it "should fail if request was executed with credentials but expected without" do
1348
- lambda {
1349
- http_request(:get, "http://user:pass@www.example.com/")
1350
- a_request(:get, "http://www.example.com").should have_been_made.once
1351
- }.should fail_with(%r(The request GET http://www.example.com/ was expected to execute 1 time but it executed 0 times))
1352
- end
1353
-
1354
- it "should be order insensitive" do
1355
- stub_request(:post, "http://www.example.com")
1356
- http_request(:post, "http://www.example.com/", :body => "def")
1357
- http_request(:post, "http://www.example.com/", :body => "abc")
1358
- WebMock.should have_requested(:post, "www.example.com").with(:body => "abc")
1359
- WebMock.should have_requested(:post, "www.example.com").with(:body => "def")
1360
- end
1361
-
1362
- end
1363
-
1364
- describe "using webmock matcher" do
1365
-
1366
- it "should verify expected requests occured" do
1367
- lambda {
1368
- http_request(:get, "http://www.example.com/")
1369
- WebMock.should have_requested(:get, "http://www.example.com").once
1370
- }.should_not raise_error
1371
- end
1372
-
1373
- it "should verify expected requests occured" do
1374
- lambda {
1375
- http_request(:post, "http://www.example.com/", :body => "abc", :headers => {'A' => 'a'})
1376
- WebMock.should have_requested(:post, "http://www.example.com").with(:body => "abc", :headers => {'A' => 'a'}).once
1377
- }.should_not raise_error
1378
- end
1379
-
1380
- it "should verify that non expected requests didn't occur" do
1381
- lambda {
1382
- http_request(:get, "http://www.example.com/")
1383
- WebMock.should_not have_requested(:get, "http://www.example.com")
1384
- }.should fail_with(%r(The request GET http://www.example.com/ was expected to execute 0 times but it executed 1 time))
1385
- end
1386
-
1387
- it "should succeed if request was executed and block evaluated to true" do
1388
- lambda {
1389
- http_request(:post, "http://www.example.com/", :body => "wadus")
1390
- WebMock.should have_requested(:post, "www.example.com").with { |req| req.body == "wadus" }
1391
- }.should_not raise_error
1392
- end
1393
-
1394
- it "should fail if request was executed and block evaluated to false" do
1395
- lambda {
1396
- http_request(:post, "http://www.example.com/", :body => "abc")
1397
- WebMock.should have_requested(:post, "www.example.com").with { |req| req.body == "wadus" }
1398
- }.should fail_with(%r(The request POST http://www.example.com/ with given block was expected to execute 1 time but it executed 0 times))
1399
- end
1400
-
1401
- it "should fail if request was not expected but executed and block matched request" do
1402
- lambda {
1403
- http_request(:post, "http://www.example.com/", :body => "wadus")
1404
- WebMock.should_not have_requested(:post, "www.example.com").with { |req| req.body == "wadus" }
1405
- }.should fail_with(%r(The request POST http://www.example.com/ with given block was expected to execute 0 times but it executed 1 time))
1406
- end
1407
- end
1408
-
1409
-
1410
-
1411
- describe "using assert_requested" do
1412
-
1413
- it "should verify expected requests occured" do
1414
- lambda {
1415
- http_request(:get, "http://www.example.com/")
1416
- assert_requested(:get, "http://www.example.com", :times => 1)
1417
- assert_requested(:get, "http://www.example.com")
1418
- }.should_not raise_error
1419
- end
1420
-
1421
- it "should verify expected requests occured" do
1422
- lambda {
1423
- http_request(:post, "http://www.example.com/", :body => "abc", :headers => {'A' => 'a'})
1424
- assert_requested(:post, "http://www.example.com", :body => "abc", :headers => {'A' => 'a'})
1425
- }.should_not raise_error
1426
- end
1427
-
1428
- it "should verify that non expected requests didn't occur" do
1429
- lambda {
1430
- http_request(:get, "http://www.example.com/")
1431
- assert_not_requested(:get, "http://www.example.com")
1432
- }.should fail_with(%r(The request GET http://www.example.com/ was expected to execute 0 times but it executed 1 time))
1433
- end
1434
-
1435
- it "should verify if non expected request executed and block evaluated to true" do
1436
- lambda {
1437
- http_request(:post, "http://www.example.com/", :body => "wadus")
1438
- assert_not_requested(:post, "www.example.com") { |req| req.body == "wadus" }
1439
- }.should fail_with(%r(The request POST http://www.example.com/ with given block was expected to execute 0 times but it executed 1 time))
1440
- end
1441
-
1442
- it "should verify if request was executed and block evaluated to true" do
1443
- lambda {
1444
- http_request(:post, "http://www.example.com/", :body => "wadus")
1445
- assert_requested(:post, "www.example.com") { |req| req.body == "wadus" }
1446
- }.should_not raise_error
1447
- end
1448
-
1449
- it "should verify if request was executed and block evaluated to false" do
1450
- lambda {
1451
- http_request(:post, "http://www.example.com/", :body => "abc")
1452
- assert_requested(:post, "www.example.com") { |req| req.body == "wadus" }
1453
- }.should fail_with(%r(The request POST http://www.example.com/ with given block was expected to execute 1 time but it executed 0 times))
1454
- end
1455
- end
1456
- end
1457
-
1458
-
1459
- describe "using matchers on the RequestStub" do
1460
-
1461
- it "should verify expected requests occured" do
1462
- stub = stub_request(:get, "http://www.example.com/")
1463
- http_request(:get, "http://www.example.com/")
1464
- stub.should have_been_requested.once
1465
- end
1466
-
1467
- it "should verify subsequent requests" do
1468
- stub = stub_request(:get, "http://www.example.com/")
1469
- http_request(:get, "http://www.example.com/")
1470
- stub.should have_been_requested.once
1471
- http_request(:get, "http://www.example.com/")
1472
- stub.should have_been_requested.twice
1473
- end
1474
-
1475
- it "should verify expected requests occured" do
1476
- stub = stub_request(:post, "http://www.example.com").with(:body => "abc", :headers => {'A' => 'a'})
1477
- http_request(:post, "http://www.example.com/", :body => "abc", :headers => {'A' => 'a'})
1478
- stub.should have_been_requested.once
1479
- end
1480
-
1481
- it "should verify that non expected requests didn't occur" do
1482
- lambda {
1483
- stub = stub_request(:get, "http://www.example.com")
1484
- http_request(:get, "http://www.example.com/")
1485
- stub.should_not have_been_requested
1486
- }.should fail_with(%r(The request GET http://www.example.com/ was expected to execute 0 times but it executed 1 time))
1487
- end
1488
-
1489
- it "should verify if non expected request executed and block evaluated to true" do
1490
- lambda {
1491
- stub = stub_request(:post, "www.example.com").with { |req| req.body == "wadus" }
1492
- http_request(:post, "http://www.example.com/", :body => "wadus")
1493
- stub.should_not have_been_requested
1494
- }.should fail_with(%r(The request POST http://www.example.com/ with given block was expected to execute 0 times but it executed 1 time))
1495
- end
1496
-
1497
- it "should verify if request was executed and block evaluated to true" do
1498
- stub = stub_request(:post, "www.example.com").with { |req| req.body == "wadus" }
1499
- http_request(:post, "http://www.example.com/", :body => "wadus")
1500
- stub.should have_been_requested
1501
- end
1502
-
1503
- it "should verify if request was executed and block evaluated to false" do
1504
- lambda {
1505
- stub_request(:any, /.+/) #stub any request
1506
- stub = stub_request(:post, "www.example.com").with { |req| req.body == "wadus" }
1507
- http_request(:post, "http://www.example.com/", :body => "abc")
1508
- stub.should have_been_requested
1509
- }.should fail_with(%r(The request POST http://www.example.com/ with given block was expected to execute 1 time but it executed 0 times))
1510
- end
1511
- end
1512
-
1513
- describe "when net connect allowed", :net_connect => true do
1514
- before(:each) do
1515
- WebMock.allow_net_connect!
1516
- end
1517
-
1518
- it "should verify expected requests occured" do
1519
- lambda {
1520
- http_request(:get, "http://www.example.com/")
1521
- a_request(:get, "http://www.example.com").should have_been_made
1522
- }.should_not raise_error
1523
- end
1524
-
1525
- it "should verify that non expected requests didn't occur" do
1526
- lambda {
1527
- http_request(:get, "http://www.example.com/")
1528
- a_request(:get, "http://www.example.com").should_not have_been_made
1529
- }.should fail_with(%r(The request GET http://www.example.com/ was expected to execute 0 times but it executed 1 time))
1530
- end
1531
- end
1532
-
1533
- end
1534
-
1535
-
1536
- describe "callbacks" do
1537
-
1538
- describe "after_request" do
1539
- before(:each) do
1540
- WebMock.reset_callbacks
1541
- stub_request(:get, "http://www.example.com")
1542
- end
1543
-
1544
- it "should not invoke callback unless request is made" do
1545
- WebMock.after_request {
1546
- @called = true
1547
- }
1548
- @called.should == nil
1549
- end
1550
-
1551
- it "should invoke a callback after request is made" do
1552
- WebMock.after_request {
1553
- @called = true
1554
- }
1555
- http_request(:get, "http://www.example.com/")
1556
- @called.should == true
1557
- end
1558
-
1559
- it "should not invoke a callback if specific http library should be ignored" do
1560
- WebMock.after_request(:except => [http_library()]) {
1561
- @called = true
1562
- }
1563
- http_request(:get, "http://www.example.com/")
1564
- @called.should == nil
1565
- end
1566
-
1567
- it "should invoke a callback even if other http libraries should be ignored" do
1568
- WebMock.after_request(:except => [:other_lib]) {
1569
- @called = true
1570
- }
1571
- http_request(:get, "http://www.example.com/")
1572
- @called.should == true
1573
- end
1574
-
1575
- it "should pass request signature to the callback" do
1576
- WebMock.after_request(:except => [:other_lib]) do |request_signature, _|
1577
- @request_signature = request_signature
1578
- end
1579
- http_request(:get, "http://www.example.com/")
1580
- @request_signature.uri.to_s.should == "http://www.example.com:80/"
1581
- end
1582
-
1583
- describe "passing response to callback" do
1584
-
1585
- describe "for stubbed requests" do
1586
- before(:each) do
1587
- stub_request(:get, "http://www.example.com").
1588
- to_return(
1589
- :status => ["200", "hello"],
1590
- :headers => {'Content-Length' => '666', 'Hello' => 'World'},
1591
- :body => "foo bar"
1592
- )
1593
- WebMock.after_request(:except => [:other_lib]) do |_, response|
1594
- @response = response
1595
- end
1596
- http_request(:get, "http://www.example.com/")
1597
- end
1598
-
1599
- it "should pass response with status and message" do
1600
- @response.status.should == ["200", "hello"]
1601
- end
1602
-
1603
- it "should pass response with headers" do
1604
- @response.headers.should == {
1605
- 'Content-Length' => '666',
1606
- 'Hello' => 'World'
1607
- }
1608
- end
1609
-
1610
- it "should pass response with body" do
1611
- @response.body.should == "foo bar"
1612
- end
1613
-
1614
- end
1615
-
1616
- describe "for real requests", :net_connect => true do
1617
- before(:each) do
1618
- WebMock.reset!
1619
- WebMock.allow_net_connect!
1620
- WebMock.after_request(:except => [:other_lib]) do |_, response|
1621
- @response = response
1622
- end
1623
- http_request(:get, "http://www.example.com/")
1624
- end
1625
-
1626
- it "should pass response with status and message" do
1627
- # not supported by em-http-request, it always returns "unknown" for http_reason
1628
- unless http_library == :em_http_request
1629
- @response.status[0].should == 302
1630
- @response.status[1].should == "Found"
1631
- end
1632
- end
1633
-
1634
- it "should pass response with headers" do
1635
- @response.headers["Content-Length"].should == "0"
1636
- end
1637
-
1638
- it "should pass response with body" do
1639
- @response.body.size.should == 0
1640
- end
1641
-
1642
- end
1643
-
1644
- end
1645
-
1646
- it "should invoke multiple callbacks in order of their declarations" do
1647
- WebMock.after_request { @called = 1 }
1648
- WebMock.after_request { @called += 1 }
1649
- http_request(:get, "http://www.example.com/")
1650
- @called.should == 2
1651
- end
1652
-
1653
- it "should invoke callbacks only for real requests if requested", :net_connect => true do
1654
- WebMock.after_request(:real_requests_only => true) { @called = true }
1655
- http_request(:get, "http://www.example.com/")
1656
- @called.should == nil
1657
- WebMock.allow_net_connect!
1658
- http_request(:get, "http://www.example.net/")
1659
- @called.should == true
1660
- end
1661
-
1662
- it "should clear all declared callbacks on reset callbacks" do
1663
- WebMock.after_request { @called = 1 }
1664
- WebMock.reset_callbacks
1665
- stub_request(:get, "http://www.example.com/")
1666
- http_request(:get, "http://www.example.com/")
1667
- @called.should == nil
1668
- end
1669
- end
1670
- end
1671
-
1672
- describe "enabling and disabling webmock" do
1673
- describe "when webmock is disabled" do
1674
- before(:each) do
1675
- WebMock.disable!
1676
- end
1677
- after(:each) do
1678
- WebMock.enable!
1679
- end
1680
- it_should_behave_like "disabled WebMock"
1681
- end
1682
-
1683
- describe "when webmock is enabled again" do
1684
- before(:each) do
1685
- WebMock.disable!
1686
- WebMock.enable!
1687
- end
1688
- it_should_behave_like "enabled WebMock"
1689
- end
1690
-
1691
- describe "when webmock is disabled except this lib" do
1692
- before(:each) do
1693
- WebMock.disable!(:except => [http_library])
1694
- end
1695
- after(:each) do
1696
- WebMock.enable!
1697
- end
1698
- it_should_behave_like "enabled WebMock"
1699
- end
1700
-
1701
- describe "when webmock is enabled except this lib" do
1702
- before(:each) do
1703
- WebMock.disable!
1704
- WebMock.enable!(:except => [http_library])
1705
- end
1706
- after(:each) do
1707
- WebMock.enable!
1708
- end
1709
- it_should_behave_like "disabled WebMock"
1710
- end
1711
- end
1712
- end
1713
-
1714
- shared_examples_for "disabled WebMock" do
1715
-
1716
- it "should not register executed requests" do
1717
- http_request(:get, "http://www.example.com/")
1718
- a_request(:get, "http://www.example.com/").should_not have_been_made
1719
- end
1720
-
1721
- it "should not block unstubbed requests" do
1722
- lambda {
1723
- http_request(:get, "http://www.example.com/")
1724
- }.should_not raise_error
1725
- end
1726
-
1727
- it "should return real response even if there are stubs" do
1728
- stub_request(:get, /.*/).to_return(:body => "x")
1729
- http_request(:get, "http://www.example.com/").
1730
- status.should == "302"
1731
- end
1732
-
1733
- it "should not invoke any callbacks" do
1734
- WebMock.reset_callbacks
1735
- stub_request(:get, "http://www.example.com/")
1736
- @called = nil
1737
- WebMock.after_request { @called = 1 }
1738
- http_request(:get, "http://www.example.com/")
1739
- @called.should == nil
1740
- end
1741
- end
1742
-
1743
- shared_examples_for "enabled WebMock" do
1744
- it "should register executed requests" do
1745
- WebMock.allow_net_connect!
1746
- http_request(:get, "http://www.example.com/")
1747
- a_request(:get, "http://www.example.com/").should have_been_made
1748
- end
1749
-
1750
- it "should block unstubbed requests" do
1751
- lambda {
1752
- http_request(:get, "http://www.example.com/")
1753
- }.should raise_error(WebMock::NetConnectNotAllowedError)
1754
- end
1755
-
1756
- it "should return stubbed response" do
1757
- stub_request(:get, /.*/).to_return(:body => "x")
1758
- http_request(:get, "http://www.example.com/").body.should == "x"
1759
- end
1760
-
1761
- it "should invoke callbacks" do
1762
- WebMock.allow_net_connect!
1763
- WebMock.reset_callbacks
1764
- @called = nil
1765
- WebMock.after_request { @called = 1 }
1766
- http_request(:get, "http://www.example.com/")
1767
- @called.should == 1
1768
- end
1769
- end
1770
-