webmock 1.19.0 → 1.20.0

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.
@@ -1,5 +1,15 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.20.0
4
+
5
+ * Add support for on_missing callback of Curb::Easy
6
+
7
+ Thanks to [Tasos Stathopoulos](https://github.com/astathopoulos)
8
+
9
+ * Add at_least_times and at_most_times matchers
10
+
11
+ Thanks to [Dan Buettner](https://github.com/Capncavedan)
12
+
3
13
  ## 1.19.0
4
14
 
5
15
  * Fixed issue with Excon adapter giving warning message when redirects middleware was enabled.
data/README.md CHANGED
@@ -563,6 +563,14 @@ a_request(:post, "www.example.com").
563
563
 
564
564
  a_request(:post, "www.something.com").should have_been_made.times(3)
565
565
 
566
+ a_request(:post, "www.something.com").should have_been_made.at_least_once
567
+
568
+ a_request(:post, "www.something.com").should have_been_made.at_least_times(3)
569
+
570
+ a_request(:post, "www.something.com").should have_been_made.at_most_twice
571
+
572
+ a_request(:post, "www.something.com").should have_been_made.at_most_times(3)
573
+
566
574
  a_request(:any, "www.example.com").should_not have_been_made
567
575
 
568
576
  a_request(:post, "www.example.com").with { |req| req.body == "abc" }.should have_been_made
@@ -920,6 +928,8 @@ People who submitted patches and new features or suggested improvements. Many th
920
928
  * Thorbjørn Hermanse
921
929
  * Mark Lorenz
922
930
  * tjsousa
931
+ * Tasos Stathopoulos
932
+ * Dan Buettner
923
933
 
924
934
  For a full list of contributors you can visit the
925
935
  [contributors](https://github.com/bblimke/webmock/contributors) page.
@@ -159,7 +159,9 @@ if defined?(Curl)
159
159
  case response_code
160
160
  when 200..299
161
161
  @on_success.call(self) if @on_success
162
- when 400..599
162
+ when 400..499
163
+ @on_missing.call(self, self.response_code) if @on_missing
164
+ when 500..599
163
165
  @on_failure.call(self, self.response_code) if @on_failure
164
166
  end
165
167
  end
@@ -257,7 +259,7 @@ if defined?(Curl)
257
259
  @content_type || super
258
260
  end
259
261
 
260
- %w[ success failure header body complete progress ].each do |callback|
262
+ %w[ success failure missing header body complete progress ].each do |callback|
261
263
  class_eval <<-METHOD, __FILE__, __LINE__
262
264
  def on_#{callback} &block
263
265
  @on_#{callback} = block
@@ -1,17 +1,26 @@
1
1
  module WebMock
2
2
  class RequestExecutionVerifier
3
3
 
4
- attr_accessor :request_pattern, :expected_times_executed, :times_executed
4
+ attr_accessor :request_pattern, :expected_times_executed, :times_executed, :at_least_times_executed, :at_most_times_executed
5
5
 
6
- def initialize(request_pattern = nil, expected_times_executed = nil)
6
+ def initialize(request_pattern = nil, expected_times_executed = nil, at_least_times_executed = nil, at_most_times_executed = nil)
7
7
  @request_pattern = request_pattern
8
8
  @expected_times_executed = expected_times_executed
9
+ @at_least_times_executed = at_least_times_executed
10
+ @at_most_times_executed = at_most_times_executed
9
11
  end
10
12
 
11
13
  def matches?
12
14
  @times_executed =
13
- RequestRegistry.instance.times_executed(@request_pattern)
14
- @times_executed == (@expected_times_executed || 1)
15
+ RequestRegistry.instance.times_executed(@request_pattern)
16
+
17
+ if @at_least_times_executed
18
+ @times_executed >= @at_least_times_executed
19
+ elsif @at_most_times_executed
20
+ @times_executed <= @at_most_times_executed
21
+ else
22
+ @times_executed == (@expected_times_executed || 1)
23
+ end
15
24
  end
16
25
 
17
26
  def does_not_match?
@@ -27,13 +36,23 @@ module WebMock
27
36
 
28
37
  def failure_message
29
38
  expected_times_executed = @expected_times_executed || 1
30
- text = %Q(The request #{request_pattern.to_s} was expected to execute #{times(expected_times_executed)} but it executed #{times(times_executed)})
39
+ text = if @at_least_times_executed
40
+ %Q(The request #{request_pattern.to_s} was expected to execute at least #{times(@at_least_times_executed)} but it executed #{times(times_executed)})
41
+ elsif @at_most_times_executed
42
+ %Q(The request #{request_pattern.to_s} was expected to execute at most #{times(@at_most_times_executed)} but it executed #{times(times_executed)})
43
+ else
44
+ %Q(The request #{request_pattern.to_s} was expected to execute #{times(expected_times_executed)} but it executed #{times(times_executed)})
45
+ end
31
46
  text << self.class.executed_requests_message
32
47
  text
33
48
  end
34
49
 
35
50
  def failure_message_when_negated
36
- text = if @expected_times_executed
51
+ text = if @at_least_times_executed
52
+ %Q(The request #{request_pattern.to_s} was not expected to execute at least #{times(@at_least_times_executed)} but it executed #{times(times_executed)})
53
+ elsif @at_most_times_executed
54
+ %Q(The request #{request_pattern.to_s} was not expected to execute at most #{times(@at_most_times_executed)} but it executed #{times(times_executed)})
55
+ elsif @expected_times_executed
37
56
  %Q(The request #{request_pattern.to_s} was not expected to execute #{times(expected_times_executed)} but it executed #{times(times_executed)})
38
57
  else
39
58
  %Q(The request #{request_pattern.to_s} was expected to execute 0 times but it executed #{times(times_executed)})
@@ -20,6 +20,36 @@ module WebMock
20
20
  self
21
21
  end
22
22
 
23
+ def at_least_once
24
+ @request_execution_verifier.at_least_times_executed = 1
25
+ self
26
+ end
27
+
28
+ def at_least_twice
29
+ @request_execution_verifier.at_least_times_executed = 2
30
+ self
31
+ end
32
+
33
+ def at_least_times(times)
34
+ @request_execution_verifier.at_least_times_executed = times.to_i
35
+ self
36
+ end
37
+
38
+ def at_most_once
39
+ @request_execution_verifier.at_most_times_executed = 1
40
+ self
41
+ end
42
+
43
+ def at_most_twice
44
+ @request_execution_verifier.at_most_times_executed = 2
45
+ self
46
+ end
47
+
48
+ def at_most_times(times)
49
+ @request_execution_verifier.at_most_times_executed = times.to_i
50
+ self
51
+ end
52
+
23
53
  def matches?(request_pattern)
24
54
  @request_execution_verifier.request_pattern = request_pattern
25
55
  @request_execution_verifier.matches?
@@ -1,3 +1,3 @@
1
1
  module WebMock
2
- VERSION = '1.19.0' unless defined?(::WebMock::VERSION)
2
+ VERSION = '1.20.0' unless defined?(::WebMock::VERSION)
3
3
  end
@@ -42,13 +42,13 @@ unless RUBY_PLATFORM =~ /java/
42
42
  test.should == body
43
43
  end
44
44
 
45
- it "should call on_failure with 4xx response" do
45
+ it "should call on_missing with 4xx response" do
46
46
  response_code = 403
47
47
  stub_request(:any, "example.com").
48
48
  to_return(:status => [response_code, "None shall pass"])
49
49
 
50
50
  test = nil
51
- @curl.on_failure do |c, code|
51
+ @curl.on_missing do |c, code|
52
52
  test = code
53
53
  end
54
54
  @curl.http_get
@@ -139,6 +139,7 @@ unless RUBY_PLATFORM =~ /java/
139
139
  stub_request(:any, "example.com")
140
140
  order = []
141
141
  @curl.on_success {|*args| order << :on_success }
142
+ @curl.on_missing {|*args| order << :on_missing }
142
143
  @curl.on_failure {|*args| order << :on_failure }
143
144
  @curl.on_header {|*args| order << :on_header }
144
145
  @curl.on_body {|*args| order << :on_body }
@@ -153,6 +154,7 @@ unless RUBY_PLATFORM =~ /java/
153
154
  stub_request(:any, "example.com").to_return(:status => [500, ""])
154
155
  order = []
155
156
  @curl.on_success {|*args| order << :on_success }
157
+ @curl.on_missing {|*args| order << :on_missing }
156
158
  @curl.on_failure {|*args| order << :on_failure }
157
159
  @curl.on_header {|*args| order << :on_header }
158
160
  @curl.on_body {|*args| order << :on_body }
@@ -162,6 +164,21 @@ unless RUBY_PLATFORM =~ /java/
162
164
 
163
165
  order.should == [:on_progress,:on_header,:on_body,:on_complete,:on_failure]
164
166
  end
167
+
168
+ it "should call callbacks in correct order on missing request" do
169
+ stub_request(:any, "example.com").to_return(:status => [403, ""])
170
+ order = []
171
+ @curl.on_success {|*args| order << :on_success }
172
+ @curl.on_missing {|*args| order << :on_missing }
173
+ @curl.on_failure {|*args| order << :on_failure }
174
+ @curl.on_header {|*args| order << :on_header }
175
+ @curl.on_body {|*args| order << :on_body }
176
+ @curl.on_complete {|*args| order << :on_complete }
177
+ @curl.on_progress {|*args| order << :on_progress }
178
+ @curl.http_get
179
+
180
+ order.should == [:on_progress,:on_header,:on_body,:on_complete,:on_missing]
181
+ end
165
182
  end
166
183
 
167
184
  describe '#last_effective_url' do
@@ -22,7 +22,7 @@ describe "Excon" do
22
22
  WebMock.allow_net_connect!
23
23
  r = Excon.new('http://httpstat.us/200').get(:response_block => lambda {|e, remaining, total| a << e}, :chunk_size => 1)
24
24
  a.should == ["2", "0", "0", " ", "O", "K"]
25
- r.body.should == "200 OK" #this should be "", but there is an issue in Excon https://github.com/excon/excon/issues/429
25
+ r.body.should == ""
26
26
  end
27
27
 
28
28
  it "should support excon response_block" do
@@ -30,7 +30,7 @@ describe "Excon" do
30
30
  stub_request(:get, "http://example.com/").to_return(:body => "abc")
31
31
  r = Excon.new('http://example.com').get(:response_block => lambda {|e, remaining, total| a << e}, :chunk_size => 1)
32
32
  a.should == ['a', 'b', 'c']
33
- r.body.should == "abc" #this should be "", but there is an issue in Excon https://github.com/excon/excon/issues/429
33
+ r.body.should == ""
34
34
  end
35
35
 
36
36
  it "should invoke callbacks with response body even if a real request is made", :net_connect => true do
@@ -43,7 +43,7 @@ describe "Excon" do
43
43
  r = Excon.new('http://httpstat.us/200').get(:response_block => lambda {|e, remaining, total| a << e}, :chunk_size => 1)
44
44
  response.body.should == "200 OK"
45
45
  a.should == ["2", "0", "0", " ", "O", "K"]
46
- r.body.should == "200 OK" #this should be "", but there is an issue in Excon https://github.com/excon/excon/issues/429
46
+ r.body.should == ""
47
47
  end
48
48
  end
49
49
 
@@ -177,6 +177,181 @@ shared_context "request expectations" do |*adapter_info|
177
177
  end
178
178
  end
179
179
 
180
+
181
+
182
+ describe "at_most_times" do
183
+ it "fails if request was made more times than maximum" do
184
+ lambda {
185
+ http_request(:get, "http://www.example.com/")
186
+ http_request(:get, "http://www.example.com/")
187
+ http_request(:get, "http://www.example.com/")
188
+ a_request(:get, "http://www.example.com").should have_been_made.at_most_times(2)
189
+ }.should fail_with(%r(The request GET http://www.example.com/ was expected to execute at most 2 times but it executed 3 times))
190
+ end
191
+
192
+ it "passes if request was made the maximum number of times" do
193
+ lambda {
194
+ http_request(:get, "http://www.example.com/")
195
+ http_request(:get, "http://www.example.com/")
196
+ a_request(:get, "http://www.example.com").should have_been_made.at_most_times(2)
197
+ }.should_not raise_error
198
+ end
199
+
200
+ it "passes if request was made fewer than the maximum number of times" do
201
+ lambda {
202
+ http_request(:get, "http://www.example.com/")
203
+ a_request(:get, "http://www.example.com").should have_been_made.at_most_times(2)
204
+ }.should_not raise_error
205
+ end
206
+
207
+ it "passes if request was not made at all" do
208
+ lambda {
209
+ a_request(:get, "http://www.example.com").should have_been_made.at_most_times(2)
210
+ }.should_not raise_error
211
+ end
212
+ end
213
+
214
+
215
+ describe "at_least_times" do
216
+ it "fails if request was made fewer times than minimum" do
217
+ lambda {
218
+ http_request(:get, "http://www.example.com/")
219
+ a_request(:get, "http://www.example.com").should have_been_made.at_least_times(2)
220
+ }.should fail_with(%r(The request GET http://www.example.com/ was expected to execute at least 2 times but it executed 1 time))
221
+ end
222
+
223
+ it "passes if request was made the minimum number of times" do
224
+ lambda {
225
+ http_request(:get, "http://www.example.com/")
226
+ http_request(:get, "http://www.example.com/")
227
+ a_request(:get, "http://www.example.com").should have_been_made.at_least_times(2)
228
+ }.should_not raise_error
229
+ end
230
+
231
+ it "passes if request was made more than the minimum number of times" do
232
+ lambda {
233
+ http_request(:get, "http://www.example.com/")
234
+ http_request(:get, "http://www.example.com/")
235
+ http_request(:get, "http://www.example.com/")
236
+ a_request(:get, "http://www.example.com").should have_been_made.at_least_times(2)
237
+ }.should_not raise_error
238
+ end
239
+
240
+ context "descriptive at_most_ matcher" do
241
+ context "at_most_once" do
242
+ it "succeeds if no request was executed" do
243
+ lambda {
244
+ a_request(:get, "http://www.example.com").should have_been_made.at_most_once
245
+ }.should_not raise_error
246
+ end
247
+
248
+ it "satisfies expectation if request was executed with the same uri and method once" do
249
+ lambda {
250
+ http_request(:get, "http://www.example.com/")
251
+ a_request(:get, "http://www.example.com").should have_been_made.at_most_once
252
+ }.should_not raise_error
253
+ end
254
+
255
+ it "fails if request was executed with the same uri and method twice" do
256
+ lambda {
257
+ http_request(:get, "http://www.example.com/")
258
+ http_request(:get, "http://www.example.com/")
259
+ a_request(:get, "http://www.example.com").should have_been_made.at_most_once
260
+ }.should raise_error
261
+ end
262
+ end
263
+
264
+ context "at_most_twice" do
265
+ it "succeeds if no request was executed" do
266
+ lambda {
267
+ a_request(:get, "http://www.example.com").should have_been_made.at_most_twice
268
+ }.should_not raise_error
269
+ end
270
+
271
+ it "succeeds if too few requests were executed" do
272
+ lambda {
273
+ http_request(:get, "http://www.example.com/")
274
+ a_request(:get, "http://www.example.com").should have_been_made.at_most_twice
275
+ }.should_not raise_error
276
+ end
277
+
278
+ it "satisfies expectation if request was executed with the same uri and method twice" do
279
+ lambda {
280
+ http_request(:get, "http://www.example.com/")
281
+ http_request(:get, "http://www.example.com/")
282
+ a_request(:get, "http://www.example.com").should have_been_made.at_most_twice
283
+ }.should_not raise_error
284
+ end
285
+
286
+ it "fails if request was executed with the same uri and method three times" do
287
+ lambda {
288
+ http_request(:get, "http://www.example.com/")
289
+ http_request(:get, "http://www.example.com/")
290
+ http_request(:get, "http://www.example.com/")
291
+ a_request(:get, "http://www.example.com").should have_been_made.at_most_twice
292
+ }.should raise_error
293
+ end
294
+ end
295
+ end
296
+
297
+ context "descriptive at_least_ matcher" do
298
+ context "at_least_once" do
299
+ it "fails if no request was executed" do
300
+ lambda {
301
+ a_request(:get, "http://www.example.com").should have_been_made.at_least_once
302
+ }.should raise_error
303
+ end
304
+
305
+ it "satisfies expectation if request was executed with the same uri and method once" do
306
+ lambda {
307
+ http_request(:get, "http://www.example.com/")
308
+ a_request(:get, "http://www.example.com").should have_been_made.at_least_once
309
+ }.should_not raise_error
310
+ end
311
+
312
+ it "satisfies expectation if request was executed with the same uri and method twice" do
313
+ lambda {
314
+ http_request(:get, "http://www.example.com/")
315
+ http_request(:get, "http://www.example.com/")
316
+ a_request(:get, "http://www.example.com").should have_been_made.at_least_once
317
+ }.should_not raise_error
318
+ end
319
+ end
320
+
321
+ context "at_least_twice" do
322
+ it "fails if no request was executed" do
323
+ lambda {
324
+ a_request(:get, "http://www.example.com").should have_been_made.at_least_twice
325
+ }.should raise_error
326
+ end
327
+
328
+ it "fails if too few requests were executed" do
329
+ lambda {
330
+ http_request(:get, "http://www.example.com/")
331
+ a_request(:get, "http://www.example.com").should have_been_made.at_least_twice
332
+ }.should raise_error
333
+ end
334
+
335
+ it "satisfies expectation if request was executed with the same uri and method twice" do
336
+ lambda {
337
+ http_request(:get, "http://www.example.com/")
338
+ http_request(:get, "http://www.example.com/")
339
+ a_request(:get, "http://www.example.com").should have_been_made.at_least_twice
340
+ }.should_not raise_error
341
+ end
342
+
343
+ it "satisfies expectation if request was executed with the same uri and method three times" do
344
+ lambda {
345
+ http_request(:get, "http://www.example.com/")
346
+ http_request(:get, "http://www.example.com/")
347
+ http_request(:get, "http://www.example.com/")
348
+ a_request(:get, "http://www.example.com").should have_been_made.at_least_twice
349
+ }.should_not raise_error
350
+ end
351
+ end
352
+ end
353
+ end
354
+
180
355
  it "should fail if request was made more times than expected" do
181
356
  lambda {
182
357
  http_request(:get, "http://www.example.com/")
@@ -28,6 +28,41 @@ describe WebMock::RequestExecutionVerifier do
28
28
  @verifier.failure_message.should == expected_text
29
29
  end
30
30
 
31
+ context "at_least_times_executed is set" do
32
+ it "reports failure message correctly when executed times is one" do
33
+ @verifier.times_executed = 1
34
+ @verifier.at_least_times_executed = 2
35
+ expected_text = "The request www.example.com was expected to execute at least 2 times but it executed 1 time"
36
+ expected_text << @executed_requests_info
37
+ @verifier.failure_message.should == expected_text
38
+ end
39
+
40
+ it "reports failure message correctly when executed times is two" do
41
+ @verifier.times_executed = 2
42
+ @verifier.at_least_times_executed = 3
43
+ expected_text = "The request www.example.com was expected to execute at least 3 times but it executed 2 times"
44
+ expected_text << @executed_requests_info
45
+ @verifier.failure_message.should == expected_text
46
+ end
47
+ end
48
+
49
+ context "at_most_times_executed is set" do
50
+ it "reports failure message correctly when executed times is three" do
51
+ @verifier.times_executed = 3
52
+ @verifier.at_most_times_executed = 2
53
+ expected_text = "The request www.example.com was expected to execute at most 2 times but it executed 3 times"
54
+ expected_text << @executed_requests_info
55
+ @verifier.failure_message.should == expected_text
56
+ end
57
+
58
+ it "reports failure message correctly when executed times is two" do
59
+ @verifier.times_executed = 2
60
+ @verifier.at_most_times_executed = 1
61
+ expected_text = "The request www.example.com was expected to execute at most 1 time but it executed 2 times"
62
+ expected_text << @executed_requests_info
63
+ @verifier.failure_message.should == expected_text
64
+ end
65
+ end
31
66
  end
32
67
 
33
68
  describe "negative failure message" do
@@ -47,6 +82,42 @@ describe WebMock::RequestExecutionVerifier do
47
82
  @verifier.failure_message_when_negated.should == expected_text
48
83
  end
49
84
 
85
+ context "at_least_times_executed is set" do
86
+ it "reports failure message correctly when executed times is one" do
87
+ @verifier.times_executed = 3
88
+ @verifier.at_least_times_executed = 2
89
+ expected_text = "The request www.example.com was not expected to execute at least 2 times but it executed 3 times"
90
+ expected_text << @executed_requests_info
91
+ @verifier.failure_message_when_negated.should == expected_text
92
+ end
93
+
94
+ it "reports failure message correctly when executed times is two" do
95
+ @verifier.times_executed = 2
96
+ @verifier.at_least_times_executed = 2
97
+ expected_text = "The request www.example.com was not expected to execute at least 2 times but it executed 2 times"
98
+ expected_text << @executed_requests_info
99
+ @verifier.failure_message_when_negated.should == expected_text
100
+ end
101
+ end
102
+
103
+ context "at_most_times_executed is set" do
104
+ it "reports failure message correctly when executed times is three" do
105
+ @verifier.times_executed = 2
106
+ @verifier.at_most_times_executed = 3
107
+ expected_text = "The request www.example.com was not expected to execute at most 3 times but it executed 2 times"
108
+ expected_text << @executed_requests_info
109
+ @verifier.failure_message_when_negated.should == expected_text
110
+ end
111
+
112
+ it "reports failure message correctly when executed times is one" do
113
+ @verifier.times_executed = 1
114
+ @verifier.at_most_times_executed = 2
115
+ expected_text = "The request www.example.com was not expected to execute at most 2 times but it executed 1 time"
116
+ expected_text << @executed_requests_info
117
+ @verifier.failure_message_when_negated.should == expected_text
118
+ end
119
+ end
120
+
50
121
  end
51
122
 
52
123
  describe "matches?" do
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: webmock
3
3
  version: !ruby/object:Gem::Version
4
- hash: 91
4
+ hash: 71
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
- - 19
8
+ - 20
9
9
  - 0
10
- version: 1.19.0
10
+ version: 1.20.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Bartosz Blimke
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2014-09-28 00:00:00 +02:00
18
+ date: 2014-10-22 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency