webmock 1.6.4 → 1.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (95) hide show
  1. data/.gemtest +0 -0
  2. data/.gitignore +3 -1
  3. data/.travis.yml +6 -0
  4. data/CHANGELOG.md +211 -118
  5. data/Gemfile +16 -1
  6. data/Guardfile +24 -0
  7. data/LICENSE +1 -1
  8. data/README.md +64 -15
  9. data/Rakefile +19 -6
  10. data/lib/webmock.rb +9 -4
  11. data/lib/webmock/api.rb +5 -4
  12. data/lib/webmock/assertion_failure.rb +1 -1
  13. data/lib/webmock/callback_registry.rb +1 -1
  14. data/lib/webmock/config.rb +2 -2
  15. data/lib/webmock/cucumber.rb +1 -1
  16. data/lib/webmock/errors.rb +17 -5
  17. data/lib/webmock/http_lib_adapters/{curb.rb → curb_adapter.rb} +79 -49
  18. data/lib/webmock/http_lib_adapters/{em_http_request.rb → em_http_request/em_http_request_0_x.rb} +20 -15
  19. data/lib/webmock/http_lib_adapters/em_http_request/em_http_request_1_x.rb +201 -0
  20. data/lib/webmock/http_lib_adapters/em_http_request_adapter.rb +11 -0
  21. data/lib/webmock/http_lib_adapters/http_lib_adapter.rb +7 -0
  22. data/lib/webmock/http_lib_adapters/http_lib_adapter_registry.rb +19 -0
  23. data/lib/webmock/http_lib_adapters/{httpclient.rb → httpclient_adapter.rb} +35 -8
  24. data/lib/webmock/http_lib_adapters/net_http.rb +84 -25
  25. data/lib/webmock/http_lib_adapters/net_http_response.rb +17 -17
  26. data/lib/webmock/http_lib_adapters/patron_adapter.rb +124 -0
  27. data/lib/webmock/http_lib_adapters/typhoeus_hydra_adapter.rb +166 -0
  28. data/lib/webmock/minitest.rb +15 -0
  29. data/lib/webmock/rack_response.rb +52 -0
  30. data/lib/webmock/request_pattern.rb +4 -2
  31. data/lib/webmock/request_signature.rb +4 -0
  32. data/lib/webmock/request_stub.rb +35 -2
  33. data/lib/webmock/responses_sequence.rb +2 -2
  34. data/lib/webmock/rspec.rb +2 -2
  35. data/lib/webmock/rspec/matchers.rb +9 -4
  36. data/lib/webmock/rspec/matchers/webmock_matcher.rb +1 -1
  37. data/lib/webmock/stub_registry.rb +1 -1
  38. data/lib/webmock/stub_request_snippet.rb +14 -11
  39. data/lib/webmock/util/hash_keys_stringifier.rb +4 -4
  40. data/lib/webmock/util/headers.rb +3 -3
  41. data/lib/webmock/util/json.rb +54 -0
  42. data/lib/webmock/util/uri.rb +1 -1
  43. data/lib/webmock/version.rb +1 -1
  44. data/lib/webmock/webmock.rb +20 -3
  45. data/minitest/test_helper.rb +29 -0
  46. data/minitest/test_webmock.rb +6 -0
  47. data/minitest/webmock_spec.rb +30 -0
  48. data/spec/curb_spec.rb +26 -8
  49. data/spec/curb_spec_helper.rb +6 -6
  50. data/spec/em_http_request_spec.rb +95 -1
  51. data/spec/em_http_request_spec_helper.rb +16 -16
  52. data/spec/errors_spec.rb +19 -4
  53. data/spec/example_curl_output.txt +22 -22
  54. data/spec/http_lib_adapters/http_lib_adapter_registry_spec.rb +17 -0
  55. data/spec/http_lib_adapters/http_lib_adapter_spec.rb +12 -0
  56. data/spec/httpclient_spec.rb +1 -1
  57. data/spec/httpclient_spec_helper.rb +3 -38
  58. data/spec/my_rack_app.rb +18 -0
  59. data/spec/net_http_shared.rb +125 -0
  60. data/spec/net_http_spec.rb +27 -31
  61. data/spec/net_http_spec_helper.rb +4 -34
  62. data/spec/network_connection.rb +1 -1
  63. data/spec/patron_spec_helper.rb +4 -7
  64. data/spec/quality_spec.rb +60 -0
  65. data/spec/rack_response_spec.rb +33 -0
  66. data/spec/real_net_http_spec.rb +20 -0
  67. data/spec/request_execution_verifier_spec.rb +8 -8
  68. data/spec/request_pattern_spec.rb +3 -3
  69. data/spec/request_stub_spec.rb +19 -19
  70. data/spec/response_spec.rb +8 -8
  71. data/spec/spec_helper.rb +14 -11
  72. data/spec/stub_request_snippet_spec.rb +85 -37
  73. data/spec/support/webmock_server.rb +62 -0
  74. data/spec/typhoeus_hydra_spec.rb +53 -0
  75. data/spec/typhoeus_hydra_spec_helper.rb +50 -0
  76. data/spec/util/headers_spec.rb +5 -5
  77. data/spec/util/json_spec.rb +7 -0
  78. data/spec/util/uri_spec.rb +1 -1
  79. data/spec/vendor/addressable/lib/uri.rb +1 -0
  80. data/spec/vendor/crack/lib/crack.rb +1 -0
  81. data/spec/vendor/right_http_connection-1.2.4/History.txt +4 -4
  82. data/spec/vendor/right_http_connection-1.2.4/README.txt +4 -4
  83. data/spec/vendor/right_http_connection-1.2.4/Rakefile +3 -3
  84. data/spec/vendor/right_http_connection-1.2.4/lib/net_fix.rb +4 -4
  85. data/spec/vendor/right_http_connection-1.2.4/setup.rb +4 -4
  86. data/spec/webmock_shared.rb +375 -143
  87. data/spec/webmock_spec.rb +7 -0
  88. data/test/http_request.rb +24 -0
  89. data/test/shared_test.rb +47 -0
  90. data/test/test_helper.rb +6 -3
  91. data/test/test_webmock.rb +2 -67
  92. data/webmock.gemspec +8 -7
  93. metadata +153 -88
  94. data/lib/webmock/http_lib_adapters/patron.rb +0 -100
  95. data/spec/other_net_http_libs_spec.rb +0 -30
