webmock 1.7.10 → 1.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) hide show
  1. data/.travis.yml +2 -2
  2. data/CHANGELOG.md +98 -24
  3. data/Gemfile +2 -3
  4. data/README.md +45 -4
  5. data/Rakefile +2 -2
  6. data/lib/webmock.rb +3 -0
  7. data/lib/webmock/api.rb +34 -6
  8. data/lib/webmock/http_lib_adapters/curb_adapter.rb +4 -41
  9. data/lib/webmock/http_lib_adapters/em_http_request/em_http_request_1_x.rb +1 -1
  10. data/lib/webmock/http_lib_adapters/excon_adapter.rb +94 -0
  11. data/lib/webmock/http_lib_adapters/httpclient_adapter.rb +31 -4
  12. data/lib/webmock/http_lib_adapters/net_http.rb +2 -0
  13. data/lib/webmock/http_lib_adapters/typhoeus_hydra_adapter.rb +4 -3
  14. data/lib/webmock/matchers/hash_including_matcher.rb +25 -0
  15. data/lib/webmock/rack_response.rb +8 -1
  16. data/lib/webmock/request_pattern.rb +108 -77
  17. data/lib/webmock/request_signature.rb +1 -0
  18. data/lib/webmock/stub_registry.rb +9 -8
  19. data/lib/webmock/version.rb +1 -1
  20. data/lib/webmock/webmock.rb +5 -2
  21. data/minitest/webmock_spec.rb +22 -2
  22. data/spec/acceptance/curb/curb_spec_helper.rb +12 -2
  23. data/spec/acceptance/em_http_request/em_http_request_spec.rb +42 -33
  24. data/spec/acceptance/em_http_request/em_http_request_spec_helper.rb +4 -2
  25. data/spec/acceptance/excon/excon_spec.rb +15 -0
  26. data/spec/acceptance/excon/excon_spec_helper.rb +37 -0
  27. data/spec/acceptance/net_http/net_http_spec.rb +7 -0
  28. data/spec/acceptance/net_http/net_http_spec_helper.rb +3 -1
  29. data/spec/acceptance/patron/patron_spec.rb +12 -3
  30. data/spec/acceptance/patron/patron_spec_helper.rb +2 -2
  31. data/spec/acceptance/shared/allowing_and_disabling_net_connect.rb +3 -3
  32. data/spec/acceptance/shared/callbacks.rb +22 -6
  33. data/spec/acceptance/shared/complex_cross_concern_behaviors.rb +21 -0
  34. data/spec/acceptance/shared/enabling_and_disabling_webmock.rb +10 -11
  35. data/spec/acceptance/shared/precedence_of_stubs.rb +1 -1
  36. data/spec/acceptance/shared/request_expectations.rb +49 -3
  37. data/spec/acceptance/shared/returning_declared_responses.rb +9 -21
  38. data/spec/acceptance/shared/stubbing_requests.rb +80 -4
  39. data/spec/acceptance/typhoeus/typhoeus_hydra_spec_helper.rb +1 -1
  40. data/spec/acceptance/webmock_shared.rb +11 -8
  41. data/spec/spec_helper.rb +3 -3
  42. data/spec/support/my_rack_app.rb +25 -1
  43. data/spec/support/webmock_server.rb +9 -6
  44. data/spec/unit/rack_response_spec.rb +18 -0
  45. data/spec/unit/request_pattern_spec.rb +205 -96
  46. data/spec/unit/request_signature_spec.rb +36 -34
  47. data/spec/unit/util/uri_spec.rb +14 -2
  48. data/test/shared_test.rb +31 -2
  49. data/webmock.gemspec +9 -7
  50. metadata +86 -73
@@ -5,7 +5,7 @@ require 'singleton'
5
5
  class WebMockServer
6
6
  include Singleton
7
7
 
8
- attr_reader :port
8
+ attr_reader :port, :started
9
9
 
10
10
  def host_with_port
11
11
  "localhost:#{port}"
@@ -22,6 +22,7 @@ class WebMockServer
22
22
  end
23
23
 
24
24
  def start
25
+ @started = true
25
26
  server = WEBrick::GenericServer.new(:Port => 0, :Logger => Logger.new("/dev/null"))
26
27
  server.logger.level = 0
27
28
  @port = server.config[:Port]
@@ -32,11 +33,13 @@ class WebMockServer
32
33
  end
33
34
  server.start do |socket|
34
35
  socket.puts <<-EOT.gsub(/^\s+\|/, '')
35
- |HTTP/1.1 200 OK
36
- |Date: Fri, 31 Dec 1999 23:59:59 GMT
37
- |Content-Type: text/html
38
- |Content-Length: 11
39
- |
36
+ |HTTP/1.1 200 OK\r
37
+ |Date: Fri, 31 Dec 1999 23:59:59 GMT\r
38
+ |Content-Type: text/html\r
39
+ |Content-Length: 11\r
40
+ |Set-Cookie: bar\r
41
+ |Set-Cookie: foo\r
42
+ |\r
40
43
  |hello world
41
44
  EOT
42
45
  end
