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
@@ -0,0 +1,377 @@
1
+ class MyException < StandardError; end;
2
+
3
+ shared_context "declared responses" do
4
+ describe "when request stub declares that request should raise exception" do
5
+ it "should raise exception" do
6
+ stub_request(:get, "www.example.com").to_raise(MyException)
7
+ lambda {
8
+ http_request(:get, "http://www.example.com/")
9
+ }.should raise_error(MyException, "Exception from WebMock")
10
+ end
11
+
12
+ it "should raise exception if declared as and exception instance" do
13
+ stub_request(:get, "www.example.com").to_raise(MyException.new("hello world"))
14
+ lambda {
15
+ http_request(:get, "http://www.example.com/")
16
+ }.should raise_error(MyException, "hello world")
17
+ end
18
+
19
+ it "should raise exception if declared as an exception instance" do
20
+ stub_request(:get, "www.example.com").to_raise("hello world")
21
+ lambda {
22
+ http_request(:get, "http://www.example.com/")
23
+ }.should raise_error("hello world")
24
+ end
25
+
26
+ it "should raise exception after returning declared successful response first" do
27
+ stub_request(:get, "www.example.com").to_return(:body => "abc").then.to_raise(MyException)
28
+ http_request(:get, "http://www.example.com/").body.should == "abc"
29
+ lambda {
30
+ http_request(:get, "http://www.example.com/")
31
+ }.should raise_error(MyException, "Exception from WebMock")
32
+ end
33
+ end
34
+
35
+ describe "when request stub declares that request should timeout" do
36
+ it "should timeout" do
37
+ stub_request(:get, "www.example.com").to_timeout
38
+ lambda {
39
+ http_request(:get, "http://www.example.com/")
40
+ }.should raise_error(client_timeout_exception_class)
41
+ end
42
+
43
+ it "should timeout after returning declared successful response" do
44
+ stub_request(:get, "www.example.com").to_return(:body => "abc").then.to_timeout
45
+ http_request(:get, "http://www.example.com/").body.should == "abc"
46
+ lambda {
47
+ http_request(:get, "http://www.example.com/")
48
+ }.should raise_error(client_timeout_exception_class)
49
+ end
50
+ end
51
+
52
+ describe "when request stub declares that request should return a response" do
53
+ it "should return response with declared body" do
54
+ stub_request(:get, "www.example.com").to_return(:body => "abc")
55
+ http_request(:get, "http://www.example.com/").body.should == "abc"
56
+ end
57
+
58
+ it "should return response with declared headers" do
59
+ stub_request(:get, "www.example.com").to_return(:headers => SAMPLE_HEADERS)
60
+ response = http_request(:get, "http://www.example.com/")
61
+ response.headers["Content-Length"].should == "8888"
62
+ end
63
+
64
+ it "should return response with declared headers even if there are multiple headers with the same key" do
65
+ stub_request(:get, "www.example.com").to_return(:headers => {"a" => ["b", "c"]})
66
+ response = http_request(:get, "http://www.example.com/")
67
+ response.headers["A"].should == "b, c"
68
+ end
69
+
70
+ it "should return response with declared status code" do
71
+ stub_request(:get, "www.example.com").to_return(:status => 500)
72
+ http_request(:get, "http://www.example.com/").status.should == "500"
73
+ end
74
+
75
+ it "should return response with declared status message" do
76
+ stub_request(:get, "www.example.com").to_return(:status => [500, "Internal Server Error"])
77
+ response = http_request(:get, "http://www.example.com/")
78
+ # not supported by em-http-request, it always returns "unknown" for http_reason
79
+ unless http_library == :em_http_request
80
+ response.message.should == "Internal Server Error"
81
+ end
82
+ end
83
+
84
+ it "should return response with a default status code" do
85
+ stub_request(:get, "www.example.com")
86
+ http_request(:get, "http://www.example.com/").status.should == "200"
87
+ end
88
+
89
+ it "should return default response with empty message if response was not declared" do
90
+ stub_request(:get, "www.example.com")
91
+ response = http_request(:get, "http://www.example.com/")
92
+ # not supported by em-http-request, it always returns "unknown" for http_reason
93
+ unless http_library == :em_http_request
94
+ response.message.should == ""
95
+ end
96
+ end
97
+
98
+ describe "when response body was declared as IO" do
99
+ it "should return response body" do
100
+ stub_request(:get, "www.example.com").to_return(:body => File.new(__FILE__))
101
+ http_request(:get, "http://www.example.com/").body.should == File.new(__FILE__).read
102
+ end
103
+
104
+ it "should return response body if requested many times" do
105
+ stub_request(:get, "www.example.com").to_return(:body => File.new(__FILE__))
106
+ 2.times do
107
+ http_request(:get, "http://www.example.com/").body.should == File.new(__FILE__).read
108
+ end
109
+ end
110
+
111
+ it "should close IO after request" do
112
+ stub_request(:get, "www.example.com").to_return(:body => @file = File.new(__FILE__))
113
+ @file.should be_closed
114
+ end
115
+ end
116
+
117
+ describe "when response parts were declared as lambdas" do
118
+ it "should return evaluated response body" do
119
+ stub_request(:post, "www.example.com").to_return(:body => lambda { |request| request.body })
120
+ http_request(:post, "http://www.example.com/", :body => "echo").body.should == "echo"
121
+ end
122
+
123
+ it "should return evaluated response headers" do
124
+ stub_request(:post, "www.example.com").to_return(:headers => lambda { |request| request.headers })
125
+ http_request(:post, "http://www.example.com/", :body => "abc", :headers => {'A' => 'B'}).headers['A'].should == 'B'
126
+ end
127
+ end
128
+
129
+ describe "when response was declared as lambda" do
130
+ class Responder
131
+ def call(request)
132
+ {:body => request.body}
133
+ end
134
+ end
135
+
136
+ it "should return evaluated response body" do
137
+ stub_request(:post, "www.example.com").to_return(lambda {|request|
138
+ {:body => request.body}
139
+ })
140
+ http_request(:post, "http://www.example.com/", :body => "echo").body.should == "echo"
141
+ end
142
+
143
+ it "should return evaluated response headers" do
144
+ stub_request(:get, "www.example.com").to_return(lambda { |request|
145
+ {:headers => request.headers}
146
+ })
147
+ http_request(:get, "http://www.example.com/", :headers => {'A' => 'B'}).headers['A'].should == 'B'
148
+ end
149
+
150
+ it "should return dynamic response declared as a block" do
151
+ stub_request(:post, "www.example.com").to_return do |request|
152
+ {:body => request.body}
153
+ end
154
+ http_request(:post, "http://www.example.com/", :body => "echo").body.should == "echo"
155
+ end
156
+
157
+ it "should return dynamic response declared as an object responding to call" do
158
+ stub_request(:post, "www.example.com").to_return(Responder.new)
159
+ http_request(:post, "http://www.example.com/", :body => "echo").body.should == "echo"
160
+ end
161
+ end
162
+
163
+
164
+ describe "when response was declared as a file with a raw response" do
165
+ before(:each) do
166
+ @file = File.new(CURL_EXAMPLE_OUTPUT_PATH)
167
+ stub_request(:get, "www.example.com").to_return(@file)
168
+ @response = http_request(:get, "http://www.example.com/")
169
+ end
170
+
171
+ it "should return recorded headers" do
172
+ @response.headers.should == {
173
+ "Date"=>"Sat, 23 Jan 2010 01:01:05 GMT",
174
+ "Content-Type"=>"text/html; charset=UTF-8",
175
+ "Content-Length"=>"419",
176
+ "Connection"=>"Keep-Alive",
177
+ "Accept"=>"image/jpeg, image/png"
178
+ }
179
+ end
180
+
181
+ it "should return recorded body" do
182
+ @response.body.size.should == 419
183
+ end
184
+
185
+ it "should return recorded status" do
186
+ @response.status.should == "202"
187
+ end
188
+
189
+ it "should return recorded status message" do
190
+ # not supported by em-http-request, it always returns "unknown" for http_reason
191
+ unless http_library == :em_http_request
192
+ @response.message.should == "OK"
193
+ end
194
+ end
195
+
196
+ it "should ensure file is closed" do
197
+ @file.should be_closed
198
+ end
199
+ end
200
+
201
+ describe "when response was declared as a string with a raw response" do
202
+ before(:each) do
203
+ @input = File.new(CURL_EXAMPLE_OUTPUT_PATH).read
204
+ stub_request(:get, "www.example.com").to_return(@input)
205
+ @response = http_request(:get, "http://www.example.com/")
206
+ end
207
+
208
+ it "should return recorded headers" do
209
+ @response.headers.should == {
210
+ "Date"=>"Sat, 23 Jan 2010 01:01:05 GMT",
211
+ "Content-Type"=>"text/html; charset=UTF-8",
212
+ "Content-Length"=>"419",
213
+ "Connection"=>"Keep-Alive",
214
+ "Accept"=>"image/jpeg, image/png"
215
+ }
216
+ end
217
+
218
+ it "should return recorded body" do
219
+ @response.body.size.should == 419
220
+ end
221
+
222
+ it "should return recorded status" do
223
+ @response.status.should == "202"
224
+ end
225
+
226
+ it "should return recorded status message" do
227
+ # not supported by em-http-request, it always returns "unknown" for http_reason
228
+ unless http_library == :em_http_request
229
+ @response.message.should == "OK"
230
+ end
231
+ end
232
+ end
233
+
234
+ describe "when response was declared as lambda evaluating to a string with a raw response" do
235
+ before(:each) do
236
+ @files = {
237
+ "www.example.com" => File.new(CURL_EXAMPLE_OUTPUT_PATH)
238
+ }
239
+ end
240
+
241
+ it "should return response from evaluated file" do
242
+ stub_request(:get, "www.example.com").to_return(lambda {|request| @files[request.uri.host.to_s] })
243
+ http_request(:get, "http://www.example.com/").body.size.should == 419
244
+ end
245
+
246
+ it "should return response from evaluated string" do
247
+ stub_request(:get, "www.example.com").to_return(lambda {|request| @files[request.uri.host.to_s].read })
248
+ http_request(:get, "http://www.example.com/").body.size.should == 419
249
+ end
250
+ end
251
+
252
+ describe "when response is declared as an Rack app" do
253
+ before(:each) do
254
+ stub_request(:any, "http://www.example.com/greet").to_rack(MyRackApp)
255
+ end
256
+
257
+ it "should return response returned by the rack app" do
258
+ http_request(:post, 'http://www.example.com/greet', :body => 'name=Jimmy').body.should == 'Good to meet you, Jimmy!'
259
+ end
260
+ end
261
+
262
+ describe "when sequences of responses are declared" do
263
+ it "should return responses one by one if declared in array" do
264
+ stub_request(:get, "www.example.com").to_return([ {:body => "1"}, {:body => "2"}, {:body => "3"} ])
265
+ http_request(:get, "http://www.example.com/").body.should == "1"
266
+ http_request(:get, "http://www.example.com/").body.should == "2"
267
+ http_request(:get, "http://www.example.com/").body.should == "3"
268
+ end
269
+
270
+ it "should repeat returning last declared response from a sequence after all responses were returned" do
271
+ stub_request(:get, "www.example.com").to_return([ {:body => "1"}, {:body => "2"} ])
272
+ http_request(:get, "http://www.example.com/").body.should == "1"
273
+ http_request(:get, "http://www.example.com/").body.should == "2"
274
+ http_request(:get, "http://www.example.com/").body.should == "2"
275
+ end
276
+
277
+ it "should return responses one by one if declared as comma separated params" do
278
+ stub_request(:get, "www.example.com").to_return({:body => "1"}, {:body => "2"}, {:body => "3"})
279
+ http_request(:get, "http://www.example.com/").body.should == "1"
280
+ http_request(:get, "http://www.example.com/").body.should == "2"
281
+ http_request(:get, "http://www.example.com/").body.should == "3"
282
+ end
283
+
284
+ it "should return responses one by one if declared with several to_return invokations" do
285
+ stub_request(:get, "www.example.com").
286
+ to_return({:body => "1"}).
287
+ to_return({:body => "2"}).
288
+ to_return({:body => "3"})
289
+ http_request(:get, "http://www.example.com/").body.should == "1"
290
+ http_request(:get, "http://www.example.com/").body.should == "2"
291
+ http_request(:get, "http://www.example.com/").body.should == "3"
292
+ end
293
+
294
+ it "should return responses one by one if declared with to_return invocations separated with then syntactic sugar" do
295
+ stub_request(:get, "www.example.com").to_return({:body => "1"}).then.
296
+ to_return({:body => "2"}).then.to_return({:body => "3"})
297
+ http_request(:get, "http://www.example.com/").body.should == "1"
298
+ http_request(:get, "http://www.example.com/").body.should == "2"
299
+ http_request(:get, "http://www.example.com/").body.should == "3"
300
+ end
301
+
302
+ end
303
+
304
+ describe "when responses are declared to return more than once" do
305
+ it "should repeat one response declared number of times" do
306
+ stub_request(:get, "www.example.com").
307
+ to_return({:body => "1"}).times(2).
308
+ to_return({:body => "2"})
309
+ http_request(:get, "http://www.example.com/").body.should == "1"
310
+ http_request(:get, "http://www.example.com/").body.should == "1"
311
+ http_request(:get, "http://www.example.com/").body.should == "2"
312
+ end
313
+
314
+
315
+ it "should repeat sequence of response declared number of times" do
316
+ stub_request(:get, "www.example.com").
317
+ to_return({:body => "1"}, {:body => "2"}).times(2).
318
+ to_return({:body => "3"})
319
+ http_request(:get, "http://www.example.com/").body.should == "1"
320
+ http_request(:get, "http://www.example.com/").body.should == "2"
321
+ http_request(:get, "http://www.example.com/").body.should == "1"
322
+ http_request(:get, "http://www.example.com/").body.should == "2"
323
+ http_request(:get, "http://www.example.com/").body.should == "3"
324
+ end
325
+
326
+
327
+ it "should repeat infinitely last response even if number of declared times is lower" do
328
+ stub_request(:get, "www.example.com").
329
+ to_return({:body => "1"}).times(2)
330
+ http_request(:get, "http://www.example.com/").body.should == "1"
331
+ http_request(:get, "http://www.example.com/").body.should == "1"
332
+ http_request(:get, "http://www.example.com/").body.should == "1"
333
+ end
334
+
335
+ it "should give error if times is declared without specifying response" do
336
+ lambda {
337
+ stub_request(:get, "www.example.com").times(3)
338
+ }.should raise_error("Invalid WebMock stub declaration. times(N) can be declared only after response declaration.")
339
+ end
340
+
341
+ end
342
+
343
+ describe "when exception is declared to be raised more than once" do
344
+ it "should repeat raising exception declared number of times" do
345
+ stub_request(:get, "www.example.com").
346
+ to_raise(MyException).times(2).
347
+ to_return({:body => "2"})
348
+ lambda {
349
+ http_request(:get, "http://www.example.com/")
350
+ }.should raise_error(MyException, "Exception from WebMock")
351
+ lambda {
352
+ http_request(:get, "http://www.example.com/")
353
+ }.should raise_error(MyException, "Exception from WebMock")
354
+ http_request(:get, "http://www.example.com/").body.should == "2"
355
+ end
356
+
357
+ it "should repeat raising sequence of exceptions declared number of times" do
358
+ stub_request(:get, "www.example.com").
359
+ to_raise(MyException, ArgumentError).times(2).
360
+ to_return({:body => "2"})
361
+ lambda {
362
+ http_request(:get, "http://www.example.com/")
363
+ }.should raise_error(MyException, "Exception from WebMock")
364
+ lambda {
365
+ http_request(:get, "http://www.example.com/")
366
+ }.should raise_error(ArgumentError)
367
+ lambda {
368
+ http_request(:get, "http://www.example.com/")
369
+ }.should raise_error(MyException, "Exception from WebMock")
370
+ lambda {
371
+ http_request(:get, "http://www.example.com/")
372
+ }.should raise_error(ArgumentError)
373
+ http_request(:get, "http://www.example.com/").body.should == "2"
374
+ end
375
+ end
376
+ end
377
+ end
@@ -0,0 +1,314 @@
1
+ shared_examples_for "stubbing requests" do
2
+ describe "when requests are stubbed" do
3
+ describe "based on uri" do
4
+ it "should return stubbed response even if request have escaped parameters" do
5
+ stub_request(:get, "www.example.com/hello/?#{NOT_ESCAPED_PARAMS}").to_return(:body => "abc")
6
+ http_request(:get, "http://www.example.com/hello/?#{ESCAPED_PARAMS}").body.should == "abc"
7
+ end
8
+
9
+ it "should return stubbed response even if request has non escaped params" do
10
+ stub_request(:get, "www.example.com/hello/?#{ESCAPED_PARAMS}").to_return(:body => "abc")
11
+ http_request(:get, "http://www.example.com/hello/?#{NOT_ESCAPED_PARAMS}").body.should == "abc"
12
+ end
13
+
14
+ it "should return stubbed response even if stub uri is declared as regexp and request params are escaped" do
15
+ stub_request(:get, /.*x=ab c.*/).to_return(:body => "abc")
16
+ http_request(:get, "http://www.example.com/hello/?#{ESCAPED_PARAMS}").body.should == "abc"
17
+ end
18
+ end
19
+
20
+ describe "based on query params" do
21
+ it "should return stubbed response when stub declares query params as a hash" do
22
+ stub_request(:get, "www.example.com").with(:query => {"a" => ["b", "c"]}).to_return(:body => "abc")
23
+ http_request(:get, "http://www.example.com/?a[]=b&a[]=c").body.should == "abc"
24
+ end
25
+
26
+ it "should return stubbed response when stub declares query params as a hash" do
27
+ stub_request(:get, "www.example.com").with(:query => "a[]=b&a[]=c").to_return(:body => "abc")
28
+ http_request(:get, "http://www.example.com/?a[]=b&a[]=c").body.should == "abc"
29
+ end
30
+
31
+ it "should return stubbed response when stub declares query params both in uri and as a hash" do
32
+ stub_request(:get, "www.example.com/?x=3").with(:query => {"a" => ["b", "c"]}).to_return(:body => "abc")
33
+ http_request(:get, "http://www.example.com/?x=3&a[]=b&a[]=c").body.should == "abc"
34
+ end
35
+ end
36
+
37
+ describe "based on method" do
38
+ it "should return stubbed response" do
39
+ stub_request(:get, "www.example.com")
40
+ http_request(:get, "http://www.example.com/").status.should == "200"
41
+ end
42
+
43
+ it "should raise error if stubbed request has different method" do
44
+ stub_request(:get, "www.example.com")
45
+ http_request(:get, "http://www.example.com/").status.should == "200"
46
+ lambda {
47
+ http_request(:delete, "http://www.example.com/")
48
+ }.should raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: DELETE http://www.example.com/)
49
+ )
50
+ end
51
+ end
52
+
53
+ describe "based on body" do
54
+ it "should match requests if body is the same" do
55
+ stub_request(:post, "www.example.com").with(:body => "abc")
56
+ http_request(
57
+ :post, "http://www.example.com/",
58
+ :body => "abc").status.should == "200"
59
+ end
60
+
61
+ it "should match requests if body is not set in the stub" do
62
+ stub_request(:post, "www.example.com")
63
+ http_request(
64
+ :post, "http://www.example.com/",
65
+ :body => "abc").status.should == "200"
66
+ end
67
+
68
+ it "should not match requests if body is different" do
69
+ stub_request(:post, "www.example.com").with(:body => "abc")
70
+ lambda {
71
+ http_request(:post, "http://www.example.com/", :body => "def")
72
+ }.should raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: POST http://www.example.com/ with body 'def'))
73
+ end
74
+
75
+ describe "with regular expressions" do
76
+ it "should match requests if body matches regexp" do
77
+ stub_request(:post, "www.example.com").with(:body => /\d+abc$/)
78
+ http_request(
79
+ :post, "http://www.example.com/",
80
+ :body => "123abc").status.should == "200"
81
+ end
82
+
83
+ it "should not match requests if body doesn't match regexp" do
84
+ stub_request(:post, "www.example.com").with(:body => /^abc/)
85
+ lambda {
86
+ http_request(:post, "http://www.example.com/", :body => "xabc")
87
+ }.should raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: POST http://www.example.com/ with body 'xabc'))
88
+ end
89
+ end
90
+
91
+ describe "when body is declared as a hash" do
92
+ before(:each) do
93
+ stub_request(:post, "www.example.com").
94
+ with(:body => {:a => '1', :b => 'five', 'c' => {'d' => ['e', 'f']} })
95
+ end
96
+
97
+ describe "for request with url encoded body" do
98
+ it "should match request if hash matches body" do
99
+ http_request(
100
+ :post, "http://www.example.com/",
101
+ :body => 'a=1&c[d][]=e&c[d][]=f&b=five').status.should == "200"
102
+ end
103
+
104
+ it "should match request if hash matches body in different order of params" do
105
+ http_request(
106
+ :post, "http://www.example.com/",
107
+ :body => 'a=1&c[d][]=e&b=five&c[d][]=f').status.should == "200"
108
+ end
109
+
110
+ it "should not match if hash doesn't match url encoded body" do
111
+ lambda {
112
+ http_request(
113
+ :post, "http://www.example.com/",
114
+ :body => 'c[d][]=f&a=1&c[d][]=e').status.should == "200"
115
+ }.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'))
116
+ end
117
+ end
118
+
119
+
120
+ describe "for request with json body and content type is set to json" do
121
+ it "should match if hash matches body" do
122
+ http_request(
123
+ :post, "http://www.example.com/", :headers => {'Content-Type' => 'application/json'},
124
+ :body => "{\"a\":\"1\",\"c\":{\"d\":[\"e\",\"f\"]},\"b\":\"five\"}").status.should == "200"
125
+ end
126
+
127
+ it "should match if hash matches body in different form" do
128
+ http_request(
129
+ :post, "http://www.example.com/", :headers => {'Content-Type' => 'application/json'},
130
+ :body => "{\"a\":\"1\",\"b\":\"five\",\"c\":{\"d\":[\"e\",\"f\"]}}").status.should == "200"
131
+ end
132
+
133
+ it "should match if hash contains date string" do #Crack creates date object
134
+ WebMock.reset!
135
+ stub_request(:post, "www.example.com").
136
+ with(:body => {"foo" => "2010-01-01"})
137
+ http_request(
138
+ :post, "http://www.example.com/", :headers => {'Content-Type' => 'application/json'},
139
+ :body => "{\"foo\":\"2010-01-01\"}").status.should == "200"
140
+ end
141
+ end
142
+
143
+ describe "for request with xml body and content type is set to xml" do
144
+ before(:each) do
145
+ WebMock.reset!
146
+ stub_request(:post, "www.example.com").
147
+ with(:body => { "opt" => {:a => '1', :b => 'five', 'c' => {'d' => ['e', 'f']} }})
148
+ end
149
+
150
+ it "should match if hash matches body" do
151
+ http_request(
152
+ :post, "http://www.example.com/", :headers => {'Content-Type' => 'application/xml'},
153
+ :body => "<opt a=\"1\" b=\"five\">\n <c>\n <d>e</d>\n <d>f</d>\n </c>\n</opt>\n").status.should == "200"
154
+ end
155
+
156
+ it "should match if hash matches body in different form" do
157
+ http_request(
158
+ :post, "http://www.example.com/", :headers => {'Content-Type' => 'application/xml'},
159
+ :body => "<opt b=\"five\" a=\"1\">\n <c>\n <d>e</d>\n <d>f</d>\n </c>\n</opt>\n").status.should == "200"
160
+ end
161
+
162
+ it "should match if hash contains date string" do #Crack creates date object
163
+ WebMock.reset!
164
+ stub_request(:post, "www.example.com").
165
+ with(:body => {"opt" => {"foo" => "2010-01-01"}})
166
+ http_request(
167
+ :post, "http://www.example.com/", :headers => {'Content-Type' => 'application/xml'},
168
+ :body => "<opt foo=\"2010-01-01\">\n</opt>\n").status.should == "200"
169
+ end
170
+ end
171
+ end
172
+ end
173
+
174
+ describe "based on headers" do
175
+ it "should match requests if headers are the same" do
176
+ stub_request(:get, "www.example.com").with(:headers => SAMPLE_HEADERS )
177
+ http_request(
178
+ :get, "http://www.example.com/",
179
+ :headers => SAMPLE_HEADERS).status.should == "200"
180
+ end
181
+
182
+ it "should match requests if headers are the same and declared as array" do
183
+ stub_request(:get, "www.example.com").with(:headers => {"a" => ["b"]} )
184
+ http_request(
185
+ :get, "http://www.example.com/",
186
+ :headers => {"a" => "b"}).status.should == "200"
187
+ end
188
+
189
+ describe "when multiple headers with the same key are used" do
190
+ it "should match requests if headers are the same" do
191
+ stub_request(:get, "www.example.com").with(:headers => {"a" => ["b", "c"]} )
192
+ http_request(
193
+ :get, "http://www.example.com/",
194
+ :headers => {"a" => ["b", "c"]}).status.should == "200"
195
+ end
196
+
197
+ it "should match requests if headers are the same but in different order" do
198
+ stub_request(:get, "www.example.com").with(:headers => {"a" => ["b", "c"]} )
199
+ http_request(
200
+ :get, "http://www.example.com/",
201
+ :headers => {"a" => ["c", "b"]}).status.should == "200"
202
+ end
203
+
204
+ it "should not match requests if headers are different" do
205
+ stub_request(:get, "www.example.com").with(:headers => {"a" => ["b", "c"]})
206
+
207
+ lambda {
208
+ http_request(
209
+ :get, "http://www.example.com/",
210
+ :headers => {"a" => ["b", "d"]})
211
+ }.should raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/ with headers))
212
+ end
213
+ end
214
+
215
+ it "should match requests if request headers are not stubbed" do
216
+ stub_request(:get, "www.example.com")
217
+ http_request(
218
+ :get, "http://www.example.com/",
219
+ :headers => SAMPLE_HEADERS).status.should == "200"
220
+ end
221
+
222
+ it "should not match requests if headers are different" do
223
+ stub_request(:get, "www.example.com").with(:headers => SAMPLE_HEADERS)
224
+
225
+ lambda {
226
+ http_request(
227
+ :get, "http://www.example.com/",
228
+ :headers => { 'Content-Length' => '9999'})
229
+ }.should raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/ with headers))
230
+ end
231
+
232
+ it "should not match if accept header is different" do
233
+ stub_request(:get, "www.example.com").
234
+ with(:headers => { 'Accept' => 'application/json'})
235
+ lambda {
236
+ http_request(
237
+ :get, "http://www.example.com/",
238
+ :headers => { 'Accept' => 'application/xml'})
239
+ }.should raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/ with headers))
240
+ end
241
+
242
+ describe "declared as regular expressions" do
243
+ it "should match requests if header values match regular expression" do
244
+ stub_request(:get, "www.example.com").with(:headers => { :some_header => /^MyAppName$/ })
245
+ http_request(
246
+ :get, "http://www.example.com/",
247
+ :headers => { 'some-header' => 'MyAppName' }).status.should == "200"
248
+ end
249
+
250
+ it "should not match requests if headers values do not match regular expression" do
251
+ stub_request(:get, "www.example.com").with(:headers => { :some_header => /^MyAppName$/ })
252
+
253
+ lambda {
254
+ http_request(
255
+ :get, "http://www.example.com/",
256
+ :headers => { 'some-header' => 'xMyAppName' })
257
+ }.should raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/ with headers))
258
+ end
259
+ end
260
+ end
261
+
262
+ describe "when stubbing request with basic authentication" do
263
+ it "should match if credentials are the same" do
264
+ stub_request(:get, "user:pass@www.example.com")
265
+ http_request(:get, "http://user:pass@www.example.com/").status.should == "200"
266
+ end
267
+
268
+ it "should not match if credentials are different" do
269
+ stub_request(:get, "user:pass@www.example.com")
270
+ lambda {
271
+ http_request(:get, "http://user:pazz@www.example.com/").status.should == "200"
272
+ }.should raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://user:pazz@www.example.com/))
273
+ end
274
+
275
+ it "should not match if credentials are stubbed but not provided in the request" do
276
+ stub_request(:get, "user:pass@www.example.com")
277
+ lambda {
278
+ http_request(:get, "http://www.example.com/").status.should == "200"
279
+ }.should raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/))
280
+ end
281
+
282
+ it "should not match if credentials are not stubbed but exist in the request" do
283
+ stub_request(:get, "www.example.com")
284
+ lambda {
285
+ http_request(:get, "http://user:pazz@www.example.com/").status.should == "200"
286
+ }.should raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://user:pazz@www.example.com/))
287
+ end
288
+ end
289
+
290
+ describe "when stubbing request with a block evaluated on request" do
291
+ it "should match if block returns true" do
292
+ stub_request(:get, "www.example.com").with { |request| true }
293
+ http_request(:get, "http://www.example.com/").status.should == "200"
294
+ end
295
+
296
+ it "should not match if block returns false" do
297
+ stub_request(:get, "www.example.com").with { |request| false }
298
+ lambda {
299
+ http_request(:get, "http://www.example.com/")
300
+ }.should raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/))
301
+ end
302
+
303
+ it "should pass the request to the block" do
304
+ stub_request(:post, "www.example.com").with { |request| request.body == "wadus" }
305
+ http_request(
306
+ :post, "http://www.example.com/",
307
+ :body => "wadus").status.should == "200"
308
+ lambda {
309
+ http_request(:post, "http://www.example.com/", :body => "jander")
310
+ }.should raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: POST http://www.example.com/ with body 'jander'))
311
+ end
312
+ end
313
+ end
314
+ end