webmock 0.7.1 → 0.7.2

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 (42) hide show
  1. data/CHANGELOG +9 -0
  2. data/README.md +169 -76
  3. data/Rakefile +2 -1
  4. data/VERSION +1 -1
  5. data/lib/webmock.rb +12 -9
  6. data/lib/webmock/adapters/rspec.rb +20 -22
  7. data/lib/webmock/adapters/rspec/matchers.rb +4 -4
  8. data/lib/webmock/adapters/rspec/webmock_matcher.rb +2 -2
  9. data/lib/webmock/adapters/test_unit.rb +20 -21
  10. data/lib/webmock/http_lib_adapters/net_http.rb +51 -15
  11. data/lib/webmock/request_profile.rb +5 -44
  12. data/lib/webmock/request_registry.rb +10 -11
  13. data/lib/webmock/request_signature.rb +44 -0
  14. data/lib/webmock/request_stub.rb +3 -3
  15. data/lib/webmock/response.rb +1 -1
  16. data/lib/webmock/rspec.rb +1 -0
  17. data/lib/webmock/test_unit.rb +1 -0
  18. data/lib/webmock/util/hash_counter.rb +16 -8
  19. data/lib/webmock/util/headers.rb +23 -0
  20. data/lib/webmock/util/uri.rb +81 -0
  21. data/lib/webmock/webmock.rb +16 -19
  22. data/spec/net_http_spec.rb +10 -9
  23. data/spec/other_net_http_libs_spec.rb +3 -1
  24. data/spec/request_profile_spec.rb +6 -116
  25. data/spec/request_registry_spec.rb +12 -17
  26. data/spec/request_signature_spec.rb +155 -0
  27. data/spec/request_stub_spec.rb +2 -2
  28. data/spec/response_spec.rb +1 -1
  29. data/spec/spec_helper.rb +4 -1
  30. data/spec/util/hash_counter_spec.rb +4 -4
  31. data/spec/util/headers_spec.rb +11 -0
  32. data/spec/util/uri_spec.rb +213 -0
  33. data/spec/vendor/addressable/lib/addressable/uri.rb +8 -0
  34. data/spec/vendor/addressable/lib/uri.rb +0 -0
  35. data/spec/webmock_spec.rb +58 -10
  36. data/test/test_helper.rb +5 -1
  37. data/test/test_webmock.rb +11 -6
  38. data/webmock.gemspec +21 -6
  39. metadata +28 -6
  40. data/lib/webmock/url.rb +0 -46
  41. data/lib/webmock/utility.rb +0 -65
  42. data/spec/utility_spec.rb +0 -70
@@ -6,7 +6,7 @@ describe RequestStub do
6
6
  @request_stub = RequestStub.new(:get, "www.google.com")
7
7
  end
8
8
 
9
- it "should have request profile with method and url" do
9
+ it "should have request profile with method and uri" do
10
10
  @request_stub.request_profile.method.should == :get
11
11
  @request_stub.request_profile.uri.host.should == "www.google.com"
12
12
  end
@@ -23,7 +23,7 @@ describe RequestStub do
23
23
  end
24
24
 
25
25
  it "should assign normalized headers to request profile" do
26
- Utility.should_receive(:normalize_headers).with('A' => 'a').and_return('B' => 'b')
26
+ Util::Headers.should_receive(:normalize_headers).with('A' => 'a').and_return('B' => 'b')
27
27
  @request_stub.with(:headers => {'A' => 'a'})
28
28
  @request_stub.request_profile.headers.should == {'B' => 'b'}
29
29
  end
@@ -6,7 +6,7 @@ describe Response do
6
6
  end
7
7
 
8
8
  it "should report normalized headers" do
9
- Utility.should_receive(:normalize_headers).with('A' => 'a').and_return('B' => 'b')
9
+ Util::Headers.should_receive(:normalize_headers).with('A' => 'a').and_return('B' => 'b')
10
10
  @response.headers.should == {'B' => 'b'}