@@ -3,6 +3,7 @@ require 'spec_helper'
3
3
  describe WebMock::RackResponse do
4
4
  before :each do
5
5
  @rack_response = WebMock::RackResponse.new(MyRackApp)
6
+ @locked_rack_response = WebMock::RackResponse.new(MyLockedRackApp)
6
7
  end
7
8
 
8
9
  it "should hook up to a rack appliance" do
@@ -13,6 +14,23 @@ describe WebMock::RackResponse do
13
14
  response.body.should include('This is my root!')
14
15
  end
15
16
 
17
+ it "should behave correctly when the rack response is not a simple array of strings" do
18
+ request = WebMock::RequestSignature.new(:get, 'www.example.com/non_array_response')
19
+ response = @rack_response.evaluate(request)
20
+
21
+ response.status.first.should == 200
22
+ response.body.should include('This is not in an array!')
23
+ end
24
+
25
+ it "should shouldn't blow up when hitting a locked resource twice" do
26
+ request = WebMock::RequestSignature.new(:get, 'www.example.com/locked')
27
+ response = @locked_rack_response.evaluate(request)
28
+ response2 = @locked_rack_response.evaluate(request)
29
+
30
+ response2.body.should include('Single threaded response.')
31
+ response2.status.first.should == 200
32
+ end
33
+
16
34
  it "should send along params" do
17
35
  request = WebMock::RequestSignature.new(:get, 'www.example.com/greet?name=Johnny')
18
36
 
@@ -2,21 +2,35 @@ require 'spec_helper'
2
2
 
3
3
  describe WebMock::RequestPattern do
4
4
 