@@ -171,7 +171,7 @@ describe WebMock::Util::URI do
171
171
  uri = 'http://www.example.com:80/path?a[]=b&a[]=c'
172
172
  lambda { WebMock::Util::URI.normalize_uri(uri) }.should_not raise_error(ArgumentError)
173
173
  end
174
-
174
+
175
175
  end
176
176
 
177
177
  describe "stripping default port" do
@@ -0,0 +1 @@
1
+
@@ -0,0 +1 @@
1
+
@@ -26,8 +26,8 @@ Initial public release
26
26
  == 1.2.0 2007-10-05
27
27
 
28
28
  * r1867, konstantin, 2007-10-05 06:19:45
29
- * # 220, (re)open connection to server if none exists or connection params
30
- have changed
29
+ * # 220, (re)open connection to server if none exists or connection params
30
+ have changed
31
31
 
32
32
  == 1.2.1
33
33
 
@@ -37,7 +37,7 @@ Initial public release
37
37
 
38
38
  * r2764, konstantin, 02-08-08 00:05:00 +03:00
39
39
  * "RightAws: incompatible Net::HTTP monkey-patch" exception is raised if our net_fix
40
- patch was overriden (by attachment_fu for example, to avoid this load attachment_fu
40
+ patch was overriden (by attachment_fu for example, to avoid this load attachment_fu
41
41
  before loading the right_http_connection gem).
42
42
 
43
43
  == 1.2.2
@@ -51,7 +51,7 @@ Initial public release
51
51
  - Improve handling of data streams during upload: if there is a failure and a retry, reset
52
52
  the seek pointer for the subsequent re-request
53
53
 
54
- == 1.2.4
54
+ == 1.2.4
55
55
 
56
56
  * r4984, konstantin, 2008-08-11 14:49:18 +0400
57
57
  * fixed a bug: <NoMethodError: You have a nil object when you didn't expect it!
@@ -1,6 +1,6 @@
1
1
  RightScale::HttpConnection
2
2
  by RightScale, Inc.
3
- www.RightScale.com
3
+ www.RightScale.com
4
4
 
5
5
  == DESCRIPTION:
6
6
 
@@ -12,7 +12,7 @@ algorithm for low-level network errors.
12
12
  - provides put/get streaming
13
13
  - does configurable retries on connect and read timeouts, DNS failures, etc.
14
14
  - HTTPS certificate checking
15
-
15
+
16
16
  == SYNOPSIS:
17
17
 
18
18
 
@@ -20,7 +20,7 @@ algorithm for low-level network errors.
20
20
 
21
21
  - 2/11/08: If you use RightScale::HttpConnection in conjunction with attachment_fu, the
22
22
  HttpConnection gem must be included (using the require statement) AFTER
23
- attachment_fu.
23
+ attachment_fu.
24
24
  This is due to a conflict between the HttpConnection gem and another
25
25
  gem required by attachment_fu.
26
26
 
@@ -32,7 +32,7 @@ sudo gem install right_http_connection
32
32
 
33
33
  == LICENSE:
34
34
 
35
- Copyright (c) 2007-2008 RightScale, Inc.
35
+ Copyright (c) 2007-2008 RightScale, Inc.
36
36
 
37
37
  Permission is hereby granted, free of charge, to any person obtaining
38
38
  a copy of this software and associated documentation files (the
@@ -25,7 +25,7 @@ VERS = RightHttpConnection::VERSION::STRING + (REV ? ".#{REV}" : "")
25
25
  CLEAN.include ['**/.*.sw?', '*.gem', '.config', '**/.DS_Store']
26
26
  RDOC_OPTS = ['--quiet', '--title', 'right_http_connection documentation',
27
27
  "--opname", "index.html",
28
- "--line-numbers",
28
+ "--line-numbers",
29
29
  "--main", "README",
30
30
  "--inline-source"]
31
31
 
@@ -51,7 +51,7 @@ end
51
51
  # Generate all the Rake tasks
52
52
  # Run 'rake -T' to see list of generated tasks (from gem root directory)
53
53
  hoe = Hoe.new(GEM_NAME, VERS) do |p|
54
- p.author = AUTHOR
54
+ p.author = AUTHOR
55
55
  p.description = DESCRIPTION
56
56
  p.email = EMAIL
57
57
  p.summary = DESCRIPTION
@@ -60,7 +60,7 @@ hoe = Hoe.new(GEM_NAME, VERS) do |p|
60
60
  p.test_globs = ["test/**/test_*.rb"]
61
61
  p.clean_globs = CLEAN #An array of file patterns to delete on clean.
62
62
  p.remote_rdoc_dir = "right_http_gem_doc"
63
-
63
+
64
64
  # == Optional
65
65
  p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
66
66
  #p.extra_deps = [] # An array of rubygem dependencies [name, version], e.g. [ ['active_support', '>= 1.3.1'] ]
@@ -22,12 +22,12 @@
22
22
  #
23
23
  #
24
24
 
25
- # Net::HTTP and Net::HTTPGenericRequest fixes to support 100-continue on
25
+ # Net::HTTP and Net::HTTPGenericRequest fixes to support 100-continue on
26
26
  # POST and PUT. The request must have 'expect' field set to '100-continue'.
27
27
 
28
28
 
29
29
  module Net
30
-
30
+
31
31
  class BufferedIO #:nodoc:
32
32
  # Monkey-patch Net::BufferedIO to read > 1024 bytes from the socket at a time
33
33
 
@@ -117,12 +117,12 @@ module Net
117
117
  end
118
118
  end
119
119
  end
120
- end
120
+ end
121
121
  end
122
122
 
123
123
 
124
124
  #-- Net::HTTP --
125
-
125
+
126
126
  class HTTP
127
127
  def request(req, body = nil, &block) # :yield: +response+
128
128
  unless started?
@@ -659,7 +659,7 @@ module FileOperations
659
659
  def ruby(*args)
660
660
  command config('rubyprog'), *args
661
661
  end
662
-
662
+
663
663
  def make(task = nil)
664
664
  command(*[config('makeprog'), task].compact)
665
665
  end
@@ -722,7 +722,7 @@ module HookScriptAPI
722
722
  def srcdirectory?(path)
723
723
  File.dir?(srcfile(path))
724
724
  end
725
-
725
+
726
726
  def srcfile?(path)
727
727
  File.file?(srcfile(path))
728
728
  end
@@ -826,7 +826,7 @@ class ToplevelInstaller
826
826
  __send__ "exec_#{task}"
827
827
  end
828
828
  end
829
-
829
+
830
830
  def run_metaconfigs
831
831
  @config.load_script "#{@ardir}/metaconfig"
832
832
  end
@@ -1404,7 +1404,7 @@ class Installer
1404
1404
  end
1405
1405
 
1406
1406
  # picked up many entries from cvs-1.11.1/src/ignore.c
1407
- JUNK_FILES = %w(
1407
+ JUNK_FILES = %w(
1408
1408
  core RCSLOG tags TAGS .make.state
1409
1409
  .nse_depinfo #* .#* cvslog.* ,* .del-* *.olb
1410
1410
  *~ *.old *.bak *.BAK *.orig *.rej _$* *$
@@ -4,18 +4,12 @@ unless defined? SAMPLE_HEADERS
4
4
  SAMPLE_HEADERS = { "Content-Length" => "8888", "Accept" => "application/json" }
5
5
  ESCAPED_PARAMS = "x=ab%20c&z=%27Stop%21%27%20said%20Fred"
6
6
  NOT_ESCAPED_PARAMS = "z='Stop!' said Fred&x=ab c"
7
- WWW_EXAMPLE_COM_CONTENT_LENGTH = 0
8
7
  end
9
8
 
10
9
  class MyException < StandardError; end;
11
10
 
12
- describe "WebMock version" do
13
- it "should report version" do
14
- WebMock.version.should == WebMock::VERSION
15
- end
16
- end
17
-
18
11
  shared_examples_for "WebMock" do
12
+ let(:webmock_server_url) {"http://#{WebMockServer.instance.host_with_port}/"}
19
13
  before(:each) do
20
14
  WebMock.disable_net_connect!
21
15
  WebMock.reset!
@@ -29,21 +23,14 @@ shared_examples_for "WebMock" do
29
23
  end
30
24
 
31
25
  it "should make a real web request if request is not stubbed" do
32
- setup_expectations_for_real_example_com_request
33
- http_request(:get, "http://www.example.com/").status.should == "302"
26
+ http_request(:get, webmock_server_url).status.should == "200"
34
27
  end
35
28
 
36
29
  it "should make a real https request if request is not stubbed" do
37
- setup_expectations_for_real_example_com_request(
38
- :host => "www.paypal.com",
39
- :port => 443,
40
- :path => "/uk/cgi-bin/webscr",
41
- :response_code => 200,
42
- :response_message => "OK",
43
- :response_body => "hello paypal"
44
- )
45
- http_request(:get, "https://www.paypal.com/uk/cgi-bin/webscr").
46
- body.should =~ /.*paypal.*/
30
+ unless http_library == :httpclient
31
+ http_request(:get, "https://www.paypal.com/uk/cgi-bin/webscr").
32
+ body.should =~ /.*paypal.*/
33
+ end
47
34
  end
48
35
 
49
36
  it "should return stubbed response if request was stubbed" do
@@ -61,7 +48,7 @@ shared_examples_for "WebMock" do
61
48
  stub_http_request(:get, "www.example.com").to_return(:body => "abc")
62
49
  http_request(:get, "http://www.example.com/").body.should == "abc"
63
50
  end
64
-
51
+
65
52
  it "should return stubbed response if request with path was stubbed" do
66
53
  stub_http_request(:get, "www.example.com/hello_world").to_return(:body => "abc")
67
54
  http_request(:get, "http://www.example.com/hello_world").body.should == "abc"
@@ -108,25 +95,64 @@ shared_examples_for "WebMock" do
108
95
  }.should raise_error(connection_refused_exception_class)
109
96
  end
110
97
  end
111
-
98
+
112
99
  describe "is not allowed with exception for allowed domains" do
100
+ let(:host_with_port){ WebMockServer.instance.host_with_port }
101
+
113
102
  before(:each) do
114
- WebMock.disable_net_connect!(:allow => ["www.example.org"])
103
+ WebMock.disable_net_connect!(:allow => ["www.example.org", host_with_port])
115
104
  end
116
105
 
117
- it "should return stubbed response if request was stubbed" do
118
- stub_http_request(:get, "www.example.com").to_return(:body => "abc")
119
- http_request(:get, "http://www.example.com/").body.should == "abc"
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
120
117
  end
121
118
 
122
- it "should raise exception if request was not stubbed" do
123
- lambda {
124
- http_request(:get, "http://www.example.com/")
125
- }.should raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/))
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
126
130
  end
127
131
 
128
- it "should allow a real request to allowed host", :net_connect => true do
129
- http_request(:get, "http://www.example.org/").status.should == "302"
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
130
156
  end
131
157
  end
132
158
  end
@@ -149,26 +175,26 @@ shared_examples_for "WebMock" do
149
175
  stub_http_request(:get, /.*x=ab c.*/).to_return(:body => "abc")
150
176
  http_request(:get, "http://www.example.com/hello/?#{ESCAPED_PARAMS}").body.should == "abc"
151
177
  end
152
-
178
+
153
179
  end
154
180
 
155
181
  describe "on query params" do
156
-
182
+
157
183
  it "should match the request by query params declared as a hash" do
158
184
  stub_http_request(:get, "www.example.com").with(:query => {"a" => ["b", "c"]}).to_return(:body => "abc")
159
185
  http_request(:get, "http://www.example.com/?a[]=b&a[]=c").body.should == "abc"
160
186
  end
161
-
187
+
162
188
  it "should match the request by query declared as a string" do
163
189
  stub_http_request(:get, "www.example.com").with(:query => "a[]=b&a[]=c").to_return(:body => "abc")
164
190
  http_request(:get, "http://www.example.com/?a[]=b&a[]=c").body.should == "abc"
165
191
  end
166
-
192
+
167
193
  it "should match the request by query params declared both in uri and query option" do
168
194
  stub_http_request(:get, "www.example.com/?x=3").with(:query => {"a" => ["b", "c"]}).to_return(:body => "abc")
169
195
  http_request(:get, "http://www.example.com/?x=3&a[]=b&a[]=c").body.should == "abc"
170
196
  end
171
-
197
+
172
198
  end
173
199
 
174
200
  describe "on method" do
@@ -229,27 +255,27 @@ shared_examples_for "WebMock" do
229
255
  end
230
256
 
231
257
  end
232
-
233
- describe "when body is declared as a hash" do
258
+
259
+ describe "when body is declared as a hash" do
234
260
  before(:each) do
235
261
  stub_http_request(:post, "www.example.com").
236
262
  with(:body => {:a => '1', :b => 'five', 'c' => {'d' => ['e', 'f']} })
237
263
  end
238
-
264
+
239
265
  describe "for request with url encoded body" do
240
-
266
+
241
267
  it "should match request if hash matches body" do
242
268
  http_request(
243
269
  :post, "http://www.example.com/",
244
270
  :body => 'a=1&c[d][]=e&c[d][]=f&b=five').status.should == "200"
245
271
  end
246
-
272
+
247
273
  it "should match request if hash matches body in different order of params" do
248
274
  http_request(
249
275
  :post, "http://www.example.com/",
250
276
  :body => 'a=1&c[d][]=e&b=five&c[d][]=f').status.should == "200"
251
277
  end
252
-
278
+
253
279
  it "should not match if hash doesn't match url encoded body" do
254
280
  lambda {
255
281
  http_request(
@@ -257,47 +283,64 @@ shared_examples_for "WebMock" do
257
283
  :body => 'c[d][]=f&a=1&c[d][]=e').status.should == "200"
258
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'))
259
285
  end
260
-
286
+
261
287
  end
262
-
263
-
288
+
289
+
264
290
  describe "for request with json body and content type is set to json" do
265
-
291
+
266
292
  it "should match if hash matches body" do
267
293
  http_request(
268
294
  :post, "http://www.example.com/", :headers => {'Content-Type' => 'application/json'},
269
295
  :body => "{\"a\":\"1\",\"c\":{\"d\":[\"e\",\"f\"]},\"b\":\"five\"}").status.should == "200"
270
296
  end
271
-
297
+
272
298
  it "should match if hash matches body in different form" do
273
299
  http_request(
274
300
  :post, "http://www.example.com/", :headers => {'Content-Type' => 'application/json'},
275
301
  :body => "{\"a\":\"1\",\"b\":\"five\",\"c\":{\"d\":[\"e\",\"f\"]}}").status.should == "200"
276
302
  end
277
-
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
278
312
  end
279
-
313
+
280
314
  describe "for request with xml body and content type is set to xml" do
281
315
  before(:each) do
282
316
  WebMock.reset!
283
317
  stub_http_request(:post, "www.example.com").
284
318
  with(:body => { "opt" => {:a => '1', :b => 'five', 'c' => {'d' => ['e', 'f']} }})
285
319
  end
286
-
320
+
287
321
  it "should match if hash matches body" do
288
322
  http_request(
289
- :post, "http://www.example.com/", :headers => {'Content-Type' => 'application/xml'},
323
+ :post, "http://www.example.com/", :headers => {'Content-Type' => 'application/xml'},
290
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"
291
325
  end
292
-
326
+
293
327
  it "should match if hash matches body in different form" do
294
328
  http_request(
295
- :post, "http://www.example.com/", :headers => {'Content-Type' => 'application/xml'},
329
+ :post, "http://www.example.com/", :headers => {'Content-Type' => 'application/xml'},
296
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"
297
331
  end
298
-
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
+
299
342
  end
300
-
343
+
301
344
  end
302
345
 
303
346
  end
@@ -310,30 +353,30 @@ shared_examples_for "WebMock" do
310
353
  :get, "http://www.example.com/",
311
354
  :headers => SAMPLE_HEADERS).status.should == "200"
312
355
  end
313
-
356
+
314
357
  it "should match requests if headers are the same and declared as array" do
315
358
  stub_http_request(:get, "www.example.com").with(:headers => {"a" => ["b"]} )
316
359
  http_request(
317
360
  :get, "http://www.example.com/",
318
361
  :headers => {"a" => "b"}).status.should == "200"
319
362
  end
320
-
363
+
321
364
  describe "when multiple headers with the same key are used" do
322
-
365
+
323
366
  it "should match requests if headers are the same" do
324
367
  stub_http_request(:get, "www.example.com").with(:headers => {"a" => ["b", "c"]} )
325
368
  http_request(
326
369
  :get, "http://www.example.com/",
327
370
  :headers => {"a" => ["b", "c"]}).status.should == "200"
328
371
  end
329
-
372
+
330
373
  it "should match requests if headers are the same but in different order" do
331
374
  stub_http_request(:get, "www.example.com").with(:headers => {"a" => ["b", "c"]} )
332
375
  http_request(
333
376
  :get, "http://www.example.com/",
334
377
  :headers => {"a" => ["c", "b"]}).status.should == "200"
335
378
  end
336
-
379
+
337
380
  it "should not match requests if headers are different" do
338
381
  stub_http_request(:get, "www.example.com").with(:headers => {"a" => ["b", "c"]})
339
382
 
@@ -343,7 +386,7 @@ shared_examples_for "WebMock" do
343
386
  :headers => {"a" => ["b", "d"]})
344
387
  }.should raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/ with headers))