11
11
  end
12
12
 
@@ -1,8 +1,10 @@
1
1
  $LOAD_PATH.unshift(File.dirname(__FILE__))
2
2
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
- require 'webmock'
4
3
  require 'spec'
5
4
  require 'spec/autorun'
5
+ require 'rubygems'
6
+
7
+ require 'webmock/rspec'
6
8
 
7
9
  include WebMock
8
10
 
@@ -56,3 +58,4 @@ def setup_expectations_for_real_google_request(options = {})
56
58
  :response_body => "<title>Google fake response</title>" }
57
59
  setup_expectations_for_real_request(defaults.merge(options))
58
60
  end
61
+
@@ -1,13 +1,13 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
2
 
3
- describe HashCounter do
3
+ describe Util::HashCounter do
4
4
 
5
5
  it "should return 0 for non existing key" do
6
- HashCounter.new.get(:abc).should == 0
6
+ Util::HashCounter.new.get(:abc).should == 0
7
7
  end
8
8
 
9
9
  it "should increase the returned value on every put with the same key" do
10
- counter =HashCounter.new
10
+ counter =Util::HashCounter.new
11
11
  counter.put(:abc)
12
12
  counter.get(:abc).should == 1
13
13
  counter.put(:abc)
@@ -15,7 +15,7 @@ describe HashCounter do
15
15
  end
16
16
 
17
17
  it "should only increase value for given key provided to put" do
18
- counter =HashCounter.new
18
+ counter =Util::HashCounter.new
19
19
  counter.put(:abc)
20
20
  counter.get(:abc).should == 1
21
21
  counter.get(:def).should == 0