5
- it "should report string describing itself" do
6
- WebMock::RequestPattern.new(:get, "www.example.com",
5
+ describe "describing itself" do
6
+ it "should report string describing itself" do
7
+ WebMock::RequestPattern.new(:get, "www.example.com",
7
8
  :body => "abc", :headers => {'A' => 'a', 'B' => 'b'}).to_s.should ==
8
- "GET http://www.example.com/ with body \"abc\" with headers {'A'=>'a', 'B'=>'b'}"
9
- end
9
+ "GET http://www.example.com/ with body \"abc\" with headers {'A'=>'a', 'B'=>'b'}"
10
+ end
10
11
 
11
- it "should report string describing itself with block" do
12
- WebMock::RequestPattern.new(:get, "www.example.com",
12
+ it "should report string describing itself with block" do
13
+ WebMock::RequestPattern.new(:get, "www.example.com",
13
14
  :body => "abc", :headers => {'A' => 'a', 'B' => 'b'}).with {|req| true}.to_s.should ==
14
- "GET http://www.example.com/ with body \"abc\" with headers {'A'=>'a', 'B'=>'b'} with given block"
15
- end
15
+ "GET http://www.example.com/ with body \"abc\" with headers {'A'=>'a', 'B'=>'b'} with given block"
16
+ end
17
+
18
+ it "should report string describing itself with query params" do
19
+ WebMock::RequestPattern.new(:get, /.*example.*/, :query => {'a' => ['b', 'c']}).to_s.should ==
20
+ "GET /.*example.*/ with query params {\"a\"=>[\"b\", \"c\"]}"
21
+ end
22
+
23
+ it "should report string describing itself with query params as hash including matcher" do
24
+ WebMock::RequestPattern.new(:get, /.*example.*/,
25
+ :query => WebMock::Matchers::HashIncludingMatcher.new({'a' => ['b', 'c']})).to_s.should ==
26
+ "GET /.*example.*/ with query params hash_including({\"a\"=>[\"b\", \"c\"]})"
27
+ end
16
28
 
17
- it "should report string describing itself with query params" do
18
- WebMock::RequestPattern.new(:get, /.*example.*/, :query => {'a' => ['b', 'c']}).to_s.should ==
19
- "GET /.*example.*/ with query params {\"a\"=>[\"b\", \"c\"]}"
29
+ it "should report string describing itself with body as hash including matcher" do
30
+ WebMock::RequestPattern.new(:get, /.*example.*/,
31
+ :body => WebMock::Matchers::HashIncludingMatcher.new({'a' => ['b', 'c']})).to_s.should ==
32
+ "GET /.*example.*/ with body hash_including({\"a\"=>[\"b\", \"c\"]})"
33
+ end
20
34
  end
21
35
 
22
36
  describe "with" do
@@ -46,79 +60,79 @@ describe WebMock::RequestPattern do
46
60
  describe "when matching" do
47
61
 
48
62
  it "should match if uri matches and method matches" do
49
- WebMock::RequestPattern.new(:get, "www.example.com").
63
+ WebMock::RequestPattern.new(:get, "www.example.com").
50
64
  should match(WebMock::RequestSignature.new(:get, "www.example.com"))
51
65
  end
52
66
 
53
67
  it "should match if uri matches and method pattern is any" do
54
- WebMock::RequestPattern.new(:any, "www.example.com").
68
+ WebMock::RequestPattern.new(:any, "www.example.com").
55
69
  should match(WebMock::RequestSignature.new(:get, "www.example.com"))
56
70
  end
57
71
 
58
72
  it "should not match if request has different method" do
59
- WebMock::RequestPattern.new(:post, "www.example.com").
73
+ WebMock::RequestPattern.new(:post, "www.example.com").
60
74
  should_not match(WebMock::RequestSignature.new(:get, "www.example.com"))
61
75
  end
62
76
 
63
77
  it "should match if uri matches request uri" do
64
- WebMock::RequestPattern.new(:get, "www.example.com").
78
+ WebMock::RequestPattern.new(:get, "www.example.com").
65
79
  should match(WebMock::RequestSignature.new(:get, "www.example.com"))
66
80
  end
67
81
 
68
82
  it "should match if request has unescaped uri" do
69
- WebMock::RequestPattern.new(:get, "www.example.com/my%20path").
83
+ WebMock::RequestPattern.new(:get, "www.example.com/my%20path").
70
84
  should match(WebMock::RequestSignature.new(:get, "www.example.com/my path"))
71
85
  end
72
86
 
73
87
  it "should match if request has escaped uri" do
74
- WebMock::RequestPattern.new(:get, "www.example.com/my path").
88
+ WebMock::RequestPattern.new(:get, "www.example.com/my path").
75
89
  should match(WebMock::RequestSignature.new(:get, "www.example.com/my%20path"))
76
90
  end
77
91
 
78
92
  it "should match if uri regexp pattern matches unescaped form of request uri" do
79
- WebMock::RequestPattern.new(:get, /.*my path.*/).
93
+ WebMock::RequestPattern.new(:get, /.*my path.*/).
80
94
  should match(WebMock::RequestSignature.new(:get, "www.example.com/my%20path"))
81
95
  end
82
96
 
83
97
  it "should match if uri regexp pattern matches request uri" do
84
- WebMock::RequestPattern.new(:get, /.*example.*/).
98
+ WebMock::RequestPattern.new(:get, /.*example.*/).
85
99
  should match(WebMock::RequestSignature.new(:get, "www.example.com"))
86
100
  end
87
101
 
88
102
  it "should match for uris with same parameters as pattern" do
89
- WebMock::RequestPattern.new(:get, "www.example.com?a=1&b=2").
103
+ WebMock::RequestPattern.new(:get, "www.example.com?a=1&b=2").
90
104
  should match(WebMock::RequestSignature.new(:get, "www.example.com?a=1&b=2"))
91
105
  end
92
106
 
93
107
  it "should not match for uris with different parameters" do
94
- WebMock::RequestPattern.new(:get, "www.example.com?a=1&b=2").
108
+ WebMock::RequestPattern.new(:get, "www.example.com?a=1&b=2").
95
109
  should_not match(WebMock::RequestSignature.new(:get, "www.example.com?a=2&b=1"))
96
110
  end
97
111
 
98
112
  it "should match for uri parameters in different order" do
99
- WebMock::RequestPattern.new(:get, "www.example.com?b=2&a=1").
113
+ WebMock::RequestPattern.new(:get, "www.example.com?b=2&a=1").
100
114
  should match(WebMock::RequestSignature.new(:get, "www.example.com?a=1&b=2"))
101
115
  end
102
116
 
103
117
  describe "when parameters are escaped" do
104
118
 
105
119
  it "should match if uri pattern has escaped parameters and request has unescaped parameters" do
106
- WebMock::RequestPattern.new(:get, "www.example.com/?a=a%20b").
120
+ WebMock::RequestPattern.new(:get, "www.example.com/?a=a%20b").
107
121
  should match(WebMock::RequestSignature.new(:get, "www.example.com/?a=a b"))
108
122
  end
109
123
 
110
124
  it "should match if uri pattern has unescaped parameters and request has escaped parameters" do
111
- WebMock::RequestPattern.new(:get, "www.example.com/?a=a b").
125
+ WebMock::RequestPattern.new(:get, "www.example.com/?a=a b").
112
126
  should match(WebMock::RequestSignature.new(:get, "www.example.com/?a=a%20b"))
113
127
  end
114
128
 
115
129
  it "should match if uri regexp pattern matches uri with unescaped parameters and request has escaped parameters" do
116
- WebMock::RequestPattern.new(:get, /.*a=a b.*/).
130
+ WebMock::RequestPattern.new(:get, /.*a=a b.*/).
117
131
  should match(WebMock::RequestSignature.new(:get, "www.example.com/?a=a%20b"))
118
132
  end
119
133
 
120
134
  it "should match if uri regexp pattern matches uri with escaped parameters and request has unescaped parameters" do
121
- WebMock::RequestPattern.new(:get, /.*a=a%20b.*/).
135
+ WebMock::RequestPattern.new(:get, /.*a=a%20b.*/).
122
136
  should match(WebMock::RequestSignature.new(:get, "www.example.com/?a=a b"))
123
137
  end
124
138
 
@@ -126,77 +140,128 @@ describe WebMock::RequestPattern do
126
140
 
127
141
  describe "when matching requests on query params" do
128
142
 
129
- it "should match request query params even if uri is declared as regexp" do
130
- WebMock::RequestPattern.new(:get, /.*example.*/, :query => {"a" => ["b", "c"]}).
131
- should match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c"))
132
- end
143
+ describe "when uri is described as regexp" do
144
+ it "should match request query params" do
145
+ WebMock::RequestPattern.new(:get, /.*example.*/, :query => {"a" => ["b", "c"]}).
146
+ should match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c"))
147
+ end
133
148
 
134
- it "should match request query params if uri is declared as regexp but params don't match" do
135
- WebMock::RequestPattern.new(:get, /.*example.*/, :query => {"x" => ["b", "c"]}).
136
- should_not match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c"))
137
- end
149
+ it "should match request query params if params don't match" do
150
+ WebMock::RequestPattern.new(:get, /.*example.*/, :query => {"x" => ["b", "c"]}).
151
+ should_not match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c"))
152
+ end
138
153
 
139
- it "should match for query params are the same as declared in hash" do
140
- WebMock::RequestPattern.new(:get, "www.example.com", :query => {"a" => ["b", "c"]}).
141
- should match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c"))
142
- end
154
+ it "should match when query params are declared as HashIncluding matcher matching params" do
155
+ WebMock::RequestPattern.new(:get, /.*example.*/,
156
+ :query => WebMock::Matchers::HashIncludingMatcher.new({"a" => ["b", "c"]})).
157
+ should match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))
158
+ end
143
159
 