345
388
  end
346
-
389
+
347
390
  end
348
391
 
349
392
  it "should match requests if request headers are not stubbed" do
@@ -376,19 +419,19 @@ shared_examples_for "WebMock" do
376
419
  describe "with regular expressions" do
377
420
 
378
421
  it "should match requests if header values match regular expression" do
379
- stub_http_request(:get, "www.example.com").with(:headers => { :user_agent => /^MyAppName$/ })
422
+ stub_http_request(:get, "www.example.com").with(:headers => { :some_header => /^MyAppName$/ })
380
423
  http_request(
381
424
  :get, "http://www.example.com/",
382
- :headers => { 'user-agent' => 'MyAppName' }).status.should == "200"
425
+ :headers => { 'some-header' => 'MyAppName' }).status.should == "200"
383
426
  end
384
427
 
385
428
  it "should not match requests if headers values do not match regular expression" do
386
- stub_http_request(:get, "www.example.com").with(:headers => { :user_agent => /^MyAppName$/ })
429
+ stub_http_request(:get, "www.example.com").with(:headers => { :some_header => /^MyAppName$/ })
387
430
 
388
431
  lambda {
389
432
  http_request(
390
433
  :get, "http://www.example.com/",
391
- :headers => { 'user-agent' => 'xMyAppName' })
434
+ :headers => { 'some-header' => 'xMyAppName' })
392
435
  }.should raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/ with headers))
