webmock 1.6.2 → 1.6.4
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/.rvmrc +1 -0
- data/CHANGELOG.md +39 -1
- data/Gemfile +2 -0
- data/README.md +229 -175
- data/Rakefile +15 -29
- data/lib/webmock.rb +2 -1
- data/lib/webmock/http_lib_adapters/curb.rb +33 -0
- data/lib/webmock/http_lib_adapters/em_http_request.rb +7 -0
- data/lib/webmock/http_lib_adapters/httpclient.rb +1 -1
- data/lib/webmock/request_pattern.rb +40 -3
- data/lib/webmock/version.rb +3 -0
- data/lib/webmock/webmock.rb +1 -3
- data/spec/curb_spec.rb +119 -1
- data/spec/em_http_request_spec.rb +5 -0
- data/spec/httpclient_spec_helper.rb +7 -3
- data/spec/net_http_spec.rb +3 -3
- data/spec/patron_spec.rb +11 -4
- data/spec/patron_spec_helper.rb +1 -0
- data/spec/request_pattern_spec.rb +10 -1
- data/spec/spec_helper.rb +5 -2
- data/spec/webmock_shared.rb +8 -10
- data/webmock.gemspec +24 -167
- metadata +24 -61
- data/VERSION +0 -1
data/.gitignore
CHANGED
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm use 1.9.2@webmock --create
|
data/CHANGELOG.md
CHANGED
@@ -1,4 +1,42 @@
|
|
1
|
-
#Changelog
|
1
|
+
# Changelog
|
2
|
+
|
3
|
+
## 1.6.4
|
4
|
+
|
5
|
+
This is a quick slip release to regenerate the gemspec. Apparently
|
6
|
+
jeweler inserts dependencies twice if you use the `gemspec` method in
|
7
|
+
your Gemfile and declare gem dependencies in your gemspec.
|
8
|
+
|
9
|
+
https://github.com/technicalpickles/jeweler/issues/154
|
10
|
+
|
11
|
+
josevalim:
|
12
|
+
|
13
|
+
> This just bit me. I just released a gem with the wrong dependencies
|
14
|
+
> because I have updated jeweler. This should have been opt-in,
|
15
|
+
> otherwise a bunch of people using jeweler are going to release gems
|
16
|
+
> with the wrong dependencies because you are automatically importing
|
17
|
+
> from the Gemfile.
|
18
|
+
|
19
|
+
## 1.6.3
|
20
|
+
|
21
|
+
* Update the dependency on addressable to get around an issue in v2.2.5.
|
22
|
+
Thanks to [Peter Higgins](https://github.com/phiggins).
|
23
|
+
|
24
|
+
* Add support for matching parameter values using a regular expression
|
25
|
+
as well as a string. Thanks to [Oleg M Prozorov](https://github.com/oleg).
|
26
|
+
|
27
|
+
* Fix integration with httpclient as the internal API has changed.
|
28
|
+
Thanks to [Frank Prößdorf](https://github.com/endor).
|
29
|
+
|
30
|
+
* Ensure Curl::Easy#content_type is always set. Thanks to [Peter
|
31
|
+
Higgins](https://github.com/phiggins).
|
32
|
+
|
33
|
+
* Fix bug with em-http-request adapter stubbing responses that have a
|
34
|
+
chunked transfer encoding. Thanks to [Myron
|
35
|
+
Marston](https://github.com/myronmarston).
|
36
|
+
|
37
|
+
* Fix a load of spec failures with Patron, httpclient, and specs that
|
38
|
+
depended on the behaviour of example.com. Thanks to [Alex
|
39
|
+
Grigorovich](https://github.com/grig).
|
2
40
|
|
3
41
|
## 1.6.2
|
4
42
|
|
data/Gemfile
ADDED
data/README.md
CHANGED
@@ -3,6 +3,12 @@ WebMock
|
|
3
3
|
|
4
4
|
Library for stubbing and setting expectations on HTTP requests in Ruby.
|
5
5
|
|
6
|
+
WebMock is looking for a maintainer
|
7
|
+
===================================
|
8
|
+
|
9
|
+
I'm not able to maintain WebMock until end of June 2011.
|
10
|
+
If anyone is interested in maintaining it in the meantime (at least handling pull requests and creating patch releases), please get in touch.
|
11
|
+
|
6
12
|
Features
|
7
13
|
--------
|
8
14
|
|
@@ -25,36 +31,36 @@ Supported HTTP libraries
|
|
25
31
|
|
26
32
|
##Installation
|
27
33
|
|
28
|
-
|
34
|
+
gem install webmock --source http://gemcutter.org
|
29
35
|
|
30
36
|
### or to install the latest development version from github master
|
31
37
|
|
32
|
-
|
33
|
-
|
34
|
-
|
38
|
+
git clone http://github.com/bblimke/webmock.git
|
39
|
+
cd webmock
|
40
|
+
rake install
|
35
41
|
|
36
|
-
### Test::Unit
|
42
|
+
### Test::Unit
|
37
43
|
|
38
44
|
Add the following code to `test/test_helper.rb`
|
39
45
|
|
40
|
-
|
46
|
+
require 'webmock/test_unit'
|
41
47
|
|
42
48
|
### RSpec
|
43
|
-
|
49
|
+
|
44
50
|
Add the following code to `spec/spec_helper`:
|
45
51
|
|
46
|
-
|
52
|
+
require 'webmock/rspec'
|
47
53
|
|
48
54
|
### Cucumber
|
49
55
|
|
50
56
|
Add the following code to `features/support/env.rb`
|
51
57
|
|
52
|
-
|
58
|
+
require 'webmock/cucumber'
|
53
59
|
|
54
60
|
You can also use WebMock outside a test framework:
|
55
61
|
|
56
|
-
|
57
|
-
|
62
|
+
require 'webmock'
|
63
|
+
include WebMock::API
|
58
64
|
|
59
65
|
## Examples
|
60
66
|
|
@@ -65,72 +71,72 @@ You can also use WebMock outside a test framework:
|
|
65
71
|
|
66
72
|
### Stubbed request based on uri only and with the default response
|
67
73
|
|
68
|
-
|
74
|
+
stub_request(:any, "www.example.com")
|
69
75
|
|
70
|
-
|
76
|
+
Net::HTTP.get("www.example.com", "/") # ===> Success
|
71
77
|
|
72
78
|
### Stubbing requests based on method, uri, body and headers
|
73
79
|
|
74
|
-
|
80
|
+
stub_request(:post, "www.example.com").with(:body => "abc", :headers => { 'Content-Length' => 3 })
|
75
81
|
|
76
|
-
|
82
|
+
uri = URI.parse("http://www.example.com/")
|
77
83
|
req = Net::HTTP::Post.new(uri.path)
|
78
|
-
|
84
|
+
req['Content-Length'] = 3
|
79
85
|
res = Net::HTTP.start(uri.host, uri.port) {|http|
|
80
86
|
http.request(req, "abc")
|
81
87
|
} # ===> Success
|
82
88
|
|
83
89
|
### Matching request body and headers against regular expressions
|
84
90
|
|
85
|
-
|
86
|
-
|
91
|
+
stub_request(:post, "www.example.com").
|
92
|
+
with(:body => /^.*world$/, :headers => {"Content-Type" => /image\/.+/}).to_return(:body => "abc")
|
87
93
|
|
88
|
-
|
94
|
+
uri = URI.parse('http://www.example.com/')
|
89
95
|
req = Net::HTTP::Post.new(uri.path)
|
90
|
-
|
96
|
+
req['Content-Type'] = 'image/png'
|
91
97
|
res = Net::HTTP.start(uri.host, uri.port) {|http|
|
92
98
|
http.request(req, 'hello world')
|
93
99
|
} # ===> Success
|
94
|
-
|
100
|
+
|
95
101
|
### Matching request body against a hash. Body can be URL-Encoded, JSON or XML.
|
96
102
|
|
97
|
-
|
98
|
-
|
103
|
+
stub_http_request(:post, "www.example.com").
|
104
|
+
with(:body => {:data => {:a => '1', :b => 'five'}})
|
105
|
+
|
106
|
+
RestClient.post('www.example.com', "data[a]=1&data[b]=five",
|
107
|
+
:content_type => 'application/x-www-form-urlencoded') # ===> Success
|
108
|
+
|
109
|
+
RestClient.post('www.example.com', '{"data":{"a":"1","b":"five"}}',
|
110
|
+
:content_type => 'application/json') # ===> Success
|
99
111
|
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
RestClient.post('www.example.com', '{"data":{"a":"1","b":"five"}}',
|
104
|
-
:content_type => 'application/json') # ===> Success
|
105
|
-
|
106
|
-
RestClient.post('www.example.com', '<data a="1" b="five" />',
|
107
|
-
:content_type => 'application/xml' ) # ===> Success
|
112
|
+
RestClient.post('www.example.com', '<data a="1" b="five" />',
|
113
|
+
:content_type => 'application/xml' ) # ===> Success
|
108
114
|
|
109
115
|
### Matching custom request headers
|
110
116
|
|
111
117
|
stub_request(:any, "www.example.com").with(:headers=>{ 'Header-Name' => 'Header-Value' })
|
112
118
|
|
113
|
-
|
119
|
+
uri = URI.parse('http://www.example.com/')
|
114
120
|
req = Net::HTTP::Post.new(uri.path)
|
115
|
-
|
121
|
+
req['Header-Name'] = 'Header-Value'
|
116
122
|
res = Net::HTTP.start(uri.host, uri.port) {|http|
|
117
123
|
http.request(req, 'abc')
|
118
124
|
} # ===> Success
|
119
125
|
|
120
126
|
### Matching multiple headers with the same name
|
121
127
|
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
+
stub_http_request(:get, 'www.example.com').with(:headers => {'Accept' => ['image/jpeg', 'image/png'] })
|
129
|
+
|
130
|
+
req = Net::HTTP::Get.new("/")
|
131
|
+
req['Accept'] = ['image/png']
|
132
|
+
req.add_field('Accept', 'image/jpeg')
|
133
|
+
Net::HTTP.start("www.example.com") {|http| http.request(req) } # ===> Success
|
128
134
|
|
129
135
|
### Matching requests against provided block
|
130
136
|
|
131
|
-
|
132
|
-
|
133
|
-
|
137
|
+
stub_request(:post, "www.example.com").with { |request| request.body == "abc" }
|
138
|
+
RestClient.post('www.example.com', 'abc') # ===> Success
|
139
|
+
|
134
140
|
### Request with basic authentication
|
135
141
|
|
136
142
|
stub_request(:get, "user:pass@www.example.com")
|
@@ -143,49 +149,49 @@ You can also use WebMock outside a test framework:
|
|
143
149
|
|
144
150
|
### Matching uris using regular expressions
|
145
151
|
|
146
|
-
|
152
|
+
stub_request(:any, /.*example.*/)
|
153
|
+
|
154
|
+
Net::HTTP.get('www.example.com', '/') # ===> Success
|
147
155
|
|
148
|
-
|
149
|
-
|
150
|
-
|
156
|
+
### Matching query params using hash
|
157
|
+
|
158
|
+
stub_http_request(:get, "www.example.com").with(:query => {"a" => ["b", "c"]})
|
159
|
+
|
160
|
+
RestClient.get("http://www.example.com/?a[]=b&a[]=c") # ===> Success
|
151
161
|
|
152
|
-
stub_http_request(:get, "www.example.com").with(:query => {"a" => ["b", "c"]})
|
153
|
-
|
154
|
-
RestClient.get("http://www.example.com/?a[]=b&a[]=c") # ===> Success
|
155
|
-
|
156
162
|
### Stubbing with custom response
|
157
163
|
|
158
|
-
|
159
|
-
|
160
|
-
|
164
|
+
stub_request(:any, "www.example.com").to_return(:body => "abc", :status => 200, :headers => { 'Content-Length' => 3 } )
|
165
|
+
|
166
|
+
Net::HTTP.get("www.example.com", '/') # ===> "abc"
|
161
167
|
|
162
168
|
### Response with body specified as IO object
|
163
169
|
|
164
|
-
|
170
|
+
File.open('/tmp/response_body.txt', 'w') { |f| f.puts 'abc' }
|
165
171
|
|
166
|
-
|
172
|
+
stub_request(:any, "www.example.com").to_return(:body => File.new('/tmp/response_body.txt'), :status => 200)
|
173
|
+
|
174
|
+
Net::HTTP.get('www.example.com', '/') # ===> "abc\n"
|
167
175
|
|
168
|
-
Net::HTTP.get('www.example.com', '/') # ===> "abc\n"
|
169
|
-
|
170
176
|
### Response with custom status message
|
171
177
|
|
172
|
-
|
178
|
+
stub_request(:any, "www.example.com").to_return(:status => [500, "Internal Server Error"])
|
179
|
+
|
180
|
+
req = Net::HTTP::Get.new("/")
|
181
|
+
Net::HTTP.start("www.example.com") { |http| http.request(req) }.message # ===> "Internal Server Error"
|
173
182
|
|
174
|
-
req = Net::HTTP::Get.new("/")
|
175
|
-
Net::HTTP.start("www.example.com") { |http| http.request(req) }.message # ===> "Internal Server Error"
|
176
|
-
|
177
183
|
### Replaying raw responses recorded with `curl -is`
|
178
184
|
|
179
|
-
|
180
|
-
|
185
|
+
`curl -is www.example.com > /tmp/example_curl_-is_output.txt`
|
186
|
+
raw_response_file = File.new("/tmp/example_curl_-is_output.txt")
|
181
187
|
|
182
188
|
from file
|
183
189
|
|
184
|
-
|
190
|
+
stub_request(:get, "www.example.com").to_return(raw_response_file)
|
185
191
|
|
186
192
|
or string
|
187
193
|
|
188
|
-
|
194
|
+
stub_request(:get, "www.example.com").to_return(raw_response_file.read)
|
189
195
|
|
190
196
|
### Responses dynamically evaluated from block
|
191
197
|
|
@@ -199,7 +205,7 @@ You can also use WebMock outside a test framework:
|
|
199
205
|
stub_request(:any, 'www.example.net').
|
200
206
|
to_return(lambda { |request| {:body => request.body} })
|
201
207
|
|
202
|
-
RestClient.post('www.example.net', 'abc') # ===> "abc\n"
|
208
|
+
RestClient.post('www.example.net', 'abc') # ===> "abc\n"
|
203
209
|
|
204
210
|
### Dynamically evaluated raw responses recorded with `curl -is`
|
205
211
|
|
@@ -211,90 +217,90 @@ You can also use WebMock outside a test framework:
|
|
211
217
|
stub_request(:any, 'www.example.net').
|
212
218
|
to_return(:body => lambda { |request| request.body })
|
213
219
|
|
214
|
-
RestClient.post('www.example.net', 'abc') # ===> "abc\n"
|
220
|
+
RestClient.post('www.example.net', 'abc') # ===> "abc\n"
|
215
221
|
|
216
222
|
### Raising errors
|
217
223
|
|
218
224
|
#### Exception declared by class
|
219
225
|
|
220
|
-
|
226
|
+
stub_request(:any, 'www.example.net').to_raise(StandardError)
|
221
227
|
|
222
228
|
RestClient.post('www.example.net', 'abc') # ===> StandardError
|
223
|
-
|
229
|
+
|
224
230
|
#### or by exception instance
|
225
231
|
|
226
232
|
stub_request(:any, 'www.example.net').to_raise(StandardError.new("some error"))
|
227
233
|
|
228
234
|
#### or by string
|
229
|
-
|
235
|
+
|
230
236
|
stub_request(:any, 'www.example.net').to_raise("some error")
|
231
237
|
|
232
238
|
### Raising timeout errors
|
233
239
|
|
234
|
-
|
240
|
+
stub_request(:any, 'www.example.net').to_timeout
|
235
241
|
|
236
|
-
|
242
|
+
RestClient.post('www.example.net', 'abc') # ===> RestClient::RequestTimeout
|
237
243
|
|
238
244
|
### Multiple responses for repeated requests
|
239
245
|
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
246
|
+
stub_request(:get, "www.example.com").to_return({:body => "abc"}, {:body => "def"})
|
247
|
+
Net::HTTP.get('www.example.com', '/') # ===> "abc\n"
|
248
|
+
Net::HTTP.get('www.example.com', '/') # ===> "def\n"
|
249
|
+
|
250
|
+
#after all responses are used the last response will be returned infinitely
|
251
|
+
|
252
|
+
Net::HTTP.get('www.example.com', '/') # ===> "def\n"
|
247
253
|
|
248
254
|
### Multiple responses using chained `to_return()`, `to_raise()` or `to_timeout` declarations
|
249
255
|
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
256
|
+
stub_request(:get, "www.example.com").
|
257
|
+
to_return({:body => "abc"}).then. #then() is just a syntactic sugar
|
258
|
+
to_return({:body => "def"}).then.
|
259
|
+
to_raise(MyException)
|
260
|
+
Net::HTTP.get('www.example.com', '/') # ===> "abc\n"
|
261
|
+
Net::HTTP.get('www.example.com', '/') # ===> "def\n"
|
262
|
+
Net::HTTP.get('www.example.com', '/') # ===> MyException raised
|
257
263
|
|
258
264
|
### Specifying number of times given response should be returned
|
259
265
|
|
260
|
-
|
261
|
-
|
262
|
-
|
266
|
+
stub_request(:get, "www.example.com").
|
267
|
+
to_return({:body => "abc"}).times(2).then.
|
268
|
+
to_return({:body => "def"})
|
263
269
|
|
264
|
-
|
265
|
-
|
266
|
-
|
270
|
+
Net::HTTP.get('www.example.com', '/') # ===> "abc\n"
|
271
|
+
Net::HTTP.get('www.example.com', '/') # ===> "abc\n"
|
272
|
+
Net::HTTP.get('www.example.com', '/') # ===> "def\n"
|
267
273
|
|
268
274
|
|
269
275
|
### Real requests to network can be allowed or disabled
|
270
276
|
|
271
|
-
|
277
|
+
WebMock.allow_net_connect!
|
278
|
+
|
279
|
+
stub_request(:any, "www.example.com").to_return(:body => "abc")
|
272
280
|
|
273
|
-
|
281
|
+
Net::HTTP.get('www.example.com', '/') # ===> "abc"
|
274
282
|
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
Net::HTTP.get('www.something.com', '/') # ===> Failure
|
283
|
+
Net::HTTP.get('www.something.com', '/') # ===> /.+Something.+/
|
284
|
+
|
285
|
+
WebMock.disable_net_connect!
|
286
|
+
|
287
|
+
Net::HTTP.get('www.something.com', '/') # ===> Failure
|
282
288
|
|
283
289
|
### External requests can be disabled while allowing localhost
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
+
|
291
|
+
WebMock.disable_net_connect!(:allow_localhost => true)
|
292
|
+
|
293
|
+
Net::HTTP.get('www.something.com', '/') # ===> Failure
|
294
|
+
|
295
|
+
Net::HTTP.get('localhost:9887', '/') # ===> Allowed. Perhaps to Selenium?
|
290
296
|
|
291
297
|
### External requests can be disabled while allowing any hostname
|
292
298
|
|
293
|
-
|
299
|
+
WebMock.disable_net_connect!(:allow => "www.example.org")
|
294
300
|
|
295
|
-
|
301
|
+
Net::HTTP.get('www.something.com', '/') # ===> Failure
|
296
302
|
|
297
|
-
|
303
|
+
Net::HTTP.get('www.example.org', '/') # ===> Allowed.
|
298
304
|
|
299
305
|
## Connecting on Net::HTTP.start
|
300
306
|
|
@@ -311,84 +317,84 @@ so when there is no request, `Net::HTTP.start` doesn't do anything.
|
|
311
317
|
To workaround this issue, WebMock offers `:net_http_connect_on_start` option,
|
312
318
|
which can be passed to `WebMock.allow_net_connect!` and `WebMock#disable_net_connect!` methods, i.e.
|
313
319
|
|
314
|
-
|
320
|
+
WebMock.allow_net_connect!(:net_http_connect_on_start => true)
|
315
321
|
|
316
322
|
This forces WebMock Net::HTTP adapter to always connect on `Net::HTTP.start`.
|
317
323
|
|
318
324
|
## Setting Expectations
|
319
325
|
|
320
326
|
### Setting expectations in Test::Unit
|
321
|
-
|
327
|
+
require 'webmock/test_unit'
|
322
328
|
|
323
329
|
stub_request(:any, "www.example.com")
|
324
330
|
|
325
|
-
|
331
|
+
uri = URI.parse('http://www.example.com/')
|
326
332
|
req = Net::HTTP::Post.new(uri.path)
|
327
|
-
|
333
|
+
req['Content-Length'] = 3
|
328
334
|
res = Net::HTTP.start(uri.host, uri.port) {|http|
|
329
335
|
http.request(req, 'abc')
|
330
336
|
}
|
331
337
|
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
338
|
+
assert_requested :post, "http://www.example.com",
|
339
|
+
:headers => {'Content-Length' => 3}, :body => "abc", :times => 1 # ===> Success
|
340
|
+
|
341
|
+
assert_not_requested :get, "http://www.something.com" # ===> Success
|
336
342
|
|
337
|
-
|
343
|
+
assert_requested(:post, "http://www.example.com", :times => 1) { |req| req.body == "abc" }
|
338
344
|
|
339
345
|
### Expecting real (not stubbed) requests
|
340
346
|
|
341
|
-
|
342
|
-
|
343
|
-
Net::HTTP.get('www.example.com', '/') # ===> Success
|
347
|
+
WebMock.allow_net_connect!
|
344
348
|
|
345
|
-
|
349
|
+
Net::HTTP.get('www.example.com', '/') # ===> Success
|
350
|
+
|
351
|
+
assert_requested :get, "http://www.example.com" # ===> Success
|
346
352
|
|
347
353
|
|
348
354
|
### Setting expectations in RSpec
|
349
355
|
This style is borrowed from [fakeweb-matcher](http://github.com/freelancing-god/fakeweb-matcher)
|
350
356
|
|
351
|
-
|
357
|
+
require 'webmock/rspec'
|
358
|
+
|
359
|
+
WebMock.should have_requested(:get, "www.example.com").with(:body => "abc", :headers => {'Content-Length' => 3}).twice
|
360
|
+
|
361
|
+
WebMock.should_not have_requested(:get, "www.something.com")
|
352
362
|
|
353
|
-
|
354
|
-
|
355
|
-
WebMock.should_not have_requested(:get, "www.something.com")
|
356
|
-
|
357
|
-
WebMock.should have_requested(:post, "www.example.com").with { |req| req.body == "abc" }
|
358
|
-
|
359
|
-
WebMock.should have_requested(:get, "www.example.com").with(:query => {"a" => ["b", "c"]})
|
363
|
+
WebMock.should have_requested(:post, "www.example.com").with { |req| req.body == "abc" }
|
360
364
|
|
361
|
-
|
362
|
-
|
365
|
+
WebMock.should have_requested(:get, "www.example.com").with(:query => {"a" => ["b", "c"]})
|
366
|
+
|
367
|
+
WebMock.should have_requested(:get, "www.example.com").
|
368
|
+
with(:body => {"a" => ["b", "c"]}, :headers => 'Content-Type' => 'application/json')
|
363
369
|
|
364
370
|
### Different way of setting expectations in RSpec
|
365
371
|
|
366
|
-
|
372
|
+
a_request(:post, "www.example.com").with(:body => "abc", :headers => {'Content-Length' => 3}).should have_been_made.once
|
367
373
|
|
368
|
-
|
374
|
+
a_request(:post, "www.something.com").should have_been_made.times(3)
|
369
375
|
|
370
|
-
|
376
|
+
a_request(:any, "www.example.com").should_not have_been_made
|
371
377
|
|
372
|
-
|
378
|
+
a_request(:post, "www.example.com").with { |req| req.body == "abc" }.should have_been_made
|
373
379
|
|
374
|
-
|
380
|
+
a_request(:get, "www.example.com").with(:query => {"a" => ["b", "c"]}).should have_been_made
|
375
381
|
|
376
|
-
|
377
|
-
|
382
|
+
a_request(:post, "www.example.com").
|
383
|
+
with(:body => {"a" => ["b", "c"]}, :headers => 'Content-Type' => 'application/json').should have_been_made
|
378
384
|
|
379
385
|
## Clearing stubs and request history
|
380
386
|
|
381
387
|
If you want to reset all current stubs and history of requests use `WebMock.reset!`
|
382
388
|
|
383
|
-
|
389
|
+
stub_request(:any, "www.example.com")
|
384
390
|
|
385
|
-
|
391
|
+
Net::HTTP.get('www.example.com', '/') # ===> Success
|
386
392
|
|
387
|
-
|
393
|
+
WebMock.reset!
|
388
394
|
|
389
|
-
|
395
|
+
Net::HTTP.get('www.example.com', '/') # ===> Failure
|
390
396
|
|
391
|
-
|
397
|
+
assert_not_requested :get, "www.example.com" # ===> Success
|
392
398
|
|
393
399
|
|
394
400
|
## Matching requests
|
@@ -405,14 +411,14 @@ An executed request matches stubbed request if it passes following criteria:
|
|
405
411
|
|
406
412
|
Always the last declared stub matching the request will be applied i.e:
|
407
413
|
|
408
|
-
|
409
|
-
|
414
|
+
stub_request(:get, "www.example.com").to_return(:body => "abc")
|
415
|
+
stub_request(:get, "www.example.com").to_return(:body => "def")
|
410
416
|
|
411
|
-
|
417
|
+
Net::HTTP.get('www.example.com', '/') # ====> "def"
|
412
418
|
|
413
419
|
## Matching URIs
|
414
420
|
|
415
|
-
WebMock will match all different representations of the same URI.
|
421
|
+
WebMock will match all different representations of the same URI.
|
416
422
|
|
417
423
|
I.e all the following representations of the URI are equal:
|
418
424
|
|
@@ -424,36 +430,36 @@ I.e all the following representations of the URI are equal:
|
|
424
430
|
"http://www.example.com/"
|
425
431
|
"http://www.example.com:80"
|
426
432
|
"http://www.example.com:80/"
|
427
|
-
|
433
|
+
|
428
434
|
The following URIs with basic authentication are also equal for WebMock
|
429
435
|
|
430
|
-
|
431
|
-
|
432
|
-
|
433
|
-
|
434
|
-
|
435
|
-
|
436
|
-
|
437
|
-
|
438
|
-
|
439
|
-
|
440
|
-
|
441
|
-
|
442
|
-
|
443
|
-
|
444
|
-
|
445
|
-
|
436
|
+
"a b:pass@www.example.com"
|
437
|
+
"a b:pass@www.example.com/"
|
438
|
+
"a b:pass@www.example.com:80"
|
439
|
+
"a b:pass@www.example.com:80/"
|
440
|
+
"http://a b:pass@www.example.com"
|
441
|
+
"http://a b:pass@www.example.com/"
|
442
|
+
"http://a b:pass@www.example.com:80"
|
443
|
+
"http://a b:pass@www.example.com:80/"
|
444
|
+
"a%20b:pass@www.example.com"
|
445
|
+
"a%20b:pass@www.example.com/"
|
446
|
+
"a%20b:pass@www.example.com:80"
|
447
|
+
"a%20b:pass@www.example.com:80/"
|
448
|
+
"http://a%20b:pass@www.example.com"
|
449
|
+
"http://a%20b:pass@www.example.com/"
|
450
|
+
"http://a%20b:pass@www.example.com:80"
|
451
|
+
"http://a%20b:pass@www.example.com:80/"
|
446
452
|
|
447
453
|
or these
|
448
454
|
|
449
|
-
|
450
|
-
|
451
|
-
|
452
|
-
|
453
|
-
|
454
|
-
|
455
|
-
|
456
|
-
|
455
|
+
"www.example.com/my path/?a=my param&b=c"
|
456
|
+
"www.example.com/my%20path/?a=my%20param&b=c"
|
457
|
+
"www.example.com:80/my path/?a=my param&b=c"
|
458
|
+
"www.example.com:80/my%20path/?a=my%20param&b=c"
|
459
|
+
"http://www.example.com/my path/?a=my param&b=c"
|
460
|
+
"http://www.example.com/my%20path/?a=my%20param&b=c"
|
461
|
+
"http://www.example.com:80/my path/?a=my param&b=c"
|
462
|
+
"http://www.example.com:80/my%20path/?a=my%20param&b=c"
|
457
463
|
|
458
464
|
|
459
465
|
If you provide Regexp to match URI, WebMock will try to match it against every valid form of the same url.
|
@@ -509,6 +515,51 @@ If you have any suggestions on how to improve WebMock please send an email to th
|
|
509
515
|
|
510
516
|
I'm particularly interested in how the DSL could be improved.
|
511
517
|
|
518
|
+
## Development
|
519
|
+
|
520
|
+
In order to work on Webmock you first need to fork and clone the repo.
|
521
|
+
Please do any work on a dedicated branch and rebase against master
|
522
|
+
before sending a pull request.
|
523
|
+
|
524
|
+
#### Running Tests
|
525
|
+
|
526
|
+
We use RVM in order to test WebMock against 1.8.6, REE, 1.8.7, 1.9.2 and
|
527
|
+
jRuby. You can get RVM setup for WebMock development using the
|
528
|
+
following commands (if you don't have these version of Ruby installed
|
529
|
+
use `rvm install` to install each of them).
|
530
|
+
|
531
|
+
rvm use --create 1.8.6@webmock
|
532
|
+
gem install jeweler bundler
|
533
|
+
bundle install
|
534
|
+
|
535
|
+
rvm use --create ree@webmock
|
536
|
+
gem install jeweler bundler
|
537
|
+
bundle install
|
538
|
+
|
539
|
+
rvm use --create 1.8.7@webmock
|
540
|
+
gem install jeweler bundler
|
541
|
+
bundle install
|
542
|
+
|
543
|
+
rvm use --create 1.9.2@webmock
|
544
|
+
gem install jeweler bundler
|
545
|
+
bundle install
|
546
|
+
|
547
|
+
rvm use --create jruby@webmock
|
548
|
+
gem install jeweler bundler
|
549
|
+
bundle install
|
550
|
+
|
551
|
+
These commands will create a gemset named WebMock for each of the
|
552
|
+
supported versions of Ruby and `bundle install` all dependencies.
|
553
|
+
|
554
|
+
With the supported versions of Ruby installed RVM will run specs across
|
555
|
+
all version with just one command.
|
556
|
+
|
557
|
+
bundle exec rvm 1.8.6@webmock,ree@webmock,1.8.7@webmock,1.9.2@webmock,jruby@webmock rspec spec/**/*_spec.rb
|
558
|
+
|
559
|
+
This command is wrapped up in to a rake task and can be invoked like so:
|
560
|
+
|
561
|
+
rake spec:rubies
|
562
|
+
|
512
563
|
## Credits
|
513
564
|
|
514
565
|
The initial lines of this project were written during New Bamboo [Hack Day](http://blog.new-bamboo.co.uk/2009/11/13/hackday-results)
|
@@ -547,10 +598,13 @@ People who submitted patches and new features or suggested improvements. Many th
|
|
547
598
|
* Sam Stokes
|
548
599
|
* Eugene Bolshakov
|
549
600
|
|
601
|
+
For a full list of contributors you can visit the
|
602
|
+
[contributors](https://github.com/bblimke/webmock/contributors) page.
|
603
|
+
|
550
604
|
## Background
|
551
605
|
|
552
606
|
Thank you Fakeweb! This library was inspired by [FakeWeb](fakeweb.rubyforge.org).
|
553
|
-
I imported some solutions from that project to WebMock. I also copied some code i.e Net:HTTP adapter.
|
607
|
+
I imported some solutions from that project to WebMock. I also copied some code i.e Net:HTTP adapter.
|
554
608
|
Fakeweb architecture unfortunately didn't allow me to extend it easily with the features I needed.
|
555
609
|
I also preferred some things to work differently i.e request stub precedence.
|
556
610
|
|