144
- it "should not match for query params are different than the declared in hash" do
145
- WebMock::RequestPattern.new(:get, "www.example.com", :query => {"a" => ["b", "c"]}).
146
- should_not match(WebMock::RequestSignature.new(:get, "www.example.com?x[]=b&a[]=c"))
147
- end
160
+ it "should not match when query params are declared as HashIncluding matcher not matching params" do
161
+ WebMock::RequestPattern.new(:get, /.*example.*/,
162
+ :query => WebMock::Matchers::HashIncludingMatcher.new({"x" => ["b", "c"]})).
163
+ should_not match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))
164
+ end
148
165
 
149
- it "should match for query params are the same as declared as string" do
150
- WebMock::RequestPattern.new(:get, "www.example.com", :query => "a[]=b&a[]=c").
151
- should match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c"))
152
- end
166
+ it "should match when query params are declared as RSpec HashIncluding matcher matching params" do
167
+ WebMock::RequestPattern.new(:get, /.*example.*/,
168
+ :query => RSpec::Mocks::ArgumentMatchers::HashIncludingMatcher.new({"a" => ["b", "c"]})).
169
+ should match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))
170
+ end
153
171
 
154
- it "should match for query params are the same as declared both in query option or url" do
155
- WebMock::RequestPattern.new(:get, "www.example.com/?x=3", :query => "a[]=b&a[]=c").
156
- should match(WebMock::RequestSignature.new(:get, "www.example.com/?x=3&a[]=b&a[]=c"))
172
+ it "should not match when query params are declared as RSpec HashIncluding matcher not matching params" do
173
+ WebMock::RequestPattern.new(:get, /.*example.*/,
174
+ :query => RSpec::Mocks::ArgumentMatchers::HashIncludingMatcher.new({"a" => ["b", "d"]})).
175
+ should_not match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))
176
+ end
157
177
  end
158
178
 
179
+ describe "when uri is described as string" do
180
+ it "should match when query params are the same as declared in hash" do
181
+ WebMock::RequestPattern.new(:get, "www.example.com", :query => {"a" => ["b", "c"]}).
182
+ should match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c"))
183
+ end
184
+
185
+ it "should not match when query params are different than the declared in hash" do
186
+ WebMock::RequestPattern.new(:get, "www.example.com", :query => {"a" => ["b", "c"]}).
187
+ should_not match(WebMock::RequestSignature.new(:get, "www.example.com?x[]=b&a[]=c"))
188
+ end
189
+
190
+ it "should match when query params are the same as declared as string" do
191
+ WebMock::RequestPattern.new(:get, "www.example.com", :query => "a[]=b&a[]=c").
192
+ should match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c"))
193
+ end
194
+
195
+ it "should match when query params are the same as declared both in query option or url" do
196
+ WebMock::RequestPattern.new(:get, "www.example.com/?x=3", :query => "a[]=b&a[]=c").
197
+ should match(WebMock::RequestSignature.new(:get, "www.example.com/?x=3&a[]=b&a[]=c"))
198
+ end
199
+
200
+ it "should match when query params are declared as HashIncluding matcher matching params" do
201
+ WebMock::RequestPattern.new(:get, "www.example.com",
202
+ :query => WebMock::Matchers::HashIncludingMatcher.new({"a" => ["b", "c"]})).
203
+ should match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))
204
+ end
205
+
206
+ it "should not match when query params are declared as HashIncluding matcher not matching params" do
207
+ WebMock::RequestPattern.new(:get, "www.example.com",
208
+ :query => WebMock::Matchers::HashIncludingMatcher.new({"x" => ["b", "c"]})).
209
+ should_not match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))
210
+ end
211
+
212
+ it "should match when query params are declared as RSpec HashIncluding matcher matching params" do
213
+ WebMock::RequestPattern.new(:get, "www.example.com",
214
+ :query => RSpec::Mocks::ArgumentMatchers::HashIncludingMatcher.new({"a" => ["b", "c"]})).
215
+ should match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))
216
+ end
217
+
218
+ it "should not match when query params are declared as RSpec HashIncluding matcher not matching params" do
219
+ WebMock::RequestPattern.new(:get, "www.example.com",
220
+ :query => RSpec::Mocks::ArgumentMatchers::HashIncludingMatcher.new({"a" => ["b", "d"]})).
221
+ should_not match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))
222
+ end
223
+ end
159
224
  end