393
436
  end
394
437
 
@@ -461,14 +504,14 @@ shared_examples_for "WebMock" do
461
504
  http_request(:get, "http://www.example.com/")
462
505
  }.should raise_error(MyException, "Exception from WebMock")
463
506
  end
464
-
507
+
465
508
  it "should raise exception if declared in a stubbed response as exception instance" do
466
509
  stub_http_request(:get, "www.example.com").to_raise(MyException.new("hello world"))
467
510
  lambda {
468
511
  http_request(:get, "http://www.example.com/")
469
512
  }.should raise_error(MyException, "hello world")
470
513
  end
471
-
514
+
472
515
  it "should raise exception if declared in a stubbed response as exception instance" do
473
516
  stub_http_request(:get, "www.example.com").to_raise("hello world")
474
517
  lambda {
@@ -488,7 +531,7 @@ shared_examples_for "WebMock" do
488
531
 
489
532
 
490
533
  describe "raising timeout errors" do
491
-
534
+
492
535
  it "should raise timeout exception if declared in a stubbed response" do
493
536
  stub_http_request(:get, "www.example.com").to_timeout
494
537
  lambda {
@@ -518,7 +561,7 @@ shared_examples_for "WebMock" do
518
561
  response = http_request(:get, "http://www.example.com/")
519
562
  response.headers["Content-Length"].should == "8888"
520
563
  end
521
-
564
+
522
565
  it "should return declared headers when there are multiple headers with the same key" do
523
566
  stub_http_request(:get, "www.example.com").to_return(:headers => {"a" => ["b", "c"]})
524
567
  response = http_request(:get, "http://www.example.com/")
@@ -529,21 +572,29 @@ shared_examples_for "WebMock" do
529
572
  stub_http_request(:get, "www.example.com").to_return(:status => 500)
530
573
  http_request(:get, "http://www.example.com/").status.should == "500"
531
574
  end
532
-
575
+
533
576
  it "should return declared status message" do
534
577
  stub_http_request(:get, "www.example.com").to_return(:status => [500, "Internal Server Error"])
535
- http_request(:get, "http://www.example.com/").message.should == "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
536
583
  end
537
-
584
+
538
585
  it "should return default status code" do
539
586
  stub_http_request(:get, "www.example.com")
540
587
  http_request(:get, "http://www.example.com/").status.should == "200"
541
588
  end
542
-
589
+
543
590
  it "should return default empty message" do
544
591
  stub_http_request(:get, "www.example.com")
545
- http_request(:get, "http://www.example.com/").message.should == ""
546
- end
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
547
598
 
548
599
  it "should return body declared as IO" do
549
600
  stub_http_request(:get, "www.example.com").to_return(:body => File.new(__FILE__))
@@ -625,22 +676,25 @@ shared_examples_for "WebMock" do
625
676
  @response.headers.should == {
626
677
  "Date"=>"Sat, 23 Jan 2010 01:01:05 GMT",
627
678
  "Content-Type"=>"text/html; charset=UTF-8",
628
- "Content-Length"=>"438",
679
+ "Content-Length"=>"419",
629
680
  "Connection"=>"Keep-Alive",
630
681
  "Accept"=>"image/jpeg, image/png"
631
682
  }
632
683
  end
633
684
 
634
685
  it "should return recorded body" do
635
- @response.body.size.should == 438
686
+ @response.body.size.should == 419
636
687
  end
637
688
 
638
689
  it "should return recorded status" do
639
690
  @response.status.should == "202"
640
691
  end
641
-
692
+
642
693
  it "should return recorded status message" do
643
- @response.message.should == "OK"
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
644
698
  end
645
699
 
646
700
  it "should ensure file is closed" do
@@ -661,22 +715,25 @@ shared_examples_for "WebMock" do
661
715
  @response.headers.should == {
662
716
  "Date"=>"Sat, 23 Jan 2010 01:01:05 GMT",
663
717
  "Content-Type"=>"text/html; charset=UTF-8",
664
- "Content-Length"=>"438",
718
+ "Content-Length"=>"419",
665
719
  "Connection"=>"Keep-Alive",
666
720
  "Accept"=>"image/jpeg, image/png"
667
721
  }
668
722
  end
669
723
 
670
724
  it "should return recorded body" do
671
- @response.body.size.should == 438
725
+ @response.body.size.should == 419
672
726
  end
673
727
 
674
728
  it "should return recorded status" do
675
729
  @response.status.should == "202"
676
730
  end
677
-
731
+
678
732
  it "should return recorded status message" do
679
- @response.message.should == "OK"
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
680
737
  end
681
738
  end
682
739
 
@@ -689,18 +746,26 @@ shared_examples_for "WebMock" do
689
746
 
690
747
  it "should return response from evaluated file" do
691
748
  stub_http_request(:get, "www.example.com").to_return(lambda {|request| @files[request.uri.host.to_s] })
692
- http_request(:get, "http://www.example.com/").body.size.should == 438
749
+ http_request(:get, "http://www.example.com/").body.size.should == 419
693
750
  end
694
751
 
695
752
  it "should return response from evaluated string" do
696
753
  stub_http_request(:get, "www.example.com").to_return(lambda {|request| @files[request.uri.host.to_s].read })
697
- http_request(:get, "http://www.example.com/").body.size.should == 438
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)
698
761
  end
699
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
700
766
  end
701
767
 
702
768
  describe "sequences of responses" do
703
-
704
769
  it "should return responses one by one if declared in array" do
705
770
  stub_http_request(:get, "www.example.com").to_return([ {:body => "1"}, {:body => "2"}, {:body => "3"} ])
706
771
  http_request(:get, "http://www.example.com/").body.should == "1"
@@ -870,7 +935,7 @@ shared_examples_for "WebMock" do
870
935
  a_request(:get, "http://www.example.com").should_not have_been_made
871
936
  }.should fail_with(%r(The request GET http://www.example.com/ was expected to execute 0 times but it executed 1 time))
872
937
  end
873
-
938
+
874
939
  it "should fail with message with executed requests listed" do
875
940
  lambda {
876
941
  http_request(:get, "http://www.example.com/")
@@ -960,14 +1025,14 @@ shared_examples_for "WebMock" do
960
1025
  a_request(:get, /.*example.*/).should have_been_made
961
1026
  }.should_not raise_error
962
1027
  end
963
-
1028
+
964
1029
  end
965
1030
 
966
1031
  describe "when matching requests with query params" do
967
1032
  before(:each) do
968
1033
  stub_http_request(:any, /.*example.*/)
969
1034
  end
970
-
1035
+
971
1036
  it "should pass if the request was executed with query params declared in a hash in query option" do
972
1037
  lambda {
973
1038
  http_request(:get, "http://www.example.com/?a[]=b&a[]=c")
@@ -988,7 +1053,7 @@ shared_examples_for "WebMock" do
988
1053
  a_request(:get, "www.example.com/?x=3").with(:query => {"a" => ["b", "c"]}).should have_been_made
989
1054
  }.should_not raise_error
990
1055
  end
991
-
1056
+
992
1057
  end
993
1058
 
994
1059
  it "should fail if requested more times than expected" do
@@ -1039,27 +1104,27 @@ shared_examples_for "WebMock" do
1039
1104
 
1040
1105
  it "should fail if request was executed with different body" do
1041
1106
  lambda {
1042
- http_request(:get, "http://www.example.com/", :body => /^abc/)
1107
+ http_request(:get, "http://www.example.com/", :body => "abc")
1043
1108
  a_request(:get, "www.example.com").
1044
- with(:body => "xabc").should have_been_made
1045
- }.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))
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))
1046
1111
  end
