webmock 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (59) hide show
  1. data/.gitignore +21 -0
  2. data/LICENSE +281 -0
  3. data/README.md +170 -0
  4. data/Rakefile +54 -0
  5. data/VERSION +1 -0
  6. data/lib/webmock.rb +18 -0
  7. data/lib/webmock/adapters/rspec.rb +23 -0
  8. data/lib/webmock/adapters/rspec/matchers.rb +19 -0
  9. data/lib/webmock/adapters/rspec/request_profile_matcher.rb +37 -0
  10. data/lib/webmock/adapters/rspec/webmock_matcher.rb +45 -0
  11. data/lib/webmock/adapters/test_unit.rb +25 -0
  12. data/lib/webmock/config.rb +7 -0
  13. data/lib/webmock/errors.rb +5 -0
  14. data/lib/webmock/http_lib_adapters/net_http.rb +129 -0
  15. data/lib/webmock/request_execution_verifier.rb +22 -0
  16. data/lib/webmock/request_profile.rb +67 -0
  17. data/lib/webmock/request_registry.rb +47 -0
  18. data/lib/webmock/request_stub.rb +26 -0
  19. data/lib/webmock/response.rb +31 -0
  20. data/lib/webmock/url.rb +46 -0
  21. data/lib/webmock/util/hash_counter.rb +12 -0
  22. data/lib/webmock/utility.rb +65 -0
  23. data/lib/webmock/webmock.rb +58 -0
  24. data/spec/net_http_spec.rb +33 -0
  25. data/spec/other_net_http_libs_spec.rb +37 -0
  26. data/spec/request_execution_verifier_spec.rb +51 -0
  27. data/spec/request_profile_spec.rb +166 -0
  28. data/spec/request_registry_spec.rb +114 -0
  29. data/spec/request_stub_spec.rb +54 -0
  30. data/spec/response_spec.rb +59 -0
  31. data/spec/spec.opts +1 -0
  32. data/spec/spec_helper.rb +58 -0
  33. data/spec/util/hash_counter_spec.rb +24 -0
  34. data/spec/utility_spec.rb +70 -0
  35. data/spec/vendor/right_http_connection-1.2.4/History.txt +59 -0
  36. data/spec/vendor/right_http_connection-1.2.4/Manifest.txt +7 -0
  37. data/spec/vendor/right_http_connection-1.2.4/README.txt +54 -0
  38. data/spec/vendor/right_http_connection-1.2.4/Rakefile +103 -0
  39. data/spec/vendor/right_http_connection-1.2.4/lib/net_fix.rb +160 -0
  40. data/spec/vendor/right_http_connection-1.2.4/lib/right_http_connection.rb +435 -0
  41. data/spec/vendor/right_http_connection-1.2.4/setup.rb +1585 -0
  42. data/spec/vendor/samuel-0.2.1/.document +5 -0
  43. data/spec/vendor/samuel-0.2.1/.gitignore +5 -0
  44. data/spec/vendor/samuel-0.2.1/LICENSE +20 -0
  45. data/spec/vendor/samuel-0.2.1/README.rdoc +70 -0
  46. data/spec/vendor/samuel-0.2.1/Rakefile +62 -0
  47. data/spec/vendor/samuel-0.2.1/VERSION +1 -0
  48. data/spec/vendor/samuel-0.2.1/lib/samuel.rb +52 -0
  49. data/spec/vendor/samuel-0.2.1/lib/samuel/net_http.rb +10 -0
  50. data/spec/vendor/samuel-0.2.1/lib/samuel/request.rb +96 -0
  51. data/spec/vendor/samuel-0.2.1/samuel.gemspec +69 -0
  52. data/spec/vendor/samuel-0.2.1/test/request_test.rb +193 -0
  53. data/spec/vendor/samuel-0.2.1/test/samuel_test.rb +42 -0
  54. data/spec/vendor/samuel-0.2.1/test/test_helper.rb +66 -0
  55. data/spec/vendor/samuel-0.2.1/test/thread_test.rb +32 -0
  56. data/spec/webmock_spec.rb +492 -0
  57. data/test/test_helper.rb +9 -0
  58. data/test/test_webmock.rb +56 -0
  59. metadata +144 -0
