Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 614 lines (390 sloc) 20.744 kb
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
1 WebMock
2 =======
3
0016b13 Minor changes to README
Bartosz Blimke authored
4 Library for stubbing and setting expectations on HTTP requests in Ruby.
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
5
7c2b9cf @bblimke WebMock is looking for a maintainer
authored
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
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
12 Features
13 --------
14
0016b13 Minor changes to README
Bartosz Blimke authored
15 * Stubbing HTTP requests at low http client lib level (no need to change tests when you change HTTP library)
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
16 * Setting and verifying expectations on HTTP requests
17 * Matching requests based on method, URI, headers and body
18 * Smart matching of the same URIs in different representations (also encoded and non encoded forms)
19 * Smart matching of the same headers in different representations.
9c8c463 Updated README with information about RSpec 2 support.
Bartosz Blimke authored
20 * Support for Test::Unit
21 * Support for RSpec 1.x and RSpec 2.x
fb18a01 Updated README with info about matching request body against hash
Bartosz Blimke authored
22
23 Supported HTTP libraries
24 ------------------------
25
3d84ef6 Added EM-HTTP-Request to the list of supported http clients
Bartosz Blimke authored
26 * Net::HTTP and libraries based on Net::HTTP (i.e RightHttpConnection, REST Client, HTTParty)
fb18a01 Updated README with info about matching request body against hash
Bartosz Blimke authored
27 * HTTPClient
28 * Patron
3d84ef6 Added EM-HTTP-Request to the list of supported http clients
Bartosz Blimke authored
29 * EM-HTTP-Request
ed9ff32 @bblimke WebMock currently doesn't support Curb::Multi
authored
30 * Curb (currently only Curb::Easy)
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
31
f25a213 @bblimke Improved README on how to include WebMock in Test::Unit, RSpec and Cu…
authored
32 ##Installation
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
33
221e8cd @jcf Document setting up a WebMock development environment
jcf authored
34 gem install webmock --source http://gemcutter.org
7a8bab1 @bblimke Added info on how to install the latest development version of WebMoc…
authored
35
36 ### or to install the latest development version from github master
37
221e8cd @jcf Document setting up a WebMock development environment
jcf authored
38 git clone http://github.com/bblimke/webmock.git
39 cd webmock
40 rake install
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
41
221e8cd @jcf Document setting up a WebMock development environment
jcf authored
42 ### Test::Unit
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
43
f25a213 @bblimke Improved README on how to include WebMock in Test::Unit, RSpec and Cu…
authored
44 Add the following code to `test/test_helper.rb`
45
221e8cd @jcf Document setting up a WebMock development environment
jcf authored
46 require 'webmock/test_unit'
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
47
f25a213 @bblimke Improved README on how to include WebMock in Test::Unit, RSpec and Cu…
authored
48 ### RSpec
221e8cd @jcf Document setting up a WebMock development environment
jcf authored
49
f25a213 @bblimke Improved README on how to include WebMock in Test::Unit, RSpec and Cu…
authored
50 Add the following code to `spec/spec_helper`:
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
51
221e8cd @jcf Document setting up a WebMock development environment
jcf authored
52 require 'webmock/rspec'
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
53
f25a213 @bblimke Improved README on how to include WebMock in Test::Unit, RSpec and Cu…
authored
54 ### Cucumber
55
56 Add the following code to `features/support/env.rb`
3ca443e WebMock module has to be included manually and is not included by RSp…
Bartosz Blimke authored
57
221e8cd @jcf Document setting up a WebMock development environment
jcf authored
58 require 'webmock/cucumber'
f25a213 @bblimke Improved README on how to include WebMock in Test::Unit, RSpec and Cu…
authored
59
60 You can also use WebMock outside a test framework:
61
221e8cd @jcf Document setting up a WebMock development environment
jcf authored
62 require 'webmock'
63 include WebMock::API
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
64
65 ## Examples
66
67
68
69 ## Stubbing
70
71
72 ### Stubbed request based on uri only and with the default response
73
221e8cd @jcf Document setting up a WebMock development environment
jcf authored
74 stub_request(:any, "www.example.com")
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
75
221e8cd @jcf Document setting up a WebMock development environment
jcf authored
76 Net::HTTP.get("www.example.com", "/") # ===> Success
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
77
78 ### Stubbing requests based on method, uri, body and headers
79
221e8cd @jcf Document setting up a WebMock development environment
jcf authored
80 stub_request(:post, "www.example.com").with(:body => "abc", :headers => { 'Content-Length' => 3 })
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
81
221e8cd @jcf Document setting up a WebMock development environment
jcf authored
82 uri = URI.parse("http://www.example.com/")
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
83 req = Net::HTTP::Post.new(uri.path)
221e8cd @jcf Document setting up a WebMock development environment
jcf authored
84 req['Content-Length'] = 3
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
85 res = Net::HTTP.start(uri.host, uri.port) {|http|
86 http.request(req, "abc")
87 } # ===> Success
88
5313e02 Added version 0.9.0 features to CHANGELOG and README
Bartosz Blimke authored
89 ### Matching request body and headers against regular expressions
90
221e8cd @jcf Document setting up a WebMock development environment
jcf authored
91 stub_request(:post, "www.example.com").
92 with(:body => /^.*world$/, :headers => {"Content-Type" => /image\/.+/}).to_return(:body => "abc")
5313e02 Added version 0.9.0 features to CHANGELOG and README
Bartosz Blimke authored
93
221e8cd @jcf Document setting up a WebMock development environment
jcf authored
94 uri = URI.parse('http://www.example.com/')
5313e02 Added version 0.9.0 features to CHANGELOG and README
Bartosz Blimke authored
95 req = Net::HTTP::Post.new(uri.path)
221e8cd @jcf Document setting up a WebMock development environment
jcf authored
96 req['Content-Type'] = 'image/png'
5313e02 Added version 0.9.0 features to CHANGELOG and README
Bartosz Blimke authored
97 res = Net::HTTP.start(uri.host, uri.port) {|http|
98 http.request(req, 'hello world')
99 } # ===> Success
221e8cd @jcf Document setting up a WebMock development environment
jcf authored
100
354af66 Updated CHANGELOG width version 1.3.0 changes
Bartosz Blimke authored
101 ### Matching request body against a hash. Body can be URL-Encoded, JSON or XML.
fb18a01 Updated README with info about matching request body against hash
Bartosz Blimke authored
102
221e8cd @jcf Document setting up a WebMock development environment
jcf authored
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
fb18a01 Updated README with info about matching request body against hash
Bartosz Blimke authored
108
221e8cd @jcf Document setting up a WebMock development environment
jcf authored
109 RestClient.post('www.example.com', '{"data":{"a":"1","b":"five"}}',
110 :content_type => 'application/json') # ===> Success
111
112 RestClient.post('www.example.com', '<data a="1" b="five" />',
113 :content_type => 'application/xml' ) # ===> Success
5313e02 Added version 0.9.0 features to CHANGELOG and README
Bartosz Blimke authored
114
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
115 ### Matching custom request headers
116
0016b13 Minor changes to README
Bartosz Blimke authored
117 stub_request(:any, "www.example.com").with(:headers=>{ 'Header-Name' => 'Header-Value' })
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
118
221e8cd @jcf Document setting up a WebMock development environment
jcf authored
119 uri = URI.parse('http://www.example.com/')
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
120 req = Net::HTTP::Post.new(uri.path)
221e8cd @jcf Document setting up a WebMock development environment
jcf authored
121 req['Header-Name'] = 'Header-Value'
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
122 res = Net::HTTP.start(uri.host, uri.port) {|http|
123 http.request(req, 'abc')
124 } # ===> Success
125
176482c Updated README with 1.1.0 changes
Bartosz Blimke authored
126 ### Matching multiple headers with the same name
127
221e8cd @jcf Document setting up a WebMock development environment
jcf authored
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
176482c Updated README with 1.1.0 changes
Bartosz Blimke authored
134
5313e02 Added version 0.9.0 features to CHANGELOG and README
Bartosz Blimke authored
135 ### Matching requests against provided block
136
221e8cd @jcf Document setting up a WebMock development environment
jcf authored
137 stub_request(:post, "www.example.com").with { |request| request.body == "abc" }
138 RestClient.post('www.example.com', 'abc') # ===> Success
139
5313e02 Added version 0.9.0 features to CHANGELOG and README
Bartosz Blimke authored
140 ### Request with basic authentication
141
142 stub_request(:get, "user:pass@www.example.com")
143
144 Net::HTTP.start('www.example.com') {|http|
145 req = Net::HTTP::Get.new('/')
146 req.basic_auth 'user', 'pass'
147 http.request(req)
148 } # ===> Success
149
150 ### Matching uris using regular expressions
151
221e8cd @jcf Document setting up a WebMock development environment
jcf authored
152 stub_request(:any, /.*example.*/)
5313e02 Added version 0.9.0 features to CHANGELOG and README
Bartosz Blimke authored
153
221e8cd @jcf Document setting up a WebMock development environment
jcf authored
154 Net::HTTP.get('www.example.com', '/') # ===> Success
155
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
5313e02 Added version 0.9.0 features to CHANGELOG and README
Bartosz Blimke authored
161
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
162 ### Stubbing with custom response
163
221e8cd @jcf Document setting up a WebMock development environment
jcf authored
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"
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
167
0f3bb21 Updated README regarding dynamic responses
Bartosz Blimke authored
168 ### Response with body specified as IO object
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
169
221e8cd @jcf Document setting up a WebMock development environment
jcf authored
170 File.open('/tmp/response_body.txt', 'w') { |f| f.puts 'abc' }
171
172 stub_request(:any, "www.example.com").to_return(:body => File.new('/tmp/response_body.txt'), :status => 200)
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
173
221e8cd @jcf Document setting up a WebMock development environment
jcf authored
174 Net::HTTP.get('www.example.com', '/') # ===> "abc\n"
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
175
3314aad Added support for status messages
Bartosz Blimke authored
176 ### Response with custom status message
177
221e8cd @jcf Document setting up a WebMock development environment
jcf authored
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"
3314aad Added support for status messages
Bartosz Blimke authored
182
5313e02 Added version 0.9.0 features to CHANGELOG and README
Bartosz Blimke authored
183 ### Replaying raw responses recorded with `curl -is`
184
221e8cd @jcf Document setting up a WebMock development environment
jcf authored
185 `curl -is www.example.com > /tmp/example_curl_-is_output.txt`
186 raw_response_file = File.new("/tmp/example_curl_-is_output.txt")
5313e02 Added version 0.9.0 features to CHANGELOG and README
Bartosz Blimke authored
187
188 from file
189
221e8cd @jcf Document setting up a WebMock development environment
jcf authored
190 stub_request(:get, "www.example.com").to_return(raw_response_file)
5313e02 Added version 0.9.0 features to CHANGELOG and README
Bartosz Blimke authored
191
192 or string
193
221e8cd @jcf Document setting up a WebMock development environment
jcf authored
194 stub_request(:get, "www.example.com").to_return(raw_response_file.read)
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
195
0f3bb21 Updated README regarding dynamic responses
Bartosz Blimke authored
196 ### Responses dynamically evaluated from block
197
4e0568a Added information about dynamic responses to README
Bartosz Blimke authored
198 stub_request(:any, 'www.example.net').
0f3bb21 Updated README regarding dynamic responses
Bartosz Blimke authored
199 to_return { |request| {:body => request.body} }
4e0568a Added information about dynamic responses to README
Bartosz Blimke authored
200
0f3bb21 Updated README regarding dynamic responses
Bartosz Blimke authored
201 RestClient.post('www.example.net', 'abc') # ===> "abc\n"
f0408f8 @bblimke Added support for dynamically evaluated raw responses recorded with `…
authored
202
0f3bb21 Updated README regarding dynamic responses
Bartosz Blimke authored
203 ### Responses dynamically evaluated from lambda
f0408f8 @bblimke Added support for dynamically evaluated raw responses recorded with `…
authored
204
5b1c97b @tomafro Added complete dynamic request, where headers, body and status can al…
tomafro authored
205 stub_request(:any, 'www.example.net').
206 to_return(lambda { |request| {:body => request.body} })
207
221e8cd @jcf Document setting up a WebMock development environment
jcf authored
208 RestClient.post('www.example.net', 'abc') # ===> "abc\n"
4e0568a Added information about dynamic responses to README
Bartosz Blimke authored
209
f0408f8 @bblimke Added support for dynamically evaluated raw responses recorded with `…
authored
210 ### Dynamically evaluated raw responses recorded with `curl -is`
211
212 `curl -is www.example.com > /tmp/www.example.com.txt`
213 stub_request(:get, "www.example.com").to_return(lambda { |request| File.new("/tmp/#{request.uri.host.to_s}.txt" }))
214
0f3bb21 Updated README regarding dynamic responses
Bartosz Blimke authored
215 ### Responses with dynamically evaluated parts
216
217 stub_request(:any, 'www.example.net').
218 to_return(:body => lambda { |request| request.body })
219
221e8cd @jcf Document setting up a WebMock development environment
jcf authored
220 RestClient.post('www.example.net', 'abc') # ===> "abc\n"
0f3bb21 Updated README regarding dynamic responses
Bartosz Blimke authored
221
af52fe7 Added support for declaring timeout errors using `to_timeout`
Bartosz Blimke authored
222 ### Raising errors
223
207443e `to_raise()` now accepts exception instance or a string as argument i…
Bartosz Blimke authored
224 #### Exception declared by class
225
221e8cd @jcf Document setting up a WebMock development environment
jcf authored
226 stub_request(:any, 'www.example.net').to_raise(StandardError)
af52fe7 Added support for declaring timeout errors using `to_timeout`
Bartosz Blimke authored
227
228 RestClient.post('www.example.net', 'abc') # ===> StandardError
221e8cd @jcf Document setting up a WebMock development environment
jcf authored
229
207443e `to_raise()` now accepts exception instance or a string as argument i…
Bartosz Blimke authored
230 #### or by exception instance
231
232 stub_request(:any, 'www.example.net').to_raise(StandardError.new("some error"))
233
234 #### or by string
221e8cd @jcf Document setting up a WebMock development environment
jcf authored
235
207443e `to_raise()` now accepts exception instance or a string as argument i…
Bartosz Blimke authored
236 stub_request(:any, 'www.example.net').to_raise("some error")
af52fe7 Added support for declaring timeout errors using `to_timeout`
Bartosz Blimke authored
237
238 ### Raising timeout errors
239
221e8cd @jcf Document setting up a WebMock development environment
jcf authored
240 stub_request(:any, 'www.example.net').to_timeout
af52fe7 Added support for declaring timeout errors using `to_timeout`
Bartosz Blimke authored
241
221e8cd @jcf Document setting up a WebMock development environment
jcf authored
242 RestClient.post('www.example.net', 'abc') # ===> RestClient::RequestTimeout
af52fe7 Added support for declaring timeout errors using `to_timeout`
Bartosz Blimke authored
243
5313e02 Added version 0.9.0 features to CHANGELOG and README
Bartosz Blimke authored
244 ### Multiple responses for repeated requests
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
245
221e8cd @jcf Document setting up a WebMock development environment
jcf authored
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"
bb41263 Improved section about basic authentication support in readme.
Bartosz Blimke authored
253
af52fe7 Added support for declaring timeout errors using `to_timeout`
Bartosz Blimke authored
254 ### Multiple responses using chained `to_return()`, `to_raise()` or `to_timeout` declarations
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
255
221e8cd @jcf Document setting up a WebMock development environment
jcf authored
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
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
263
5313e02 Added version 0.9.0 features to CHANGELOG and README
Bartosz Blimke authored
264 ### Specifying number of times given response should be returned
265
221e8cd @jcf Document setting up a WebMock development environment
jcf authored
266 stub_request(:get, "www.example.com").
267 to_return({:body => "abc"}).times(2).then.
268 to_return({:body => "def"})
5313e02 Added version 0.9.0 features to CHANGELOG and README
Bartosz Blimke authored
269
221e8cd @jcf Document setting up a WebMock development environment
jcf authored
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"
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
273
274
275 ### Real requests to network can be allowed or disabled
276
221e8cd @jcf Document setting up a WebMock development environment
jcf authored
277 WebMock.allow_net_connect!
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
278
221e8cd @jcf Document setting up a WebMock development environment
jcf authored
279 stub_request(:any, "www.example.com").to_return(:body => "abc")
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
280
221e8cd @jcf Document setting up a WebMock development environment
jcf authored
281 Net::HTTP.get('www.example.com', '/') # ===> "abc"
282
283 Net::HTTP.get('www.something.com', '/') # ===> /.+Something.+/
284
285 WebMock.disable_net_connect!
286
287 Net::HTTP.get('www.something.com', '/') # ===> Failure
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
288
8859deb @macksmind Change the locahost option syntax and update README
macksmind authored
289 ### External requests can be disabled while allowing localhost
221e8cd @jcf Document setting up a WebMock development environment
jcf authored
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?
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
296
67a0819 @bblimke Version 1.3.5 changelog + updated docs
authored
297 ### External requests can be disabled while allowing any hostname
298
221e8cd @jcf Document setting up a WebMock development environment
jcf authored
299 WebMock.disable_net_connect!(:allow => "www.example.org")
67a0819 @bblimke Version 1.3.5 changelog + updated docs
authored
300
221e8cd @jcf Document setting up a WebMock development environment
jcf authored
301 Net::HTTP.get('www.something.com', '/') # ===> Failure
67a0819 @bblimke Version 1.3.5 changelog + updated docs
authored
302
221e8cd @jcf Document setting up a WebMock development environment
jcf authored
303 Net::HTTP.get('www.example.org', '/') # ===> Allowed.
67a0819 @bblimke Version 1.3.5 changelog + updated docs
authored
304
4e313c2 @bblimke Updated CHANGELOG and README with information about net_http_connect_…
authored
305 ## Connecting on Net::HTTP.start
306
c92144f @bblimke Fixed text formatting in README
authored
307 HTTP protocol has 3 steps: connect, request and response (or 4 with close). Most Ruby HTTP client libraries
308 treat connect as a part of request step, with the exception of `Net::HTTP` which
309 allows opening connection to the server separately to the request, by using `Net::HTTP.start`.
4e313c2 @bblimke Updated CHANGELOG and README with information about net_http_connect_…
authored
310
c92144f @bblimke Fixed text formatting in README
authored
311 WebMock API was also designed with connect being part of request step, and it only allows stubbing
312 requests, not connections. When `Net::HTTP.start` is called, WebMock doesn't know yet whether
313 a request is stubbed or not. WebMock by default delays a connection until the request is invoked,
314 so when there is no request, `Net::HTTP.start` doesn't do anything.
315 **This means that WebMock breaks the Net::HTTP behaviour by default!**
4e313c2 @bblimke Updated CHANGELOG and README with information about net_http_connect_…
authored
316
079a719 @bblimke Updated changelog with version 1.5.0 changes
authored
317 To workaround this issue, WebMock offers `:net_http_connect_on_start` option,
318 which can be passed to `WebMock.allow_net_connect!` and `WebMock#disable_net_connect!` methods, i.e.
4e313c2 @bblimke Updated CHANGELOG and README with information about net_http_connect_…
authored
319
221e8cd @jcf Document setting up a WebMock development environment
jcf authored
320 WebMock.allow_net_connect!(:net_http_connect_on_start => true)
4e313c2 @bblimke Updated CHANGELOG and README with information about net_http_connect_…
authored
321
c92144f @bblimke Fixed text formatting in README
authored
322 This forces WebMock Net::HTTP adapter to always connect on `Net::HTTP.start`.
4e313c2 @bblimke Updated CHANGELOG and README with information about net_http_connect_…
authored
323
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
324 ## Setting Expectations
325
326 ### Setting expectations in Test::Unit
221e8cd @jcf Document setting up a WebMock development environment
jcf authored
327 require 'webmock/test_unit'
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
328
32c653c Replaced google with example in README
Bartosz Blimke authored
329 stub_request(:any, "www.example.com")
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
330
221e8cd @jcf Document setting up a WebMock development environment
jcf authored
331 uri = URI.parse('http://www.example.com/')
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
332 req = Net::HTTP::Post.new(uri.path)
221e8cd @jcf Document setting up a WebMock development environment
jcf authored
333 req['Content-Length'] = 3
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
334 res = Net::HTTP.start(uri.host, uri.port) {|http|
335 http.request(req, 'abc')
336 }
337
221e8cd @jcf Document setting up a WebMock development environment
jcf authored
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
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
342
221e8cd @jcf Document setting up a WebMock development environment
jcf authored
343 assert_requested(:post, "http://www.example.com", :times => 1) { |req| req.body == "abc" }
5313e02 Added version 0.9.0 features to CHANGELOG and README
Bartosz Blimke authored
344
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
345 ### Expecting real (not stubbed) requests
346
221e8cd @jcf Document setting up a WebMock development environment
jcf authored
347 WebMock.allow_net_connect!
348
349 Net::HTTP.get('www.example.com', '/') # ===> Success
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
350
221e8cd @jcf Document setting up a WebMock development environment
jcf authored
351 assert_requested :get, "http://www.example.com" # ===> Success
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
352
353
354 ### Setting expectations in RSpec
355 This style is borrowed from [fakeweb-matcher](http://github.com/freelancing-god/fakeweb-matcher)
356
221e8cd @jcf Document setting up a WebMock development environment
jcf authored
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")
362
363 WebMock.should have_requested(:post, "www.example.com").with { |req| req.body == "abc" }
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
364
221e8cd @jcf Document setting up a WebMock development environment
jcf authored
365 WebMock.should have_requested(:get, "www.example.com").with(:query => {"a" => ["b", "c"]})
fa4ab29 Updated README with info about setting expectations on query params o…
Bartosz Blimke authored
366
221e8cd @jcf Document setting up a WebMock development environment
jcf authored
367 WebMock.should have_requested(:get, "www.example.com").
c109d95 @fb3 Fixed syntax error in README examples
fb3 authored
368 with(:body => {"a" => ["b", "c"]}, :headers => {'Content-Type' => 'application/json'})
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
369
370 ### Different way of setting expectations in RSpec
371
221e8cd @jcf Document setting up a WebMock development environment
jcf authored
372 a_request(:post, "www.example.com").with(:body => "abc", :headers => {'Content-Length' => 3}).should have_been_made.once
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
373
221e8cd @jcf Document setting up a WebMock development environment
jcf authored
374 a_request(:post, "www.something.com").should have_been_made.times(3)
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
375
221e8cd @jcf Document setting up a WebMock development environment
jcf authored
376 a_request(:any, "www.example.com").should_not have_been_made
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
377
221e8cd @jcf Document setting up a WebMock development environment
jcf authored
378 a_request(:post, "www.example.com").with { |req| req.body == "abc" }.should have_been_made
6cb45ed @bblimke Renames WebMock#request to WebMock#a_request to avoid conflicts with …
authored
379
221e8cd @jcf Document setting up a WebMock development environment
jcf authored
380 a_request(:get, "www.example.com").with(:query => {"a" => ["b", "c"]}).should have_been_made
6cb45ed @bblimke Renames WebMock#request to WebMock#a_request to avoid conflicts with …
authored
381
221e8cd @jcf Document setting up a WebMock development environment
jcf authored
382 a_request(:post, "www.example.com").
c109d95 @fb3 Fixed syntax error in README examples
fb3 authored
383 with(:body => {"a" => ["b", "c"]}, :headers => {'Content-Type' => 'application/json'}).should have_been_made
6cb45ed @bblimke Renames WebMock#request to WebMock#a_request to avoid conflicts with …
authored
384
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
385 ## Clearing stubs and request history
386
473b709 @bblimke Added WebMock.reset! method. WebMock.reset_webmock is now deprecated
authored
387 If you want to reset all current stubs and history of requests use `WebMock.reset!`
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
388
221e8cd @jcf Document setting up a WebMock development environment
jcf authored
389 stub_request(:any, "www.example.com")
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
390
221e8cd @jcf Document setting up a WebMock development environment
jcf authored
391 Net::HTTP.get('www.example.com', '/') # ===> Success
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
392
221e8cd @jcf Document setting up a WebMock development environment
jcf authored
393 WebMock.reset!
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
394
221e8cd @jcf Document setting up a WebMock development environment
jcf authored
395 Net::HTTP.get('www.example.com', '/') # ===> Failure
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
396
221e8cd @jcf Document setting up a WebMock development environment
jcf authored
397 assert_not_requested :get, "www.example.com" # ===> Success
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
398
399
400 ## Matching requests
401
402 An executed request matches stubbed request if it passes following criteria:
403
4306efa Ability to register and expect requests with empty body.
Bartosz Blimke authored
404 When request URI matches stubbed request URI string or Regexp pattern<br/>
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
405 And request method is the same as stubbed request method or stubbed request method is :any<br/>
4306efa Ability to register and expect requests with empty body.
Bartosz Blimke authored
406 And request body is the same as stubbed request body or stubbed request body is not specified<br/>
0016b13 Minor changes to README
Bartosz Blimke authored
407 And request headers match stubbed request headers, or stubbed request headers match a subset of request headers, or stubbed request headers are not specified<br/>
408 And request matches provided block or block is not provided
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
409
410 ## Precedence of stubs
411
412 Always the last declared stub matching the request will be applied i.e:
413
221e8cd @jcf Document setting up a WebMock development environment
jcf authored
414 stub_request(:get, "www.example.com").to_return(:body => "abc")
415 stub_request(:get, "www.example.com").to_return(:body => "def")
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
416
221e8cd @jcf Document setting up a WebMock development environment
jcf authored
417 Net::HTTP.get('www.example.com', '/') # ====> "def"
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
418
419 ## Matching URIs
420
221e8cd @jcf Document setting up a WebMock development environment
jcf authored
421 WebMock will match all different representations of the same URI.
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
422
423 I.e all the following representations of the URI are equal:
424
32c653c Replaced google with example in README
Bartosz Blimke authored
425 "www.example.com"
426 "www.example.com/"
427 "www.example.com:80"
428 "www.example.com:80/"
429 "http://www.example.com"
430 "http://www.example.com/"
431 "http://www.example.com:80"
432 "http://www.example.com:80/"
221e8cd @jcf Document setting up a WebMock development environment
jcf authored
433
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
434 The following URIs with basic authentication are also equal for WebMock
435
221e8cd @jcf Document setting up a WebMock development environment
jcf authored
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/"
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
452
453 or these
454
221e8cd @jcf Document setting up a WebMock development environment
jcf authored
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"
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
463
464
465 If you provide Regexp to match URI, WebMock will try to match it against every valid form of the same url.
466
78414b7 Improved examples with equality of URIs with the same parameters
Bartosz Blimke authored
467 I.e `/.*my param.*/` will match `www.example.com/my%20path` because it is equivalent of `www.example.com/my path`
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
468
469
470 ## Matching headers
471
472 WebMock will match request headers against stubbed request headers in the following situations:
473
474 1. Stubbed request has headers specified and request headers are the same as stubbed headers <br/>
475 i.e stubbed headers: `{ 'Header1' => 'Value1', 'Header1' => 'Value1' }`, requested: `{ 'Header1' => 'Value1', 'Header1' => 'Value1' }`
476
477 2. Stubbed request has headers specified and stubbed request headers are a subset of request headers <br/>
478 i.e stubbed headers: `{ 'Header1' => 'Value1' }`, requested: `{ 'Header1' => 'Value1', 'Header1' => 'Value1' }`
479
480 3. Stubbed request has no headers <br/>
481 i.e stubbed headers: `nil`, requested: `{ 'Header1' => 'Value1', 'Header1' => 'Value1' }`
482
483 WebMock normalises headers and treats all forms of same headers as equal:
484 i.e the following two sets of headers are equal:
485
486 `{ "Header1" => "value1", :content_length => 123, :X_CuStOm_hEAder => :value }`
487
488 `{ :header1 => "value1", "Content-Length" => 123, "x-cuSTOM-HeAder" => "value" }`
489
176482c Updated README with 1.1.0 changes
Bartosz Blimke authored
490 ## Recording real requests and responses and replaying them later
491
492 To record your application's real HTTP interactions and replay them later in tests you can use [VCR](http://github.com/myronmarston/vcr) with WebMock.
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
493
5331132 Updated README with information about request callbacks
Bartosz Blimke authored
494 ## Request callbacks
495
354af66 Updated CHANGELOG width version 1.3.0 changes
Bartosz Blimke authored
496 ####WebMock can invoke callbacks stubbed or real requests:
5331132 Updated README with information about request callbacks
Bartosz Blimke authored
497
498 WebMock.after_request do |request_signature, response|
354af66 Updated CHANGELOG width version 1.3.0 changes
Bartosz Blimke authored
499 puts "Request #{request_signature} was made and #{response} was returned"
5331132 Updated README with information about request callbacks
Bartosz Blimke authored
500 end
501
502 #### invoke callbacks for real requests only and except requests made with Patron
503
504 WebMock.after_request(:except => [:patron], :real_requests_only => true) do |request_signature, response|
354af66 Updated CHANGELOG width version 1.3.0 changes
Bartosz Blimke authored
505 puts "Request #{request_signature} was made and #{response} was returned"
5331132 Updated README with information about request callbacks
Bartosz Blimke authored
506 end
507
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
508 ## Bugs and Issues
509
510 Please submit them here [http://github.com/bblimke/webmock/issues](http://github.com/bblimke/webmock/issues)
511
512 ## Suggestions
513
514 If you have any suggestions on how to improve WebMock please send an email to the mailing list [groups.google.com/group/webmock-users](http://groups.google.com/group/webmock-users)
515
516 I'm particularly interested in how the DSL could be improved.
517
221e8cd @jcf Document setting up a WebMock development environment
jcf authored
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
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
563 ## Credits
564
00424d9 Updated credits in README
Bartosz Blimke authored
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)
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
566 Thanks to my fellow [Bambinos](http://new-bamboo.co.uk/) for all the great suggestions!
567
00424d9 Updated credits in README
Bartosz Blimke authored
568 People who submitted patches and new features or suggested improvements. Many thanks to these people:
569
570 * Ben Pickles
571 * Mark Evans
572 * Ivan Vega
573 * Piotr Usewicz
574 * Nick Plante
575 * Nick Quaranto
576 * Diego E. "Flameeyes" Pettenò
577 * Niels Meersschaert
578 * Mack Earnhardt
579 * Arvicco
580 * Sergio Gil
581 * Jeffrey Jones
582 * Tekin Suleyman
583 * Tom Ward
584 * Nadim Bitar
ee7b3b4 Fixed spelling
Bartosz Blimke authored
585 * Myron Marston
9c8c463 Updated README with information about RSpec 2 support.
Bartosz Blimke authored
586 * Sam Phillips
2e88f99 Updated info on how to include WebMock in Test::Unit and RSpec withou…
Bartosz Blimke authored
587 * Jose Angel Cortinas
52698d8 Updated credits
Bartosz Blimke authored
588 * Razic
fd2472a Version 1.2.2 changelog
Bartosz Blimke authored
589 * Steve Tooke
fb18a01 Updated README with info about matching request body against hash
Bartosz Blimke authored
590 * Nathaniel Bibler
15f888e @bblimke The less hacky way to get the stream behaviour working for em-http-re…
authored
591 * Martyn Loughran
60fa244 @bblimke Updated changelog
authored
592 * Muness Alrubaie
67a0819 @bblimke Version 1.3.5 changelog + updated docs
authored
593 * Charles Li
594 * Ryan Bigg
aaf774b @bblimke Added info to changelog and readme about Curb support, implemented by…
authored
595 * Pete Higgins
ffbcdb3 @bblimke Updated changelog with the latest changes in master branch
authored
596 * Hans de Graaff
4e313c2 @bblimke Updated CHANGELOG and README with information about net_http_connect_…
authored
597 * Alastair Brunton
e6af079 @bblimke Updated 1.6.2 changelog and credits
authored
598 * Sam Stokes
599 * Eugene Bolshakov
00424d9 Updated credits in README
Bartosz Blimke authored
600
221e8cd @jcf Document setting up a WebMock development environment
jcf authored
601 For a full list of contributors you can visit the
602 [contributors](https://github.com/bblimke/webmock/contributors) page.
603
00424d9 Updated credits in README
Bartosz Blimke authored
604 ## Background
605
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
606 Thank you Fakeweb! This library was inspired by [FakeWeb](fakeweb.rubyforge.org).
221e8cd @jcf Document setting up a WebMock development environment
jcf authored
607 I imported some solutions from that project to WebMock. I also copied some code i.e Net:HTTP adapter.
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
608 Fakeweb architecture unfortunately didn't allow me to extend it easily with the features I needed.
609 I also preferred some things to work differently i.e request stub precedence.
610
611 ## Copyright
612
bf18e89 Changed license from GPL to MIT
Bartosz Blimke authored
613 Copyright (c) 2009-2010 Bartosz Blimke. See LICENSE for details.
Something went wrong with that request. Please try again.