160
225
 
161
226
  describe "when matching requests with body" do
162
227
 
163
228
  it "should match if request body and body pattern are the same" do
164
- WebMock::RequestPattern.new(:get, "www.example.com", :body => "abc").
229
+ WebMock::RequestPattern.new(:get, "www.example.com", :body => "abc").
165
230
  should match(WebMock::RequestSignature.new(:get, "www.example.com", :body => "abc"))
166
231
  end
167
232
 
168
233
  it "should match if request body matches regexp" do
169
- WebMock::RequestPattern.new(:get, "www.example.com", :body => /^abc$/).
234
+ WebMock::RequestPattern.new(:get, "www.example.com", :body => /^abc$/).
170
235
  should match(WebMock::RequestSignature.new(:get, "www.example.com", :body => "abc"))
171
236
  end
172
237
 
173
238
  it "should not match if body pattern is different than request body" do
174
- WebMock::RequestPattern.new(:get, "www.example.com", :body => "def").
239
+ WebMock::RequestPattern.new(:get, "www.example.com", :body => "def").
175
240
  should_not match(WebMock::RequestSignature.new(:get, "www.example.com", :body => "abc"))
176
241
  end
177
242
 
178
243
  it "should not match if request body doesn't match regexp pattern" do
179
- WebMock::RequestPattern.new(:get, "www.example.com", :body => /^abc$/).
244
+ WebMock::RequestPattern.new(:get, "www.example.com", :body => /^abc$/).
180
245
  should_not match(WebMock::RequestSignature.new(:get, "www.example.com", :body => "xabc"))
181
246
  end
182
247
 
183
248
  it "should match if pattern doesn't have specified body" do
184
- WebMock::RequestPattern.new(:get, "www.example.com").
249
+ WebMock::RequestPattern.new(:get, "www.example.com").
185
250
  should match(WebMock::RequestSignature.new(:get, "www.example.com", :body => "abc"))
186
251
  end
187
252
 
188
253
  it "should not match if pattern has body specified as nil but request body is not empty" do
189
- WebMock::RequestPattern.new(:get, "www.example.com", :body => nil).
254
+ WebMock::RequestPattern.new(:get, "www.example.com", :body => nil).
190
255
  should_not match(WebMock::RequestSignature.new(:get, "www.example.com", :body => "abc"))
191
256
  end
192
257
 
193
258
  it "should not match if pattern has empty body but request body is not empty" do
194
- WebMock::RequestPattern.new(:get, "www.example.com", :body => "").
259
+ WebMock::RequestPattern.new(:get, "www.example.com", :body => "").
195
260
  should_not match(WebMock::RequestSignature.new(:get, "www.example.com", :body => "abc"))
196
261
  end
197
262
 
198
263
  it "should not match if pattern has body specified but request has no body" do
199
- WebMock::RequestPattern.new(:get, "www.example.com", :body => "abc").
264
+ WebMock::RequestPattern.new(:get, "www.example.com", :body => "abc").
200
265
  should_not match(WebMock::RequestSignature.new(:get, "www.example.com"))
201
266
  end
202
267
 
@@ -205,53 +270,71 @@ describe WebMock::RequestPattern do
205
270
 
206
271
  describe "for request with url encoded body" do
207
272
  it "should match when hash matches body" do
208
- WebMock::RequestPattern.new(:post, 'www.example.com', :body => body_hash).
273
+ WebMock::RequestPattern.new(:post, 'www.example.com', :body => body_hash).
209
274
  should match(WebMock::RequestSignature.new(:post, "www.example.com", :body => 'a=1&c[d][]=e&c[d][]=f&b=five'))
210
275
  end
211
276
 
212
277
  it "should match when hash matches body in different order of params" do
213
- WebMock::RequestPattern.new(:post, 'www.example.com', :body => body_hash).
278
+ WebMock::RequestPattern.new(:post, 'www.example.com', :body => body_hash).
214
279
  should match(WebMock::RequestSignature.new(:post, "www.example.com", :body => 'a=1&c[d][]=e&b=five&c[d][]=f'))
215
280
  end
216
281
 
217
282
  it "should not match when hash doesn't match url encoded body" do
218
- WebMock::RequestPattern.new(:post, 'www.example.com', :body => body_hash).
283
+ WebMock::RequestPattern.new(:post, 'www.example.com', :body => body_hash).
219
284
  should_not match(WebMock::RequestSignature.new(:post, "www.example.com", :body => 'c[d][]=f&a=1&c[d][]=e'))
220
285
  end
221
286
 
222
287
  it "should not match when body is not url encoded" do