@@ -0,0 +1,11 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe WebMock::Util::Headers do
4
+
5
+ it "should decode_userinfo_from_header handles basic auth" do
6
+ authorization_header = "Basic dXNlcm5hbWU6c2VjcmV0"
7
+ userinfo = Util::Headers.decode_userinfo_from_header(authorization_header)
8
+ userinfo.should == "username:secret"
9
+ end
10
+
11
+ end
@@ -0,0 +1,213 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+
4
+ URIS_WITHOUT_PATH_OR_PARAMS =
5
+ [
6
+ "www.google.com",
7
+ "www.google.com/",
8
+ "www.google.com:80",
9
+ "www.google.com:80/",
10
+ "http://www.google.com",
11
+ "http://www.google.com/",
12
+ "http://www.google.com:80",
13
+ "http://www.google.com:80/"
14
+ ].sort
15
+
16
+ URIS_WITH_AUTH =
17
+ [
18
+ "a b:pass@www.google.com",
19
+ "a b:pass@www.google.com/",
20
+ "a b:pass@www.google.com:80",
21
+ "a b:pass@www.google.com:80/",
22
+ "http://a b:pass@www.google.com",
23
+ "http://a b:pass@www.google.com/",
24
+ "http://a b:pass@www.google.com:80",
25
+ "http://a b:pass@www.google.com:80/",
26
+ "a%20b:pass@www.google.com",
27
+ "a%20b:pass@www.google.com/",
28
+ "a%20b:pass@www.google.com:80",
29
+ "a%20b:pass@www.google.com:80/",
30
+ "http://a%20b:pass@www.google.com",
31
+ "http://a%20b:pass@www.google.com/",
32
+ "http://a%20b:pass@www.google.com:80",
33
+ "http://a%20b:pass@www.google.com:80/"
34
+ ].sort
35
+
36
+ URIS_WITH_PATH_AND_PARAMS =
37
+ [
38
+ "www.google.com/big image.jpg/?a=big image&b=c",
39
+ "www.google.com/big%20image.jpg/?a=big%20image&b=c",
40
+ "www.google.com:80/big image.jpg/?a=big image&b=c",
41
+ "www.google.com:80/big%20image.jpg/?a=big%20image&b=c",
42
+ "http://www.google.com/big image.jpg/?a=big image&b=c",
43
+ "http://www.google.com/big%20image.jpg/?a=big%20image&b=c",
44
+ "http://www.google.com:80/big image.jpg/?a=big image&b=c",
45
+ "http://www.google.com:80/big%20image.jpg/?a=big%20image&b=c",
46
+ ].sort
47
+
48
+ URIS_WITH_DIFFERENT_PORT =
49
+ [
50
+ "www.google.com:88",
51
+ "www.google.com:88/",
52
+ "http://www.google.com:88",
53
+ "http://www.google.com:88/"
54
+ ].sort
55
+
56
+ URIS_FOR_HTTPS =
57
+ [
58
+ "https://www.google.com",
59
+ "https://www.google.com/",
60
+ "https://www.google.com:443",
61
+ "https://www.google.com:443/"
62
+ ].sort
63
+
64
+
65
+ describe WebMock::Util::URI do
66
+
67
+ describe "reporting variations of uri" do
68
+
69
+ it "should find all variations of the same uri for all variations of uri with params and path" do
70
+ URIS_WITH_PATH_AND_PARAMS.each do |uri|
71
+ WebMock::Util::URI.variations_of_uri_as_strings(uri).sort.should == URIS_WITH_PATH_AND_PARAMS
72
+ end
73
+ end
74
+
75
+ it "should find all variations of the same uri for all variations of uri without params or path" do
76
+ URIS_WITHOUT_PATH_OR_PARAMS.each do |uri|
77
+ WebMock::Util::URI.variations_of_uri_as_strings(uri).sort.should == URIS_WITHOUT_PATH_OR_PARAMS
78
+ end
79
+ end
80
+
81
+ it "should find all variations of the same uri for all variations of uri with auth" do
82
+ URIS_WITH_AUTH.each do |uri|
83
+ WebMock::Util::URI.variations_of_uri_as_strings(uri).sort.should == URIS_WITH_AUTH
84
+ end
85
+ end
86
+
87
+ it "should find all variations of the same uri for all variations of uri with different port" do
88
+ URIS_WITH_DIFFERENT_PORT.each do |uri|
89
+ WebMock::Util::URI.variations_of_uri_as_strings(uri).sort.should == URIS_WITH_DIFFERENT_PORT
90
+ end
91
+ end
92
+
93
+ it "should find all variations of the same uri for all variations of https uris" do
94
+ URIS_FOR_HTTPS.each do |uri|
95
+ WebMock::Util::URI.variations_of_uri_as_strings(uri).sort.should == URIS_FOR_HTTPS
96
+ end
97
+ end
98
+
99
+ end
100
+
101
+ describe "normalized uri equality" do
102
+
103
+ it "should successfully compare all variations of the same uri with path and params" do
104
+ URIS_WITH_PATH_AND_PARAMS.each do |uri_a|
105
+ URIS_WITH_PATH_AND_PARAMS.each do |uri_b|
106
+ WebMock::Util::URI.normalize_uri(uri_a).should === WebMock::Util::URI.normalize_uri(uri_b)
107
+ end
108
+ end
109
+ end
110
+
111
+ it "should successfully compare all variations of the same uri without path or params" do
112
+ URIS_WITHOUT_PATH_OR_PARAMS.each do |uri_a|
113
+ URIS_WITHOUT_PATH_OR_PARAMS.each do |uri_b|
114
+ WebMock::Util::URI.normalize_uri(uri_a).should === WebMock::Util::URI.normalize_uri(uri_b)
115
+ end
116
+ end
117
+ end
118
+
119
+ it "should successfully compare all variations of the same uri with authority" do
120
+ URIS_WITH_AUTH.each do |uri_a|
121
+ URIS_WITH_AUTH.each do |uri_b|
122
+ WebMock::Util::URI.normalize_uri(uri_a).should === WebMock::Util::URI.normalize_uri(uri_b)
123
+ end
124
+ end
125
+ end
126
+
127
+ it "should successfully compare all variations of the same uri custom port" do
128
+ URIS_WITH_DIFFERENT_PORT.each do |uri_a|
129
+ URIS_WITH_DIFFERENT_PORT.each do |uri_b|
130
+ WebMock::Util::URI.normalize_uri(uri_a).should === WebMock::Util::URI.normalize_uri(uri_b)
131
+ end
132
+ end
133
+ end
134
+
135
+ it "should successfully compare all variations of the same https uri" do
136
+ URIS_FOR_HTTPS.each do |uri_a|
137
+ URIS_FOR_HTTPS.each do |uri_b|
138
+ WebMock::Util::URI.normalize_uri(uri_a).should === WebMock::Util::URI.normalize_uri(uri_b)
139
+ end
140
+ end
141
+ end
142
+
143
+ end
144
+
145
+ describe "stripping default port" do
146
+
147
+ it "should strip_default_port_from_uri strips 80 from http with path" do
148
+ uri = "http://example.com:80/foo/bar"
149
+ stripped_uri = WebMock::Util::URI.strip_default_port_from_uri_string(uri)
150
+ stripped_uri.should == "http://example.com/foo/bar"
151
+ end
152
+
153
+ it "should strip_default_port_from_uri strips 80 from http without path" do
154
+ uri = "http://example.com:80"
155
+ stripped_uri = WebMock::Util::URI.strip_default_port_from_uri_string(uri)
156
+ stripped_uri.should == "http://example.com"
157
+ end
158
+
159
+ it "should strip_default_port_from_uri strips 443 from https without path" do
160
+ uri = "https://example.com:443"
161
+ stripped_uri = WebMock::Util::URI.strip_default_port_from_uri_string(uri)
162
+ stripped_uri.should == "https://example.com"
163
+ end
164
+
165
+ it "should strip_default_port_from_uri strips 443 from https" do
166
+ uri = "https://example.com:443/foo/bar"
167
+ stripped_uri = WebMock::Util::URI.strip_default_port_from_uri_string(uri)
168
+ stripped_uri.should == "https://example.com/foo/bar"
169
+ end
170
+
171
+ it "should strip_default_port_from_uri does not strip 8080 from http" do
172
+ uri = "http://example.com:8080/foo/bar"
173
+ WebMock::Util::URI.strip_default_port_from_uri_string(uri).should == uri
174
+ end
175
+
176
+ it "should strip_default_port_from_uri does not strip 443 from http" do
177
+ uri = "http://example.com:443/foo/bar"
178
+ WebMock::Util::URI.strip_default_port_from_uri_string(uri).should == uri
179
+ end
180
+
181
+ it "should strip_default_port_from_uri does not strip 80 from query string" do
182
+ uri = "http://example.com/?a=:80&b=c"
183
+ WebMock::Util::URI.strip_default_port_from_uri_string(uri).should == uri
184
+ end
185
+
186
+ it "should strip_default_port_from_uri does not modify strings that do not start with http or https" do
187
+ uri = "httpz://example.com:80/"
188
+ WebMock::Util::URI.strip_default_port_from_uri_string(uri).should == uri
189
+ end
190
+
191
+ end
192
+
193
+
194
+ describe "encoding userinfo" do
195
+
196
+ it "should encode unsafe chars in userinfo does not encode userinfo safe punctuation" do
197
+ userinfo = "user;&=+$,:secret"
198
+ WebMock::Util::URI.encode_unsafe_chars_in_userinfo(userinfo).should == userinfo
199
+ end
200
+
201
+ it "should encode unsafe chars in userinfo does not encode rfc 3986 unreserved characters" do
202
+ userinfo = "-.!~*'()abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789:secret"
203
+ WebMock::Util::URI.encode_unsafe_chars_in_userinfo(userinfo).should == userinfo
204
+ end
205
+
206
+ it "should encode unsafe chars in userinfo does encode other characters" do
207
+ userinfo, safe_userinfo = 'us#rn@me:sec//ret?"', 'us%23rn%40me:sec%2F%2Fret%3F%22'
208
+ WebMock::Util::URI.encode_unsafe_chars_in_userinfo(userinfo).should == safe_userinfo
209
+ end
210
+
211
+ end
212
+
213
+ end
@@ -0,0 +1,8 @@
1
+ module Addressable
2
+ class URI
3
+ module CharacterClasses
4
+ UNRESERVED = "123"
5
+ SUB_DELIMS = "123"
6
+ end
7
+ end
8
+ end
File without changes
@@ -1,8 +1,8 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
- include WebMock
4
-
5
3
  SAMPLE_HEADERS = { "Content-Length" => "8888" }