1047
-
1112
+
1048
1113
  end
1049
-
1114
+
1050
1115
  describe "when expected body is declared as a hash" do
1051
1116
  let(:body_hash) { {:a => '1', :b => 'five', 'c' => {'d' => ['e', 'f']}} }
1052
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)}
1053
1118
 
1054
1119
  describe "when request is executed with url encoded body matching hash" do
1055
-
1120
+
1056
1121
  it "should succeed" do
1057
1122
  lambda {
1058
1123
  http_request(:post, "http://www.example.com/", :body => 'a=1&c[d][]=e&c[d][]=f&b=five')
1059
1124
  a_request(:post, "www.example.com").with(:body => body_hash).should have_been_made
1060
1125
  }.should_not raise_error
1061
1126
  end
1062
-
1127
+
1063
1128
  it "should succeed if url encoded params have different order" do
1064
1129
  lambda {
1065
1130
  http_request(:post, "http://www.example.com/", :body => 'a=1&c[d][]=e&b=five&c[d][]=f')
@@ -1073,7 +1138,7 @@ shared_examples_for "WebMock" do
1073
1138
  a_request(:post, "www.example.com").with(:body => body_hash).should have_been_made
1074
1139
  }.should fail_with(fail_message)
1075
1140
  end
1076
-
1141
+
1077
1142
  end