223
- WebMock::RequestPattern.new(:post, 'www.example.com', :body => body_hash).
288
+ WebMock::RequestPattern.new(:post, 'www.example.com', :body => body_hash).
224
289
  should_not match(WebMock::RequestSignature.new(:post, "www.example.com", :body => 'foo bar'))
225
290
  end
226
291
 
227
292
  it "should match when hash contains regex values" do
228
- WebMock::RequestPattern.new(:post, "www.example.com", :body => {:a => /^\w{5}$/, :b => {:c => /^\d{3}$/}}).
293
+ WebMock::RequestPattern.new(:post, "www.example.com", :body => {:a => /^\w{5}$/, :b => {:c => /^\d{3}$/}}).
229
294
  should match(WebMock::RequestSignature.new(:post, "www.example.com", :body => 'a=abcde&b[c]=123'))
230
295
  end
231
296
 
232
297
  it "should not match when hash does not contains regex values" do
233
- WebMock::RequestPattern.new(:post, "www.example.com", :body => {:a => /^\d+$/, :b => {:c => /^\d{3}$/}}).
298
+ WebMock::RequestPattern.new(:post, "www.example.com", :body => {:a => /^\d+$/, :b => {:c => /^\d{3}$/}}).
234
299
  should_not match(WebMock::RequestSignature.new(:post, "www.example.com", :body => 'a=abcde&b[c]=123'))
235
300
  end
236
301
  end
237
302
 
238
303
  describe "for request with json body and content type is set to json" do
239
304
  it "should match when hash matches body" do
240
- WebMock::RequestPattern.new(:post, 'www.example.com', :body => body_hash).
305
+ WebMock::RequestPattern.new(:post, 'www.example.com', :body => body_hash).
241
306
  should match(WebMock::RequestSignature.new(:post, "www.example.com", :headers => {:content_type => 'application/json'},
242
- :body => "{\"a\":\"1\",\"c\":{\"d\":[\"e\",\"f\"]},\"b\":\"five\"}"))
243
- end
307
+ :body => "{\"a\":\"1\",\"c\":{\"d\":[\"e\",\"f\"]},\"b\":\"five\"}"))
308
+ end
244
309
 
245
310
  it "should match if hash matches body in different form" do
246
- WebMock::RequestPattern.new(:post, 'www.example.com', :body => body_hash).
311
+ WebMock::RequestPattern.new(:post, 'www.example.com', :body => body_hash).
247
312
  should match(WebMock::RequestSignature.new(:post, "www.example.com", :headers => {:content_type => 'application/json'},
248
- :body => "{\"a\":\"1\",\"b\":\"five\",\"c\":{\"d\":[\"e\",\"f\"]}}"))
249
- end
313
+ :body => "{\"a\":\"1\",\"b\":\"five\",\"c\":{\"d\":[\"e\",\"f\"]}}"))
314
+ end
250
315
 
251
316
  it "should not match when body is not json" do
252
- WebMock::RequestPattern.new(:post, 'www.example.com', :body => body_hash).
317
+ WebMock::RequestPattern.new(:post, 'www.example.com', :body => body_hash).
318
+ should_not match(WebMock::RequestSignature.new(:post, "www.example.com",
319
+ :headers => {:content_type => 'application/json'}, :body => "foo bar"))
320
+ end
321
+
322
+ it "shound not match if request body is different" do
323
+ WebMock::RequestPattern.new(:post, 'www.example.com', :body => {:a => 1, :b => 2}).
324
+ should_not match(WebMock::RequestSignature.new(:post, "www.example.com",
325
+ :headers => {:content_type => 'application/json'}, :body => "{\"a\":1,\"c\":null}"))
326
+ end
327
+
328
+ it "should not match if request body is has less params than pattern" do
329
+ WebMock::RequestPattern.new(:post, 'www.example.com', :body => {:a => 1, :b => 2}).
330
+ should_not match(WebMock::RequestSignature.new(:post, "www.example.com",
331
+ :headers => {:content_type => 'application/json'}, :body => "{\"a\":1}"))
332
+ end
333
+
334
+ it "should not match if request body is has more params than pattern" do
335
+ WebMock::RequestPattern.new(:post, 'www.example.com', :body => {:a => 1}).
253
336
  should_not match(WebMock::RequestSignature.new(:post, "www.example.com",
254
- :headers => {:content_type => 'application/json'}, :body => "foo bar"))
337
+ :headers => {:content_type => 'application/json'}, :body => "{\"a\":1,\"c\":1}"))
255
338
  end
256
339
  end
257
340
 
@@ -259,101 +342,127 @@ describe WebMock::RequestPattern do
259
342
  let(:body_hash) { {"opt" => {:a => '1', :b => 'five', 'c' => {'d' => ['e', 'f']}}} }
260
343
 
261
344
  it "should match when hash matches body" do
262
- WebMock::RequestPattern.new(:post, 'www.example.com', :body => body_hash).
345
+ WebMock::RequestPattern.new(:post, 'www.example.com', :body => body_hash).
263
346
  should match(WebMock::RequestSignature.new(:post, "www.example.com", :headers => {:content_type => 'application/xml'},
264
- :body => "<opt a=\"1\" b=\"five\">\n <c>\n <d>e</d>\n <d>f</d>\n </c>\n</opt>\n"))
265
- end
347
+ :body => "<opt a=\"1\" b=\"five\">\n <c>\n <d>e</d>\n <d>f</d>\n </c>\n</opt>\n"))
348
+ end
266
349
 