4
+ ESCAPED_PARAMS = "x=ab%2Bc&z=%27Stop%21%27%20said%20Fred"
5
+ NOT_ESCAPED_PARAMS = "z='Stop!' said Fred&x=ab c"
6
6
 
7
7
  describe "WebMock", :shared => true do
8
8
  before(:each) do
@@ -51,6 +51,25 @@ describe "WebMock", :shared => true do
51
51
 
52
52
  describe "when matching requests" do
53
53
 
54
+ describe "on uri" do
55
+
56
+ it "should match the request by uri with non escaped params if request have escaped parameters" do
57
+ stub_http_request(:get, "www.google.com/?#{NOT_ESCAPED_PARAMS}").to_return(:body => "abc")
58
+ http_request(:get, "http://www.google.com/?#{ESCAPED_PARAMS}").body.should == "abc"
59
+ end
60
+
61
+ it "should match the request by uri with escaped parameters even if request has non escaped params" do
62
+ stub_http_request(:get, "www.google.com/?#{ESCAPED_PARAMS}").to_return(:body => "abc")
63
+ http_request(:get, "http://www.google.com/?#{NOT_ESCAPED_PARAMS}").body.should == "abc"
64
+ end
65
+
66
+ it "should match the request by regexp matching non escaped params uri if request params are escaped" do
67
+ stub_http_request(:get, /.*x=ab c.*/).to_return(:body => "abc")
68
+ http_request(:get, "http://www.google.com/?#{ESCAPED_PARAMS}").body.should == "abc"
69
+ end
70
+
71
+ end
72
+
54
73
  describe "on method" do