1078
1143
 
1079
1144
  describe "when request is executed with json body matching hash and content type is set to json" do
@@ -1085,7 +1150,7 @@ shared_examples_for "WebMock" do
1085
1150
  a_request(:post, "www.example.com").with(:body => body_hash).should have_been_made
1086
1151
  }.should_not raise_error
1087
1152
  end
1088
-
1153
+
1089
1154
  it "should succeed if json body is in different form" do
1090
1155
  lambda {
1091
1156
  http_request(:post, "http://www.example.com/", :headers => {'Content-Type' => 'application/json'},
@@ -1093,13 +1158,20 @@ shared_examples_for "WebMock" do
1093
1158
  a_request(:post, "www.example.com").with(:body => body_hash).should have_been_made
1094
1159
  }.should_not raise_error
1095
1160
  end
1096
-
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
1097
1169
  end
1098
1170
 
1099
1171
 
1100
1172
  describe "when request is executed with xml body matching hash and content type is set to xml" do
1101
1173
  let(:body_hash) { { "opt" => {:a => "1", :b => 'five', 'c' => {'d' => ['e', 'f']}} }}
1102
-
1174
+
1103
1175
  it "should succeed" do
1104
1176
  lambda {
1105
1177
  http_request(:post, "http://www.example.com/", :headers => {'Content-Type' => 'application/xml'},
@@ -1107,7 +1179,7 @@ shared_examples_for "WebMock" do
1107
1179
  a_request(:post, "www.example.com").with(:body => body_hash).should have_been_made
1108
1180
  }.should_not raise_error
1109
1181
  end
1110
-
1182
+
1111
1183
  it "should succeed if xml body is in different form" do
1112
1184
  lambda {
1113
1185
  http_request(:post, "http://www.example.com/", :headers => {'Content-Type' => 'application/xml'},
@@ -1115,9 +1187,17 @@ shared_examples_for "WebMock" do
1115
1187
  a_request(:post, "www.example.com").with(:body => body_hash).should have_been_made
1116
1188
  }.should_not raise_error
1117
1189
  end
1118
-
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
+
1119
1199
  end
1120
-
1200
+
1121
1201
  end
1122
1202
 
1123
1203
  it "should succeed if request was executed with the same headers" do
@@ -1127,7 +1207,7 @@ shared_examples_for "WebMock" do
1127
1207
  with(:headers => SAMPLE_HEADERS).should have_been_made
1128
1208
  }.should_not raise_error
1129
1209
  end
1130
-
1210
+
1131
1211
  it "should succeed if request was executed with the same headers with value declared as array" do
1132
1212
  lambda {
1133
1213
  http_request(:get, "http://www.example.com/", :headers => {"a" => "b"})
@@ -1135,9 +1215,9 @@ shared_examples_for "WebMock" do
1135
1215
  with(:headers => {"a" => ["b"]}).should have_been_made
1136
1216
  }.should_not raise_error
1137
1217
  end
1138
-
1218
+
1139
1219
  describe "when multiple headers with the same key are passed" do
1140
-
1220
+
1141
1221
  it "should succeed if request was executed with the same headers" do
1142
1222
  lambda {
1143
1223
  http_request(:get, "http://www.example.com/", :headers => {"a" => ["b", "c"]})
@@ -1145,7 +1225,7 @@ shared_examples_for "WebMock" do
1145
1225
  with(:headers => {"a" => ["b", "c"]}).should have_been_made
1146
1226
  }.should_not raise_error
1147
1227
  end
1148
-
1228
+
1149
1229
  it "should succeed if request was executed with the same headers but different order" do
1150
1230
  lambda {
1151
1231
  http_request(:get, "http://www.example.com/", :headers => {"a" => ["b", "c"]})
@@ -1153,7 +1233,7 @@ shared_examples_for "WebMock" do
1153
1233
  with(:headers => {"a" => ["c", "b"]}).should have_been_made
1154
1234
  }.should_not raise_error
1155
1235
  end
1156
-
1236
+
1157
1237
  it "should fail if request was executed with different headers" do
1158
1238
  lambda {
1159
1239
  http_request(:get, "http://www.example.com/", :headers => {"a" => ["b", "c"]})
@@ -1161,7 +1241,7 @@ shared_examples_for "WebMock" do
1161
1241
  with(:headers => {"a" => ["b", "d"]}).should have_been_made
1162
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))
1163
1243
  end
1164
-
1244
+
1165
1245
  end
1166
1246
 
1167
1247
  it "should fail if request was executed with different headers" do
@@ -1202,18 +1282,18 @@ shared_examples_for "WebMock" do
1202
1282
 
1203
1283
  it "should succeed if request was executed with headers matching regular expressions" do
1204
1284
  lambda {
1205
- http_request(:get, "http://www.example.com/", :headers => { 'user-agent' => 'MyAppName' })
1285
+ http_request(:get, "http://www.example.com/", :headers => { 'some-header' => 'MyAppName' })
1206
1286
  a_request(:get, "www.example.com").
1207
- with(:headers => { :user_agent => /^MyAppName$/ }).should have_been_made
1287
+ with(:headers => { :some_header => /^MyAppName$/ }).should have_been_made
1208
1288
  }.should_not raise_error
1209
1289
  end
1210
1290
 
1211
1291
  it "should fail if request was executed with headers not matching regular expression" do
1212
1292
  lambda {
1213
- http_request(:get, "http://www.example.com/", :headers => { 'user_agent' => 'xMyAppName' })
1293
+ http_request(:get, "http://www.example.com/", :headers => { 'some-header' => 'xMyAppName' })
1214
1294
  a_request(:get, "www.example.com").
1215
- with(:headers => { :user_agent => /^MyAppName$/ }).should have_been_made
1216
- }.should fail_with(%r(The request GET http://www.example.com/ with headers \{'User-Agent'=>/\^MyAppName\$/\} was expected to execute 1 time but it executed 0 times))
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))
1217
1297
  end
1218
1298
 
1219
1299
  it "should suceed if request was executed and block evaluated to true" do
@@ -1376,13 +1456,66 @@ shared_examples_for "WebMock" do
1376
1456
  end
1377
1457
 
1378
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
+
1379
1513
  describe "when net connect allowed", :net_connect => true do
1380
1514
  before(:each) do
1381
1515
  WebMock.allow_net_connect!
1382
1516
  end
1383
1517
 
1384
1518
  it "should verify expected requests occured" do
1385
- setup_expectations_for_real_example_com_request
1386
1519
  lambda {
1387
1520
  http_request(:get, "http://www.example.com/")
1388
1521
  a_request(:get, "http://www.example.com").should have_been_made
@@ -1401,20 +1534,20 @@ shared_examples_for "WebMock" do
1401
1534
 
1402
1535
 
1403
1536
  describe "callbacks" do
1404
-
1537
+
1405
1538
  describe "after_request" do
1406
1539
  before(:each) do
1407
1540
  WebMock.reset_callbacks
1408
1541
  stub_request(:get, "http://www.example.com")
1409
1542
  end
1410
-
1543
+
1411
1544
  it "should not invoke callback unless request is made" do
1412
1545
  WebMock.after_request {
1413
1546
  @called = true
1414
1547
  }
1415
1548
  @called.should == nil
1416
1549
  end
1417
-
1550
+
1418
1551
  it "should invoke a callback after request is made" do
1419
1552
  WebMock.after_request {
1420
1553
  @called = true
@@ -1422,7 +1555,7 @@ shared_examples_for "WebMock" do
1422
1555
  http_request(:get, "http://www.example.com/")
1423
1556
  @called.should == true
1424
1557
  end
1425
-
1558
+
1426
1559
  it "should not invoke a callback if specific http library should be ignored" do
1427
1560
  WebMock.after_request(:except => [http_library()]) {
1428
1561
  @called = true
@@ -1430,7 +1563,7 @@ shared_examples_for "WebMock" do
1430
1563
  http_request(:get, "http://www.example.com/")
1431
1564
  @called.should == nil
1432
1565
  end
1433
-
1566
+
1434
1567
  it "should invoke a callback even if other http libraries should be ignored" do
1435
1568
  WebMock.after_request(:except => [:other_lib]) {
1436
1569
  @called = true
@@ -1438,7 +1571,7 @@ shared_examples_for "WebMock" do
1438
1571
  http_request(:get, "http://www.example.com/")
1439
1572
  @called.should == true
1440
1573
  end
1441
-
1574
+
1442
1575
  it "should pass request signature to the callback" do
1443
1576
  WebMock.after_request(:except => [:other_lib]) do |request_signature, _|
1444
1577
  @request_signature = request_signature
@@ -1446,7 +1579,7 @@ shared_examples_for "WebMock" do
1446
1579
  http_request(:get, "http://www.example.com/")
1447
1580
  @request_signature.uri.to_s.should == "http://www.example.com:80/"
1448
1581
  end
1449
-
1582
+
1450
1583
  describe "passing response to callback" do
1451
1584
 
1452
1585
  describe "for stubbed requests" do
@@ -1463,23 +1596,23 @@ shared_examples_for "WebMock" do
1463
1596
  http_request(:get, "http://www.example.com/")
1464
1597
  end
1465
1598
 
1466
- it "should pass response with status and message" do
1599
+ it "should pass response with status and message" do
1467
1600
  @response.status.should == ["200", "hello"]
1468
1601
  end
1469
-
1602
+
1470
1603
  it "should pass response with headers" do
1471
1604
  @response.headers.should == {
1472
- 'Content-Length' => '666',
1605
+ 'Content-Length' => '666',
1473
1606
  'Hello' => 'World'
1474
1607
  }
1475
1608
  end
1476
-
1609
+
1477
1610
  it "should pass response with body" do
1478
1611
  @response.body.should == "foo bar"
1479
1612
  end
1480
-
1613
+
1481
1614
  end
1482
-
1615
+
1483
1616
  describe "for real requests", :net_connect => true do
1484
1617
  before(:each) do
1485
1618
  WebMock.reset!
@@ -1491,29 +1624,32 @@ shared_examples_for "WebMock" do
1491
1624
  end
1492
1625
 
1493
1626
  it "should pass response with status and message" do
1494
- @response.status[0].should == 302
1495
- @response.status[1].should == "Found"
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
1496
1632
  end
1497
-
1633
+
1498
1634
  it "should pass response with headers" do
1499
- @response.headers["Content-Length"].should == "#{WWW_EXAMPLE_COM_CONTENT_LENGTH}"
1635
+ @response.headers["Content-Length"].should == "0"
1500
1636
  end
1501
-
1637
+
1502
1638
  it "should pass response with body" do
1503
- @response.body.size.should == WWW_EXAMPLE_COM_CONTENT_LENGTH
1639
+ @response.body.size.should == 0
1504
1640
  end
1505
-
1641
+
1506
1642
  end
1507
-
1643
+
1508
1644
  end
1509
-
1645
+
1510
1646
  it "should invoke multiple callbacks in order of their declarations" do
1511
1647
  WebMock.after_request { @called = 1 }
1512
1648
  WebMock.after_request { @called += 1 }
1513
1649
  http_request(:get, "http://www.example.com/")
1514
1650
  @called.should == 2
1515
1651
  end
1516
-
1652
+
1517
1653
  it "should invoke callbacks only for real requests if requested", :net_connect => true do
1518
1654
  WebMock.after_request(:real_requests_only => true) { @called = true }
1519
1655
  http_request(:get, "http://www.example.com/")
@@ -1522,17 +1658,113 @@ shared_examples_for "WebMock" do
1522
1658
  http_request(:get, "http://www.example.net/")
1523
1659
  @called.should == true
1524
1660
  end
1525
-
1661
+
1526
1662
  it "should clear all declared callbacks on reset callbacks" do
1527
1663
  WebMock.after_request { @called = 1 }
1528
1664
  WebMock.reset_callbacks
1529
- stub_request(:get, "http://www.example.com")
1665
+ stub_request(:get, "http://www.example.com/")
1530
1666
  http_request(:get, "http://www.example.com/")
1531
1667
  @called.should == nil
1532
1668
  end
1533
-
1534
1669
  end
1535
-
1536
1670
  end
1537
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
1538
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
+