267
350
  it "should match if hash matches body in different form" do
268
- WebMock::RequestPattern.new(:post, 'www.example.com', :body => body_hash).
351
+ WebMock::RequestPattern.new(:post, 'www.example.com', :body => body_hash).
269
352
  should match(WebMock::RequestSignature.new(:post, "www.example.com", :headers => {:content_type => 'application/xml'},
270
- :body => "<opt b=\"five\" a=\"1\">\n <c>\n <d>e</d>\n <d>f</d>\n </c>\n</opt>\n"))
271
- end
353
+ :body => "<opt b=\"five\" a=\"1\">\n <c>\n <d>e</d>\n <d>f</d>\n </c>\n</opt>\n"))
354
+ end
272
355
 
273
356
  it "should not match when body is not xml" do
274
- WebMock::RequestPattern.new(:post, 'www.example.com', :body => body_hash).
357
+ WebMock::RequestPattern.new(:post, 'www.example.com', :body => body_hash).
275
358
  should_not match(WebMock::RequestSignature.new(:post, "www.example.com",
276
- :headers => {:content_type => 'application/xml'}, :body => "foo bar"))
277
- end
359
+ :headers => {:content_type => 'application/xml'}, :body => "foo bar"))
360
+ end
278
361
  end
279
362
  end
280
- end
281
363
 
364
+ describe "when body in a pattern is declared as a partial hash matcher" do
365
+ let(:signature) { WebMock::RequestSignature.new(:post, "www.example.com", :body => 'a=1&c[d][]=e&c[d][]=f&b=five') }
366
+
367
+ it "should match when query params are declared as HashIncluding matcher matching params" do
368
+ WebMock::RequestPattern.new(:post, "www.example.com",
369
+ :body => WebMock::Matchers::HashIncludingMatcher.new({:a => '1', 'c' => {'d' => ['e', 'f']}})).
370
+ should match(signature)
371
+ end
282
372
 
373
+ it "should not match when query params are declared as HashIncluding matcher not matching params" do
374
+ WebMock::RequestPattern.new(:post, "www.example.com",
375
+ :body => WebMock::Matchers::HashIncludingMatcher.new({:x => '1', 'c' => {'d' => ['e', 'f']}})).
376
+ should_not match(signature)
377
+ end
378
+
379
+ it "should match when query params are declared as RSpec HashIncluding matcher matching params" do
380
+ WebMock::RequestPattern.new(:post, "www.example.com",
381
+ :body => RSpec::Mocks::ArgumentMatchers::HashIncludingMatcher.new({:a => '1', 'c' => {'d' => ['e', 'f']}})).
382
+ should match(signature)
383
+ end
384
+
385
+ it "should not match when query params are declared as RSpec HashIncluding matcher not matching params" do
386
+ WebMock::RequestPattern.new(:post, "www.example.com",
387
+ :body => RSpec::Mocks::ArgumentMatchers::HashIncludingMatcher.new({:x => '1', 'c' => {'d' => ['e', 'f']}})).
388
+ should_not match(signature)
389
+ end
390
+ end
391
+ end
283
392
 
284
393
  it "should match if pattern and request have the same headers" do
285
- WebMock::RequestPattern.new(:get, "www.example.com", :headers => {'Content-Type' => 'image/jpeg'}).
394
+ WebMock::RequestPattern.new(:get, "www.example.com", :headers => {'Content-Type' => 'image/jpeg'}).
286
395
  should match(WebMock::RequestSignature.new(:get, "www.example.com", :headers => {'Content-Type' => 'image/jpeg'}))
287
396
  end
288
397
 
289
398
  it "should match if pattern headers values are regexps matching request header values" do
290
- WebMock::RequestPattern.new(:get, "www.example.com", :headers => {'Content-Type' => %r{^image/jpeg$}}).
399
+ WebMock::RequestPattern.new(:get, "www.example.com", :headers => {'Content-Type' => %r{^image/jpeg$}}).
291
400
  should match(WebMock::RequestSignature.new(:get, "www.example.com", :headers => {'Content-Type' => 'image/jpeg'}))
292
401
  end
293
402
 
294
403
  it "should not match if pattern has different value of header than request" do
295
- WebMock::RequestPattern.new(:get, "www.example.com", :headers => {'Content-Type' => 'image/png'}).
404
+ WebMock::RequestPattern.new(:get, "www.example.com", :headers => {'Content-Type' => 'image/png'}).
296
405
  should_not match(WebMock::RequestSignature.new(:get, "www.example.com", :headers => {'Content-Type' => 'image/jpeg'}))
297
406
  end
298
407
 
299
408
  it "should not match if pattern header value regexp doesn't match request header value" do