55
74
 
56
75
  it "should match the request by method if registered" do
@@ -203,7 +222,7 @@ describe "WebMock", :shared => true do
203
222
  http_request(:get, "http://www.google.com/").body.should == "def"
204
223
  end
205
224
 
206
- it "should not be affected by the type of url or request method" do
225
+ it "should not be affected by the type of uri or request method" do
207
226
  stub_http_request(:get, "www.google.com").to_return(:body => "abc")
208
227
  stub_http_request(:any, /.*google.*/).to_return(:body => "def")
209
228
  http_request(:get, "http://www.google.com/").body.should == "def"
@@ -221,7 +240,7 @@ describe "WebMock", :shared => true do
221
240
  stub_http_request(:any, "https://www.google.com")
222
241
  end
223
242
 
224
- it "should pass if request was executed with the same url and method" do
243
+ it "should pass if request was executed with the same uri and method" do
225
244
  lambda {
226
245
  http_request(:get, "http://www.google.com/")
227
246
  request(:get, "http://www.google.com").should have_been_made.once
@@ -248,7 +267,7 @@ describe "WebMock", :shared => true do
248
267
  }.should fail_with("The request GET http://www.google.com/ was expected to execute 1 time but it executed 0 times")
249
268
  end
250
269
 
251
- it "should fail if request was executed to different url" do
270
+ it "should fail if request was executed to different uri" do
252
271
  lambda {
253
272
  http_request(:get, "http://www.google.com/")
254
273
  request(:get, "http://www.google.org").should have_been_made
@@ -262,21 +281,21 @@ describe "WebMock", :shared => true do
262
281
  }.should fail_with("The request GET http://www.google.com/ was expected to execute 1 time but it executed 0 times")