@@ -0,0 +1,42 @@
1
+ require 'test_helper'
2
+
3
+ class SamuelTest < Test::Unit::TestCase
4
+
5
+ context "logger configuration" do
6
+ setup do
7
+ Samuel.logger = nil
8
+ if Object.const_defined?(:RAILS_DEFAULT_LOGGER)
9
+ Object.send(:remove_const, :RAILS_DEFAULT_LOGGER)
10
+ end
11
+ end
12
+
13
+ teardown do
14
+ Samuel.logger = nil
15
+ end
16
+
17
+ context "when Rails's logger is available" do
18
+ setup { Object.const_set(:RAILS_DEFAULT_LOGGER, :mock_logger) }
19
+
20
+ should "use the same logger" do
21
+ assert_equal :mock_logger, Samuel.logger
22
+ end
23
+ end
24
+
25
+ context "when Rails's logger is not available" do
26
+ should "use a new Logger instance pointed to STDOUT" do
27
+ assert_instance_of Logger, Samuel.logger
28
+ assert_equal STDOUT, Samuel.logger.instance_variable_get(:"@logdev").dev
29
+ end
30
+ end
31
+ end
32
+
33
+
34
+ context ".reset_config" do
35
+ should "reset the config to default vaules" do
36
+ Samuel.config = {:foo => "bar"}
37
+ Samuel.reset_config
38
+ assert_equal({:label => nil, :labels => {"" => "HTTP"}, :filtered_params => []}, Samuel.config)
39
+ end
40
+ end
41
+
42
+ end
@@ -0,0 +1,66 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+ require 'mocha'
5
+ require 'open-uri'
6
+ require 'fakeweb'
7
+
8
+ FakeWeb.allow_net_connect = false
9
+
10
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
11
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
12
+ require 'samuel'
13
+
14
+ class Test::Unit::TestCase
15
+ TEST_LOG_PATH = File.join(File.dirname(__FILE__), 'test.log')
16
+
17
+ def self.should_log_lines(expected_count)
18
+ should "log #{expected_count} line#{'s' unless expected_count == 1}" do
19
+ lines = File.readlines(TEST_LOG_PATH)
20
+ assert_equal expected_count, lines.length
21
+ end
22
+ end
23
+
24
+ def self.should_log_including(what)
25
+ should "log a line including #{what.inspect}" do
26
+ contents = File.read(TEST_LOG_PATH)
27
+ if what.is_a?(Regexp)
28
+ assert_match what, contents
29
+ else
30
+ assert contents.include?(what),
31
+ "Expected #{contents.inspect} to include #{what.inspect}"
32
+ end
33
+ end
34
+ end
35
+
36
+ def self.should_log_at_level(level)
37
+ level = level.to_s.upcase
38
+ should "log at the #{level} level" do
39
+ assert File.read(TEST_LOG_PATH).include?(" #{level} -- :")
40
+ end
41
+ end
42
+
43
+ def self.should_raise_exception(klass)
44
+ should "raise an #{klass} exception" do
45
+ assert @exception.is_a?(klass)
46
+ end
47
+ end
48
+
49
+ def self.should_have_config_afterwards_including(config)
50
+ config.each_pair do |key, value|
51
+ should "continue afterwards with Samuel.config[#{key.inspect}] set to #{value.inspect}" do
52
+ assert_equal value, Samuel.config[key]
53
+ end
54
+ end
55
+ end
56
+
57
+ def setup_test_logger
58
+ FileUtils.rm_rf TEST_LOG_PATH
59
+ FileUtils.touch TEST_LOG_PATH
60
+ Samuel.logger = Logger.new(TEST_LOG_PATH)
61
+ end
62
+
63
+ def teardown_test_logger
64
+ FileUtils.rm_rf TEST_LOG_PATH
65
+ end
66
+ end
@@ -0,0 +1,32 @@
1
+ require 'test_helper'
2
+
3
+ class ThreadTest < Test::Unit::TestCase
4
+
5
+ context "when logging multiple requests at once" do
6
+ setup do
7
+ @log = StringIO.new
8
+ Samuel.logger = Logger.new(@log)
9
+ FakeWeb.register_uri(:get, /example\.com/, :status => [200, "OK"])
10
+ threads = []
11
+ 5.times do |i|
12
+ threads << Thread.new(i) do |n|
13
+ Samuel.with_config :label => "Example #{n}" do
14
+ Thread.pass
15
+ open "http://example.com/#{n}"
16
+ end
17
+ end
18
+ end
19
+ threads.each { |t| t.join }
20
+ @log.rewind
21
+ end
22
+
23
+ should "not let configuration blocks interfere with eachother" do
24
+ @log.each_line do |line|
25
+ matches = %r|Example (\d+).*example\.com/(\d+)|.match(line)
26
+ assert_not_nil matches
27
+ assert_equal matches[1], matches[2]
28
+ end
29
+ end
30
+ end
31
+
32
+ end
@@ -0,0 +1,492 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ include WebMock
4
+
5
+ SAMPLE_HEADERS = { "Content-Length" => "8888" }
6
+
7
+ describe "WebMock", :shared => true do
8
+ before(:each) do
9
+ WebMock.disable_net_connect!
10
+ RequestRegistry.instance.reset_webmock
11
+ end
12
+
13
+ describe "when web connect" do
14
+
15
+ describe "is allowed" do
16
+ before(:each) do
17
+ WebMock.allow_net_connect!
18
+ end
19
+
20
+ it "should make a real web request if request is not stubbed" do
21
+ setup_expectations_for_real_google_request
22
+ http_request(:get, "http://www.google.com/").
23
+ body.should =~ /.*Google fake response.*/
24
+ end
25
+
26
+ it "should return stubbed response if request was stubbed" do
27
+ stub_http_request(:get, "www.google.com").to_return(:body => "abc")
28
+ http_request(:get, "http://www.google.com/").body.should == "abc"
29
+ end
30
+ end
31
+
32
+ describe "is not allowed" do
33
+ before(:each) do
34
+ WebMock.disable_net_connect!
35
+ end
36
+
37
+ it "should return stubbed response if request was stubbed" do
38
+ stub_http_request(:get, "www.google.com").to_return(:body => "abc")
39
+ http_request(:get, "http://www.google.com/").body.should == "abc"
40
+ end
41
+
42
+ it "should raise exception if request was not stubbed" do
43
+ lambda {
44
+ http_request(:get, "http://www.google.com/")
45
+ }.should raise_error(WebMock::NetConnectNotAllowedError,
46
+ "Real HTTP connections are disabled. Unregistered request: GET http://www.google.com/")
47
+ end
48
+ end
49
+
50
+ end
51
+
52
+ describe "when matching requests" do
53
+
54
+ describe "on method" do
55
+
56
+ it "should match the request by method if registered" do
57
+ stub_http_request(:get, "www.google.com")
58
+ http_request(:get, "http://www.google.com/").status.should == "200"
59
+ end
60
+
61
+ it "should not match requests if method is different" do
62
+ stub_http_request(:get, "www.google.com")
63
+ http_request(:get, "http://www.google.com/").status.should == "200"
64
+ lambda {
65
+ http_request(:post, "http://www.google.com/")
66
+ }.should raise_error(WebMock::NetConnectNotAllowedError,
67
+ "Real HTTP connections are disabled. Unregistered request: POST http://www.google.com/"
68
+ )
69
+ end
70
+
71
+ end
72
+
73
+ describe "on body" do
74
+
75
+ it "should match requests if body is the same" do
76
+ stub_http_request(:get, "www.google.com").with(:body => "abc")
77
+ http_request(
78
+ :get, "http://www.google.com/",
79
+ :body => "abc").status.should == "200"
80
+ end
81
+
82
+ it "should match requests if body is not set in the stub" do
83
+ stub_http_request(:get, "www.google.com")
84
+ http_request(
85
+ :get, "http://www.google.com/",
86
+ :body => "abc").status.should == "200"
87
+ end
88
+
89
+ it "should not match requests if body is different" do
90
+ stub_http_request(:get, "www.google.com").with(:body => "abc")
91
+
92
+ lambda {
93
+ http_request(:get, "http://www.google.com/", :body => "def")
94
+ }.should raise_error(WebMock::NetConnectNotAllowedError,
95
+ "Real HTTP connections are disabled. Unregistered request: GET http://www.google.com/ with body 'def'")
96
+ end
97
+
98
+ end
99
+
100
+ describe "on headers" do
101
+
102
+ it "should match requests if headers are the same" do
103
+ stub_http_request(:get, "www.google.com").with(:headers => SAMPLE_HEADERS )
104
+ http_request(
105
+ :get, "http://www.google.com/",
106
+ :headers => SAMPLE_HEADERS).status.should == "200"
107
+ end
108
+
109
+ it "should match requests if request headers are not stubbed" do
110
+ stub_http_request(:get, "www.google.com")
111
+ http_request(
112
+ :get, "http://www.google.com/",
113
+ :headers => SAMPLE_HEADERS).status.should == "200"
114
+ end
115
+
116
+
117
+ it "should not match requests if headers are different" do
118
+ stub_http_request(:get, "www.google.com").with(:headers => SAMPLE_HEADERS)
119
+
120
+ lambda {
121
+ http_request(
122
+ :get, "http://www.google.com/",
123
+ :headers => { 'Content-Length' => '9999'})
124
+ }.should raise_error(WebMock::NetConnectNotAllowedError,
125
+ %q(Real HTTP connections are disabled. Unregistered request: GET http://www.google.com/ with headers {'Content-Length'=>'9999'}))
126
+ end
127
+ end
128
+
129
+ describe "with basic authentication" do
130
+
131
+ it "should match if credentials are the same" do
132
+ stub_http_request(:get, "user:pass@www.google.com")
133
+ http_request(:get, "http://user:pass@www.google.com/").status.should == "200"
134
+ end
135
+
136
+ it "should not match if credentials are different" do
137
+ stub_http_request(:get, "user:pass@www.google.com")
138
+ lambda {
139
+ http_request(:get, "http://user:pazz@www.google.com/").status.should == "200"
140
+ }.should raise_error(WebMock::NetConnectNotAllowedError,
141
+ %q(Real HTTP connections are disabled. Unregistered request: GET http://user:pazz@www.google.com/ with headers {'Authorization'=>'Basic dXNlcjpwYXp6'}))
142
+ end
143
+
144
+ it "should not match if credentials are stubbed but not provided in the request" do
145
+ stub_http_request(:get, "user:pass@www.google.com")
146
+ lambda {
147
+ http_request(:get, "http://www.google.com/").status.should == "200"
148
+ }.should raise_error(WebMock::NetConnectNotAllowedError,
149
+ %q(Real HTTP connections are disabled. Unregistered request: GET http://www.google.com/))
150
+ end
151
+
152
+ it "should not match if credentials are not stubbed but exist in the request" do
153
+ stub_http_request(:get, "www.google.com")
154
+ lambda {
155
+ http_request(:get, "http://user:pazz@www.google.com/").status.should == "200"
156
+ }.should raise_error(WebMock::NetConnectNotAllowedError,
157
+ %q(Real HTTP connections are disabled. Unregistered request: GET http://user:pazz@www.google.com/ with headers {'Authorization'=>'Basic dXNlcjpwYXp6'}))
158
+ end
159
+
160
+ end
161
+
162
+ end
163
+
164
+ describe "raising stubbed exceptions" do
165
+
166
+ it "should raise exception if declared in a stubbed response" do
167
+ class MyException < StandardError; end;
168
+ stub_http_request(:get, "www.google.com").to_raise(MyException)
169
+ lambda {
170
+ http_request(:get, "http://www.google.com/")
171
+ }.should raise_error(MyException, "Exception from WebMock")
172
+ end
173
+
174
+ end
175
+
176
+
177
+ describe "returning stubbed responses" do
178
+
179
+ it "should return declared body" do
180
+ stub_http_request(:get, "www.google.com").to_return(:body => "abc")
181
+ http_request(:get, "http://www.google.com/").body.should == "abc"
182
+ end
183
+
184
+ it "should return declared headers" do
185
+ stub_http_request(:get, "www.google.com").to_return(:headers => SAMPLE_HEADERS)
186
+ response = http_request(:get, "http://www.google.com/")
187
+ response.headers["Content-Length"].should == "8888"
188
+ end
189
+
190
+ it "should return declared status" do
191
+ stub_http_request(:get, "www.google.com").to_return(:status => 500)
192
+ http_request(:get, "http://www.google.com/").status.should == "500"
193
+ end
194
+
195
+ end
196
+
197
+
198
+ describe "precedence of stubs" do
199
+
200
+ it "should use the last declared matching request stub" do
201
+ stub_http_request(:get, "www.google.com").to_return(:body => "abc")
202
+ stub_http_request(:get, "www.google.com").to_return(:body => "def")
203
+ http_request(:get, "http://www.google.com/").body.should == "def"
204
+ end
205
+
206
+ it "should not be affected by the type of url or request method" do
207
+ stub_http_request(:get, "www.google.com").to_return(:body => "abc")
208
+ stub_http_request(:any, /.*google.*/).to_return(:body => "def")
209
+ http_request(:get, "http://www.google.com/").body.should == "def"
210
+ end
211
+
212
+ end
213
+
214
+ describe "verification of request expectation" do
215
+
216
+ describe "when net connect not allowed" do
217
+
218
+ before(:each) do
219
+ WebMock.disable_net_connect!
220
+ stub_http_request(:any, "http://www.google.com")
221
+ stub_http_request(:any, "https://www.google.com")
222
+ end
223
+
224
+ it "should pass if request was executed with the same url and method" do
225
+ lambda {
226
+ http_request(:get, "http://www.google.com/")
227
+ request(:get, "http://www.google.com").should have_been_made.once
228
+ }.should_not raise_error
229
+ end
230
+
231
+ it "should pass if request was not expected and not executed" do
232
+ lambda {
233
+ request(:get, "http://www.google.com").should have_not_been_made
234
+ }.should_not raise_error
235
+ end
236
+
237
+ it "should fail if request was not expected but executed" do
238
+ lambda {
239
+ http_request(:get, "http://www.google.com/")
240
+ request(:get, "http://www.google.com").should have_not_been_made
241
+ }.should fail_with("The request GET http://www.google.com/ was expected to execute 0 times but it executed 1 time")
242
+ end
243
+
244
+
245
+ it "should fail if request was not executed" do
246
+ lambda {
247
+ request(:get, "http://www.google.com").should have_been_made
248
+ }.should fail_with("The request GET http://www.google.com/ was expected to execute 1 time but it executed 0 times")
249
+ end
250
+
251
+ it "should fail if request was executed to different url" do
252
+ lambda {
253
+ http_request(:get, "http://www.google.com/")
254
+ request(:get, "http://www.google.org").should have_been_made
255
+ }.should fail_with("The request GET http://www.google.org/ was expected to execute 1 time but it executed 0 times")
256
+ end
257
+
258
+ it "should fail if request was executed with different method" do
259
+ lambda {
260
+ http_request(:post, "http://www.google.com/")
261
+ request(:get, "http://www.google.com").should have_been_made
262
+ }.should fail_with("The request GET http://www.google.com/ was expected to execute 1 time but it executed 0 times")
263
+ end
264
+
265
+ it "should_pass if request was executed with different form of url" do
266
+ lambda {
267
+ http_request(:get, "http://www.google.com/")
268
+ request(:get, "www.google.com").should have_been_made
269
+ }.should_not raise_error
270
+ end
271
+
272
+ it "should_pass if request was executed with different form of url without port " do
273
+ lambda {
274
+ http_request(:get, "http://www.google.com/")
275
+ request(:get, "www.google.com:80").should have_been_made
276
+ }.should_not raise_error
277
+ end
278
+
279
+ it "should_pass if request was executed with different form of url with port" do
280
+ lambda {
281
+ http_request(:get, "http://www.google.com/")
282
+ request(:get, "www.google.com:80").should have_been_made
283
+ }.should_not raise_error
284
+ end
285
+
286
+ it "should fail if request was executed with different port" do
287
+ lambda {
288
+ http_request(:get, "http://www.google.com:80/")
289
+ request(:get, "www.google.com:90").should have_been_made
290
+ }.should fail_with("The request GET http://www.google.com:90/ was expected to execute 1 time but it executed 0 times")
291
+ end
292
+
293
+ it "should_pass if request was executed with different form of url with https port" do
294
+ lambda {
295
+ http_request(:get, "https://www.google.com/")
296
+ request(:get, "https://www.google.com:443/").should have_been_made
297
+ }.should_not raise_error
298
+ end
299
+
300
+ it "should fail if requested more times than expected" do
301
+ lambda {
302
+ http_request(:get, "http://www.google.com/")
303
+ http_request(:get, "http://www.google.com/")
304
+ request(:get, "http://www.google.com").should have_been_made
305
+ }.should fail_with("The request GET http://www.google.com/ was expected to execute 1 time but it executed 2 times")
306
+ end
307
+
308
+ it "should fail if requested less times than expected" do
309
+ lambda {
310
+ http_request(:get, "http://www.google.com/")
311
+ request(:get, "http://www.google.com").should have_been_made.twice
312
+ }.should fail_with("The request GET http://www.google.com/ was expected to execute 2 times but it executed 1 time")
313
+ end
314
+
315
+ it "should fail if requested less times than expected when 3 times expected" do
316
+ lambda {
317
+ http_request(:get, "http://www.google.com/")
318
+ request(:get, "http://www.google.com").should have_been_made.times(3)
319
+ }.should fail_with("The request GET http://www.google.com/ was expected to execute 3 times but it executed 1 time")
320
+ end
321
+
322
+ it "should succeed if request was executed with the same body" do
323
+ lambda {
324
+ http_request(:get, "http://www.google.com/", :body => "abc")
325
+ request(:get, "www.google.com").with(:body => "abc").should have_been_made
326
+ }.should_not raise_error
327
+ end
328
+
329
+ it "should fail if request was executed with different body" do
330
+ lambda {
331
+ http_request(:get, "http://www.google.com/", :body => "abc")
332
+ request(:get, "www.google.com").
333
+ with(:body => "def").should have_been_made
334
+ }.should fail_with("The request GET http://www.google.com/ with body 'def' was expected to execute 1 time but it executed 0 times")
335
+ end
336
+
337
+ it "should succeed if request was executed with the same headers" do
338
+ lambda {
339
+ http_request(:get, "http://www.google.com/", :headers => SAMPLE_HEADERS)
340
+ request(:get, "www.google.com").
341
+ with(:headers => SAMPLE_HEADERS).should have_been_made
342
+ }.should_not raise_error
343
+ end
344
+
345
+ it "should fail if request was executed with different headers" do
346
+ lambda {
347
+ http_request(:get, "http://www.google.com/", :headers => SAMPLE_HEADERS)
348
+ request(:get, "www.google.com").
349
+ with(:headers => { 'Content-Length' => '9999'}).should have_been_made
350
+ }.should fail_with("The request GET http://www.google.com/ with headers {'Content-Length'=>'9999'} was expected to execute 1 time but it executed 0 times")
351
+ end
352
+
353
+ it "should fail if request was executed with less headers" do
354
+ lambda {
355
+ http_request(:get, "http://www.google.com/", :headers => {'A' => 'a'})
356
+ request(:get, "www.google.com").
357
+ with(:headers => {'A' => 'a', 'B' => 'b'}).should have_been_made
358
+ }.should fail_with("The request GET http://www.google.com/ with headers {'A'=>'a', 'B'=>'b'} was expected to execute 1 time but it executed 0 times")
359
+ end
360
+
361
+ it "should succeed if request was executed with more headers" do
362
+ lambda {
363
+ http_request(:get, "http://www.google.com/",
364
+ :headers => {'A' => 'a', 'B' => 'b'}
365
+ )
366
+ request(:get, "www.google.com").
367
+ with(:headers => {'A' => 'a'}).should have_been_made
368
+ }.should_not raise_error
369
+ end
370
+
371
+ it "should succeed if request was executed with body and headers but they were not specified in expectantion" do
372
+ lambda {
373
+ http_request(:get, "http://www.google.com/",
374
+ :body => "abc",
375
+ :headers => SAMPLE_HEADERS
376
+ )
377
+ request(:get, "www.google.com").should have_been_made
378
+ }.should_not raise_error
379
+ end
380
+
381
+
382
+ describe "with authentication" do
383
+ before(:each) do
384
+ stub_http_request(:any, "http://user:pass@www.google.com")
385
+ stub_http_request(:any, "http://user:pazz@www.google.com")
386
+ end
387
+
388
+ it "should succeed if succeed if request was executed with expected credentials" do
389
+ lambda {
390
+ http_request(:get, "http://user:pass@www.google.com/")
391
+ request(:get, "http://user:pass@www.google.com").should have_been_made.once
392
+ }.should_not raise_error
393
+ end
394
+
395
+ it "should fail if request was executed with different credentials than expected" do
396
+ lambda {
397
+ http_request(:get, "http://user:pass@www.google.com/")
398
+ request(:get, "http://user:pazz@www.google.com").should have_been_made.once
399
+ }.should fail_with("The request GET http://user:pazz@www.google.com/ was expected to execute 1 time but it executed 0 times")
400
+ end
401
+
402
+ it "should fail if request was executed without credentials but credentials were expected" do
403
+ lambda {
404
+ http_request(:get, "http://www.google.com/")
405
+ request(:get, "http://user:pass@www.google.com").should have_been_made.once
406
+ }.should fail_with("The request GET http://user:pass@www.google.com/ was expected to execute 1 time but it executed 0 times")
407
+ end
408
+
409
+ it "should fail if request was executed with credentials but expected without" do
410
+ lambda {
411
+ http_request(:get, "http://user:pass@www.google.com/")
412
+ request(:get, "http://www.google.com").should have_been_made.once
413
+ }.should fail_with("The request GET http://www.google.com/ was expected to execute 1 time but it executed 0 times")
414
+ end
415
+
416
+ end
417
+
418
+ describe "using webmock matcher" do
419
+
420
+ it "should verify expected requests occured" do
421
+ lambda {
422
+ http_request(:get, "http://www.google.com/")
423
+ WebMock.should have_requested(:get, "http://www.google.com").once
424
+ }.should_not raise_error
425
+ end
426
+
427
+ it "should verify expected requests occured" do
428
+ lambda {
429
+ http_request(:get, "http://www.google.com/", :body => "abc", :headers => {'A' => 'a'})
430
+ WebMock.should have_requested(:get, "http://www.google.com").with(:body => "abc", :headers => {'A' => 'a'}).once
431
+ }.should_not raise_error
432
+ end
433
+
434
+ it "should verify that non expected requests didn't occur" do
435
+ lambda {
436
+ http_request(:get, "http://www.google.com/")
437
+ WebMock.should have_not_requested(:get, "http://www.google.com")
438
+ }.should fail_with("The request GET http://www.google.com/ was expected to execute 0 times but it executed 1 time")
439
+ end
440
+ end
441
+
442
+ describe "using assert_requested" do
443
+
444
+ it "should verify expected requests occured" do
445
+ lambda {
446
+ http_request(:get, "http://www.google.com/")
447
+ assert_requested(:get, "http://www.google.com", :times => 1)
448
+ assert_requested(:get, "http://www.google.com")
449
+ }.should_not raise_error
450
+ end
451
+
452
+ it "should verify expected requests occured" do
453
+ lambda {
454
+ http_request(:get, "http://www.google.com/", :body => "abc", :headers => {'A' => 'a'})
455
+ assert_requested(:get, "http://www.google.com", :body => "abc", :headers => {'A' => 'a'})
456
+ }.should_not raise_error
457
+ end
458
+
459
+ it "should verify that non expected requests didn't occur" do
460
+ lambda {
461
+ http_request(:get, "http://www.google.com/")
462
+ assert_not_requested(:get, "http://www.google.com")
463
+ }.should fail_with("The request GET http://www.google.com/ was expected to execute 0 times but it executed 1 time")
464
+ end
465
+ end
466
+ end
467
+
468
+
469
+ describe "when net connect allowed" do
470
+ before(:each) do
471
+ WebMock.allow_net_connect!
472
+ end
473
+
474
+ it "should verify expected requests occured" do
475
+ setup_expectations_for_real_google_request
476
+ lambda {
477
+ http_request(:get, "http://www.google.com/")
478
+ request(:get, "http://www.google.com").should have_been_made
479
+ }.should_not raise_error
480
+ end
481
+
482
+ it "should verify that non expected requests didn't occur" do
483
+ lambda {
484
+ http_request(:get, "http://www.google.com/")
485
+ request(:get, "http://www.google.com").should have_not_been_made
486
+ }.should fail_with("The request GET http://www.google.com/ was expected to execute 0 times but it executed 1 time")
487
+ end
488
+ end
489
+
490
+ end
491
+
492
+ end