300
- WebMock::RequestPattern.new(:get, "www.example.com", :headers => {'Content-Type' => %r{^image\/jpeg$}}).
409
+ WebMock::RequestPattern.new(:get, "www.example.com", :headers => {'Content-Type' => %r{^image\/jpeg$}}).
301
410
  should_not match(WebMock::RequestSignature.new(:get, "www.example.com", :headers => {'Content-Type' => 'image/jpegx'}))
302
411
  end
303
412
 
304
413
  it "should match if request has more headers than request pattern" do
305
- WebMock::RequestPattern.new(:get, "www.example.com", :headers => {'Content-Type' => 'image/jpeg'}).
414
+ WebMock::RequestPattern.new(:get, "www.example.com", :headers => {'Content-Type' => 'image/jpeg'}).
306
415
  should match(WebMock::RequestSignature.new(:get, "www.example.com", :headers => {'Content-Type' => 'image/jpeg', 'Content-Length' => '8888'}))
307
416
  end
308
417
 
309
418
  it "should not match if request has less headers than the request pattern" do
310
- WebMock::RequestPattern.new(:get, "www.example.com", :headers => {'Content-Type' => 'image/jpeg', 'Content-Length' => '8888'}).
419
+ WebMock::RequestPattern.new(:get, "www.example.com", :headers => {'Content-Type' => 'image/jpeg', 'Content-Length' => '8888'}).
311
420
  should_not match(WebMock::RequestSignature.new(:get, "www.example.com", :headers => {'Content-Type' => 'image/jpeg'}))
312
421
  end
313
422
 
314
423
  it "should match even is header keys are declared in different form" do
315
- WebMock::RequestPattern.new(:get, "www.example.com", :headers => {'ContentLength' => '8888', 'Content-type' => 'image/png'}).
424
+ WebMock::RequestPattern.new(:get, "www.example.com", :headers => {'ContentLength' => '8888', 'Content-type' => 'image/png'}).
316
425
  should match(WebMock::RequestSignature.new(:get, "www.example.com", :headers => {:ContentLength => 8888, 'content_type' => 'image/png'}))
317
426
  end
318
427
 
319
428
  it "should match is pattern doesn't have specified headers" do
320
- WebMock::RequestPattern.new(:get, "www.example.com").
429
+ WebMock::RequestPattern.new(:get, "www.example.com").
321
430
  should match(WebMock::RequestSignature.new(:get, "www.example.com", :headers => {'A' => 'a'}))
322
431
  end
323
432
 
324
433
  it "should not match if pattern has nil headers but request has headers" do
325
- WebMock::RequestPattern.new(:get, "www.example.com", :headers => nil).
434
+ WebMock::RequestPattern.new(:get, "www.example.com", :headers => nil).
326
435
  should_not match(WebMock::RequestSignature.new(:get, "www.example.com", :headers => {'A' => 'a'}))
327
436
  end
328
437
 
329
438
  it "should not match if pattern has empty headers but request has headers" do
330
- WebMock::RequestPattern.new(:get, "www.example.com", :headers => {}).
439
+ WebMock::RequestPattern.new(:get, "www.example.com", :headers => {}).
331
440
  should_not match(WebMock::RequestSignature.new(:get, "www.example.com", :headers => {'A' => 'a'}))
332
441
  end
333
442
 
334
443
  it "should not match if pattern has specified headers but request has nil headers" do
335
- WebMock::RequestPattern.new(:get, "www.example.com", :headers => {'A'=>'a'}).
444
+ WebMock::RequestPattern.new(:get, "www.example.com", :headers => {'A'=>'a'}).
336
445
  should_not match(WebMock::RequestSignature.new(:get, "www.example.com"))
337
446
  end
338
447
 
339
448
  it "should not match if pattern has specified headers but request has empty headers" do
340
- WebMock::RequestPattern.new(:get, "www.example.com", :headers => {'A'=>'a'}).
449
+ WebMock::RequestPattern.new(:get, "www.example.com", :headers => {'A'=>'a'}).
341
450
  should_not match(WebMock::RequestSignature.new(:get, "www.example.com", :headers => {}))
342
451
  end
343
452
 
344
453
  it "should match if block given in pattern evaluates request to true" do
345
- WebMock::RequestPattern.new(:get, "www.example.com").with { |request| true }.
454
+ WebMock::RequestPattern.new(:get, "www.example.com").with { |request| true }.
346
455
  should match(WebMock::RequestSignature.new(:get, "www.example.com"))
347
456
  end
348
457
 
349
458
  it "should not match if block given in pattrn evaluates request to false" do
350
- WebMock::RequestPattern.new(:get, "www.example.com").with { |request| false }.
459
+ WebMock::RequestPattern.new(:get, "www.example.com").with { |request| false }.
351
460
  should_not match(WebMock::RequestSignature.new(:get, "www.example.com"))
352
461
  end
353
462
 
354
463
  it "should yield block with request signature" do
355
464
  signature = WebMock::RequestSignature.new(:get, "www.example.com")
356
- WebMock::RequestPattern.new(:get, "www.example.com").with { |request| request == signature }.
465
+ WebMock::RequestPattern.new(:get, "www.example.com").with { |request| request == signature }.
357
466
  should match(signature)
358
467
  end
359
468