263
282
  end
264
283
 
265
- it "should_pass if request was executed with different form of url" do
284
+ it "should pass if request was executed with different form of uri" do
266
285
  lambda {
267
286
  http_request(:get, "http://www.google.com/")
268
287
  request(:get, "www.google.com").should have_been_made
269
288
  }.should_not raise_error
270
289
  end
271
290
 
272
- it "should_pass if request was executed with different form of url without port " do
291
+ it "should pass if request was executed with different form of uri without port " do
273
292
  lambda {
274
293
  http_request(:get, "http://www.google.com/")
275
294
  request(:get, "www.google.com:80").should have_been_made
276
295
  }.should_not raise_error
277
296
  end
278
297
 
279
- it "should_pass if request was executed with different form of url with port" do
298
+ it "should pass if request was executed with different form of uri with port" do
280
299
  lambda {
281
300
  http_request(:get, "http://www.google.com/")
282
301
  request(:get, "www.google.com:80").should have_been_made
@@ -290,13 +309,42 @@ describe "WebMock", :shared => true do
290
309
  }.should fail_with("The request GET http://www.google.com:90/ was expected to execute 1 time but it executed 0 times")
291
310
  end
292
311
 
293
- it "should_pass if request was executed with different form of url with https port" do
312
+ it "should pass if request was executed with different form of uri with https port" do
294
313
  lambda {
295
314
  http_request(:get, "https://www.google.com/")
296
315
  request(:get, "https://www.google.com:443/").should have_been_made
297
316
  }.should_not raise_error
298
317
  end
299
318
 
319
+ describe "when matching requests with escaped uris" do
320
+
321
+ before(:each) do
322
+ WebMock.disable_net_connect!
323
+ stub_http_request(:any, "http://www.google.com/?#{NOT_ESCAPED_PARAMS}")
324
+ end
325
+
326
+ it "should pass if request was executed with escaped params" do
327
+ lambda {
328
+ http_request(:get, "http://www.google.com/?#{ESCAPED_PARAMS}")
329
+ request(:get, "http://www.google.com/?#{NOT_ESCAPED_PARAMS}").should have_been_made
330
+ }.should_not raise_error
331
+ end
332
+
333
+ it "should pass if request was executed with non escaped params but escaped expected" do
334
+ lambda {
335
+ http_request(:get, "http://www.google.com/?#{NOT_ESCAPED_PARAMS}")
336
+ request(:get, "http://www.google.com/?#{ESCAPED_PARAMS}").should have_been_made
337
+ }.should_not raise_error
338
+ end
339
+
340
+ it "should pass if request was executed with escaped params but uri matichg regexp expected" do
341
+ lambda {
342
+ http_request(:get, "http://www.google.com/?#{ESCAPED_PARAMS}")
343
+ request(:get, /.*google.*/).should have_been_made
344
+ }.should_not raise_error
345
+ end
346
+ end
347
+
300
348
  it "should fail if requested more times than expected" do
301
349
  lambda {
302
350
  http_request(:get, "http://www.google.com/")
@@ -438,7 +486,7 @@ describe "WebMock", :shared => true do
438
486
  }.should fail_with("The request GET http://www.google.com/ was expected to execute 0 times but it executed 1 time")
439
487
  end
440
488
  end
441
-
489
+
442
490
 
443
491
 
444
492
  describe "using assert_requested" do
@@ -1,7 +1,11 @@
1
+ require 'rubygems'
2
+
1
3
  $LOAD_PATH.unshift(File.dirname(__FILE__))
2
4
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
5
+
6
+ require 'webmock/test_unit'
3
7
  require 'test/unit'
4
- require 'webmock'
8
+ include WebMock
5
9
 
6
10
  def assert_fail(message, &block)
7
11
  e = assert_raise(Test::Unit::AssertionFailedError, &block)