Skip to content
This repository
Newer
Older
100644 663 lines (427 sloc) 21.919 kb
4af51995 »
2011-08-09 Use ssl for Travis status icon so that GitHub does not proxy and cach…
1 WebMock [![Build Status](https://secure.travis-ci.org/bblimke/webmock.png)](http://travis-ci.org/bblimke/webmock)
95e82980 »
2009-11-24 Revert "WebMock module has to be included manually and is not include…
2 =======
3
0016b136 »
2010-01-31 Minor changes to README
4 Library for stubbing and setting expectations on HTTP requests in Ruby.
95e82980 »
2009-11-24 Revert "WebMock module has to be included manually and is not include…
5
6 Features
7 --------
8
0016b136 »
2010-01-31 Minor changes to README
9 * Stubbing HTTP requests at low http client lib level (no need to change tests when you change HTTP library)
95e82980 »
2009-11-24 Revert "WebMock module has to be included manually and is not include…
10 * Setting and verifying expectations on HTTP requests
11 * Matching requests based on method, URI, headers and body
12 * Smart matching of the same URIs in different representations (also encoded and non encoded forms)
13 * Smart matching of the same headers in different representations.
9c8c4635 »
2010-05-14 Updated README with information about RSpec 2 support.
14 * Support for Test::Unit
15 * Support for RSpec 1.x and RSpec 2.x
5627070a »
2011-08-09 Updated README with info about MiniTest support.
16 * Support for MiniTest
fb18a017 »
2010-06-19 Updated README with info about matching request body against hash
17
18 Supported HTTP libraries
19 ------------------------
20
3d84ef6a »
2010-06-20 Added EM-HTTP-Request to the list of supported http clients
21 * Net::HTTP and libraries based on Net::HTTP (i.e RightHttpConnection, REST Client, HTTParty)
fb18a017 »
2010-06-19 Updated README with info about matching request body against hash
22 * HTTPClient
23 * Patron
3d84ef6a »
2010-06-20 Added EM-HTTP-Request to the list of supported http clients
24 * EM-HTTP-Request
ed9ff32e »
2010-11-15 WebMock currently doesn't support Curb::Multi
25 * Curb (currently only Curb::Easy)
21aba845 »
2011-08-09 Updated README and Changelog with inf about Typhoeus support
26 * Typhoeus (currently only Typhoeus::Hydra)
95e82980 »
2009-11-24 Revert "WebMock module has to be included manually and is not include…
27
34c758e3 »
2011-08-09 Listed supported Ruby interpreters in README
28 Supported Ruby Interpreters
29 ---------------------------
30
31 * MRI 1.8.6
32 * MRI 1.8.7
33 * MRI 1.9.1
34 * MRI 1.9.2
972cab1b »
2011-08-09 Listed 1.9.3-preview1 in README and Travis
35 * MRI 1.9.3-preview1
34c758e3 »
2011-08-09 Listed supported Ruby interpreters in README
36 * REE 1.8.7
37 * JRuby
163f678f »
2011-08-09 Fixed README formatting
38
f25a2139 »
2010-07-16 Improved README on how to include WebMock in Test::Unit, RSpec and Cu…
39 ##Installation
95e82980 »
2009-11-24 Revert "WebMock module has to be included manually and is not include…
40
221e8cd5 »
2011-05-18 Document setting up a WebMock development environment
41 gem install webmock --source http://gemcutter.org
7a8bab18 »
2010-11-03 Added info on how to install the latest development version of WebMoc…
42
43 ### or to install the latest development version from github master
44
221e8cd5 »
2011-05-18 Document setting up a WebMock development environment
45 git clone http://github.com/bblimke/webmock.git
46 cd webmock
47 rake install
95e82980 »
2009-11-24 Revert "WebMock module has to be included manually and is not include…
48
221e8cd5 »
2011-05-18 Document setting up a WebMock development environment
49 ### Test::Unit
95e82980 »
2009-11-24 Revert "WebMock module has to be included manually and is not include…
50
f25a2139 »
2010-07-16 Improved README on how to include WebMock in Test::Unit, RSpec and Cu…
51 Add the following code to `test/test_helper.rb`
52
221e8cd5 »
2011-05-18 Document setting up a WebMock development environment
53 require 'webmock/test_unit'
95e82980 »
2009-11-24 Revert "WebMock module has to be included manually and is not include…
54
f25a2139 »
2010-07-16 Improved README on how to include WebMock in Test::Unit, RSpec and Cu…
55 ### RSpec
221e8cd5 »
2011-05-18 Document setting up a WebMock development environment
56
f25a2139 »
2010-07-16 Improved README on how to include WebMock in Test::Unit, RSpec and Cu…
57 Add the following code to `spec/spec_helper`:
95e82980 »
2009-11-24 Revert "WebMock module has to be included manually and is not include…
58
221e8cd5 »
2011-05-18 Document setting up a WebMock development environment
59 require 'webmock/rspec'
95e82980 »
2009-11-24 Revert "WebMock module has to be included manually and is not include…
60
bd438974 »
2011-08-09 Added info about minitest support to README
61 ### MiniTest
62
63 Add the following code to `test/test_helper`:
64
65 require 'webmock/minitest'
66
f25a2139 »
2010-07-16 Improved README on how to include WebMock in Test::Unit, RSpec and Cu…
67 ### Cucumber
68
69 Add the following code to `features/support/env.rb`
3ca443e8 »
2009-11-24 WebMock module has to be included manually and is not included by RSp…
70
221e8cd5 »
2011-05-18 Document setting up a WebMock development environment
71 require 'webmock/cucumber'
f25a2139 »
2010-07-16 Improved README on how to include WebMock in Test::Unit, RSpec and Cu…
72
73 You can also use WebMock outside a test framework:
74
221e8cd5 »
2011-05-18 Document setting up a WebMock development environment
75 require 'webmock'
76 include WebMock::API
95e82980 »
2009-11-24 Revert "WebMock module has to be included manually and is not include…
77
78 ## Examples
79
80
81
82 ## Stubbing
83
84
85 ### Stubbed request based on uri only and with the default response
86
221e8cd5 »
2011-05-18 Document setting up a WebMock development environment
87 stub_request(:any, "www.example.com")
95e82980 »
2009-11-24 Revert "WebMock module has to be included manually and is not include…
88
221e8cd5 »
2011-05-18 Document setting up a WebMock development environment
89 Net::HTTP.get("www.example.com", "/") # ===> Success
95e82980 »
2009-11-24 Revert "WebMock module has to be included manually and is not include…
90
91 ### Stubbing requests based on method, uri, body and headers
92
221e8cd5 »
2011-05-18 Document setting up a WebMock development environment
93 stub_request(:post, "www.example.com").with(:body => "abc", :headers => { 'Content-Length' => 3 })
95e82980 »
2009-11-24 Revert "WebMock module has to be included manually and is not include…
94
221e8cd5 »
2011-05-18 Document setting up a WebMock development environment
95 uri = URI.parse("http://www.example.com/")
95e82980 »
2009-11-24 Revert "WebMock module has to be included manually and is not include…
96 req = Net::HTTP::Post.new(uri.path)
221e8cd5 »
2011-05-18 Document setting up a WebMock development environment
97 req['Content-Length'] = 3
95e82980 »
2009-11-24 Revert "WebMock module has to be included manually and is not include…
98 res = Net::HTTP.start(uri.host, uri.port) {|http|
99 http.request(req, "abc")
100 } # ===> Success
101
5313e02f »
2010-01-31 Added version 0.9.0 features to CHANGELOG and README
102 ### Matching request body and headers against regular expressions
103
221e8cd5 »
2011-05-18 Document setting up a WebMock development environment
104 stub_request(:post, "www.example.com").
105 with(:body => /^.*world$/, :headers => {"Content-Type" => /image\/.+/}).to_return(:body => "abc")
5313e02f »
2010-01-31 Added version 0.9.0 features to CHANGELOG and README
106
221e8cd5 »
2011-05-18 Document setting up a WebMock development environment
107 uri = URI.parse('http://www.example.com/')
5313e02f »
2010-01-31 Added version 0.9.0 features to CHANGELOG and README
108 req = Net::HTTP::Post.new(uri.path)
221e8cd5 »
2011-05-18 Document setting up a WebMock development environment
109 req['Content-Type'] = 'image/png'
5313e02f »
2010-01-31 Added version 0.9.0 features to CHANGELOG and README
110 res = Net::HTTP.start(uri.host, uri.port) {|http|
111 http.request(req, 'hello world')
112 } # ===> Success
221e8cd5 »
2011-05-18 Document setting up a WebMock development environment
113
354af66a »
2010-06-21 Updated CHANGELOG width version 1.3.0 changes
114 ### Matching request body against a hash. Body can be URL-Encoded, JSON or XML.
fb18a017 »
2010-06-19 Updated README with info about matching request body against hash
115
221e8cd5 »
2011-05-18 Document setting up a WebMock development environment
116 stub_http_request(:post, "www.example.com").
117 with(:body => {:data => {:a => '1', :b => 'five'}})
118
119 RestClient.post('www.example.com', "data[a]=1&data[b]=five",
120 :content_type => 'application/x-www-form-urlencoded') # ===> Success
fb18a017 »
2010-06-19 Updated README with info about matching request body against hash
121
221e8cd5 »
2011-05-18 Document setting up a WebMock development environment
122 RestClient.post('www.example.com', '{"data":{"a":"1","b":"five"}}',
123 :content_type => 'application/json') # ===> Success
124
125 RestClient.post('www.example.com', '<data a="1" b="five" />',
126 :content_type => 'application/xml' ) # ===> Success
5313e02f »
2010-01-31 Added version 0.9.0 features to CHANGELOG and README
127
95e82980 »
2009-11-24 Revert "WebMock module has to be included manually and is not include…
128 ### Matching custom request headers
129
0016b136 »
2010-01-31 Minor changes to README
130 stub_request(:any, "www.example.com").with(:headers=>{ 'Header-Name' => 'Header-Value' })
95e82980 »
2009-11-24 Revert "WebMock module has to be included manually and is not include…
131
221e8cd5 »
2011-05-18 Document setting up a WebMock development environment
132 uri = URI.parse('http://www.example.com/')
95e82980 »
2009-11-24 Revert "WebMock module has to be included manually and is not include…
133 req = Net::HTTP::Post.new(uri.path)
221e8cd5 »
2011-05-18 Document setting up a WebMock development environment
134 req['Header-Name'] = 'Header-Value'
95e82980 »
2009-11-24 Revert "WebMock module has to be included manually and is not include…
135 res = Net::HTTP.start(uri.host, uri.port) {|http|
136 http.request(req, 'abc')
137 } # ===> Success
138
176482c1 »
2010-04-28 Updated README with 1.1.0 changes
139 ### Matching multiple headers with the same name
140
221e8cd5 »
2011-05-18 Document setting up a WebMock development environment
141 stub_http_request(:get, 'www.example.com').with(:headers => {'Accept' => ['image/jpeg', 'image/png'] })
142
143 req = Net::HTTP::Get.new("/")
144 req['Accept'] = ['image/png']
145 req.add_field('Accept', 'image/jpeg')
146 Net::HTTP.start("www.example.com") {|http| http.request(req) } # ===> Success
176482c1 »
2010-04-28 Updated README with 1.1.0 changes
147
5313e02f »
2010-01-31 Added version 0.9.0 features to CHANGELOG and README
148 ### Matching requests against provided block
149
221e8cd5 »
2011-05-18 Document setting up a WebMock development environment
150 stub_request(:post, "www.example.com").with { |request| request.body == "abc" }
151 RestClient.post('www.example.com', 'abc') # ===> Success
152
5313e02f »
2010-01-31 Added version 0.9.0 features to CHANGELOG and README
153 ### Request with basic authentication
154
155 stub_request(:get, "user:pass@www.example.com")
156
157 Net::HTTP.start('www.example.com') {|http|
158 req = Net::HTTP::Get.new('/')
159 req.basic_auth 'user', 'pass'
160 http.request(req)
161 } # ===> Success
162
163 ### Matching uris using regular expressions
164
221e8cd5 »
2011-05-18 Document setting up a WebMock development environment
165 stub_request(:any, /.*example.*/)
5313e02f »
2010-01-31 Added version 0.9.0 features to CHANGELOG and README
166
221e8cd5 »
2011-05-18 Document setting up a WebMock development environment
167 Net::HTTP.get('www.example.com', '/') # ===> Success
168
169 ### Matching query params using hash
170
171 stub_http_request(:get, "www.example.com").with(:query => {"a" => ["b", "c"]})
172
173 RestClient.get("http://www.example.com/?a[]=b&a[]=c") # ===> Success
5313e02f »
2010-01-31 Added version 0.9.0 features to CHANGELOG and README
174
95e82980 »
2009-11-24 Revert "WebMock module has to be included manually and is not include…
175 ### Stubbing with custom response
176
221e8cd5 »
2011-05-18 Document setting up a WebMock development environment
177 stub_request(:any, "www.example.com").to_return(:body => "abc", :status => 200, :headers => { 'Content-Length' => 3 } )
178
179 Net::HTTP.get("www.example.com", '/') # ===> "abc"
95e82980 »
2009-11-24 Revert "WebMock module has to be included manually and is not include…
180
0f3bb219 »
2010-03-13 Updated README regarding dynamic responses
181 ### Response with body specified as IO object
95e82980 »
2009-11-24 Revert "WebMock module has to be included manually and is not include…
182
221e8cd5 »
2011-05-18 Document setting up a WebMock development environment
183 File.open('/tmp/response_body.txt', 'w') { |f| f.puts 'abc' }
184
185 stub_request(:any, "www.example.com").to_return(:body => File.new('/tmp/response_body.txt'), :status => 200)
95e82980 »
2009-11-24 Revert "WebMock module has to be included manually and is not include…
186
221e8cd5 »
2011-05-18 Document setting up a WebMock development environment
187 Net::HTTP.get('www.example.com', '/') # ===> "abc\n"
95e82980 »
2009-11-24 Revert "WebMock module has to be included manually and is not include…
188
3314aad0 »
2010-03-14 Added support for status messages
189 ### Response with custom status message
190
221e8cd5 »
2011-05-18 Document setting up a WebMock development environment
191 stub_request(:any, "www.example.com").to_return(:status => [500, "Internal Server Error"])
192
193 req = Net::HTTP::Get.new("/")
194 Net::HTTP.start("www.example.com") { |http| http.request(req) }.message # ===> "Internal Server Error"
3314aad0 »
2010-03-14 Added support for status messages
195
5313e02f »
2010-01-31 Added version 0.9.0 features to CHANGELOG and README
196 ### Replaying raw responses recorded with `curl -is`
197
221e8cd5 »
2011-05-18 Document setting up a WebMock development environment
198 `curl -is www.example.com > /tmp/example_curl_-is_output.txt`
199 raw_response_file = File.new("/tmp/example_curl_-is_output.txt")
5313e02f »
2010-01-31 Added version 0.9.0 features to CHANGELOG and README
200
201 from file
202
221e8cd5 »
2011-05-18 Document setting up a WebMock development environment
203 stub_request(:get, "www.example.com").to_return(raw_response_file)
5313e02f »
2010-01-31 Added version 0.9.0 features to CHANGELOG and README
204
205 or string
206
221e8cd5 »
2011-05-18 Document setting up a WebMock development environment
207 stub_request(:get, "www.example.com").to_return(raw_response_file.read)
95e82980 »
2009-11-24 Revert "WebMock module has to be included manually and is not include…
208
0f3bb219 »
2010-03-13 Updated README regarding dynamic responses
209 ### Responses dynamically evaluated from block
210
4e0568aa »
2009-12-10 Added information about dynamic responses to README
211 stub_request(:any, 'www.example.net').
0f3bb219 »
2010-03-13 Updated README regarding dynamic responses
212 to_return { |request| {:body => request.body} }
4e0568aa »
2009-12-10 Added information about dynamic responses to README
213
0f3bb219 »
2010-03-13 Updated README regarding dynamic responses
214 RestClient.post('www.example.net', 'abc') # ===> "abc\n"
f0408f85 »
2010-10-25 Added support for dynamically evaluated raw responses recorded with `…
215
0f3bb219 »
2010-03-13 Updated README regarding dynamic responses
216 ### Responses dynamically evaluated from lambda
f0408f85 »
2010-10-25 Added support for dynamically evaluated raw responses recorded with `…
217
5b1c97b0 »
2010-02-26 Added complete dynamic request, where headers, body and status can al…
218 stub_request(:any, 'www.example.net').
219 to_return(lambda { |request| {:body => request.body} })
220
221e8cd5 »
2011-05-18 Document setting up a WebMock development environment
221 RestClient.post('www.example.net', 'abc') # ===> "abc\n"
4e0568aa »
2009-12-10 Added information about dynamic responses to README
222
f0408f85 »
2010-10-25 Added support for dynamically evaluated raw responses recorded with `…
223 ### Dynamically evaluated raw responses recorded with `curl -is`
224
225 `curl -is www.example.com > /tmp/www.example.com.txt`
226 stub_request(:get, "www.example.com").to_return(lambda { |request| File.new("/tmp/#{request.uri.host.to_s}.txt" }))
227
0f3bb219 »
2010-03-13 Updated README regarding dynamic responses
228 ### Responses with dynamically evaluated parts
229
230 stub_request(:any, 'www.example.net').
231 to_return(:body => lambda { |request| request.body })
232
221e8cd5 »
2011-05-18 Document setting up a WebMock development environment
233 RestClient.post('www.example.net', 'abc') # ===> "abc\n"
0f3bb219 »
2010-03-13 Updated README regarding dynamic responses
234
c6974dba »
2011-07-31 Added info about how to stub requests to respond with Rack app response.
235 ### Rack responses
236
237 class MyRackApp
238 def self.call(env)
239 [200, {}, ["Hello"]]
240 end
241 end
242
243 stub_request(:get, "www.example.com").to_rack(MyRackApp)
244
245 RestClient.post('www.example.com') # ===> "Hello"
246
af52fe70 »
2010-03-14 Added support for declaring timeout errors using `to_timeout`
247 ### Raising errors
248
207443ef »
2010-06-19 `to_raise()` now accepts exception instance or a string as argument i…
249 #### Exception declared by class
250
221e8cd5 »
2011-05-18 Document setting up a WebMock development environment
251 stub_request(:any, 'www.example.net').to_raise(StandardError)
af52fe70 »
2010-03-14 Added support for declaring timeout errors using `to_timeout`
252
253 RestClient.post('www.example.net', 'abc') # ===> StandardError
221e8cd5 »
2011-05-18 Document setting up a WebMock development environment
254
207443ef »
2010-06-19 `to_raise()` now accepts exception instance or a string as argument i…
255 #### or by exception instance
256
257 stub_request(:any, 'www.example.net').to_raise(StandardError.new("some error"))
258
259 #### or by string
221e8cd5 »
2011-05-18 Document setting up a WebMock development environment
260
207443ef »
2010-06-19 `to_raise()` now accepts exception instance or a string as argument i…
261 stub_request(:any, 'www.example.net').to_raise("some error")
af52fe70 »
2010-03-14 Added support for declaring timeout errors using `to_timeout`
262
263 ### Raising timeout errors
264
221e8cd5 »
2011-05-18 Document setting up a WebMock development environment
265 stub_request(:any, 'www.example.net').to_timeout
af52fe70 »
2010-03-14 Added support for declaring timeout errors using `to_timeout`
266
221e8cd5 »
2011-05-18 Document setting up a WebMock development environment
267 RestClient.post('www.example.net', 'abc') # ===> RestClient::RequestTimeout
af52fe70 »
2010-03-14 Added support for declaring timeout errors using `to_timeout`
268
5313e02f »
2010-01-31 Added version 0.9.0 features to CHANGELOG and README
269 ### Multiple responses for repeated requests
95e82980 »
2009-11-24 Revert "WebMock module has to be included manually and is not include…
270
221e8cd5 »
2011-05-18 Document setting up a WebMock development environment
271 stub_request(:get, "www.example.com").to_return({:body => "abc"}, {:body => "def"})
272 Net::HTTP.get('www.example.com', '/') # ===> "abc\n"
273 Net::HTTP.get('www.example.com', '/') # ===> "def\n"
274
275 #after all responses are used the last response will be returned infinitely
276
277 Net::HTTP.get('www.example.com', '/') # ===> "def\n"
bb412635 »
2009-12-10 Improved section about basic authentication support in readme.
278
af52fe70 »
2010-03-14 Added support for declaring timeout errors using `to_timeout`
279 ### Multiple responses using chained `to_return()`, `to_raise()` or `to_timeout` declarations
95e82980 »
2009-11-24 Revert "WebMock module has to be included manually and is not include…
280
221e8cd5 »
2011-05-18 Document setting up a WebMock development environment
281 stub_request(:get, "www.example.com").
282 to_return({:body => "abc"}).then. #then() is just a syntactic sugar
283 to_return({:body => "def"}).then.
284 to_raise(MyException)
285 Net::HTTP.get('www.example.com', '/') # ===> "abc\n"
286 Net::HTTP.get('www.example.com', '/') # ===> "def\n"
287 Net::HTTP.get('www.example.com', '/') # ===> MyException raised
95e82980 »
2009-11-24 Revert "WebMock module has to be included manually and is not include…
288
5313e02f »
2010-01-31 Added version 0.9.0 features to CHANGELOG and README
289 ### Specifying number of times given response should be returned
290
221e8cd5 »
2011-05-18 Document setting up a WebMock development environment
291 stub_request(:get, "www.example.com").
292 to_return({:body => "abc"}).times(2).then.
293 to_return({:body => "def"})
5313e02f »
2010-01-31 Added version 0.9.0 features to CHANGELOG and README
294
221e8cd5 »
2011-05-18 Document setting up a WebMock development environment
295 Net::HTTP.get('www.example.com', '/') # ===> "abc\n"
296 Net::HTTP.get('www.example.com', '/') # ===> "abc\n"
297 Net::HTTP.get('www.example.com', '/') # ===> "def\n"
95e82980 »
2009-11-24 Revert "WebMock module has to be included manually and is not include…
298
299
300 ### Real requests to network can be allowed or disabled
301
221e8cd5 »
2011-05-18 Document setting up a WebMock development environment
302 WebMock.allow_net_connect!
95e82980 »
2009-11-24 Revert "WebMock module has to be included manually and is not include…
303
221e8cd5 »
2011-05-18 Document setting up a WebMock development environment
304 stub_request(:any, "www.example.com").to_return(:body => "abc")
95e82980 »
2009-11-24 Revert "WebMock module has to be included manually and is not include…
305
221e8cd5 »
2011-05-18 Document setting up a WebMock development environment
306 Net::HTTP.get('www.example.com', '/') # ===> "abc"
307
308 Net::HTTP.get('www.something.com', '/') # ===> /.+Something.+/
309
310 WebMock.disable_net_connect!
311
312 Net::HTTP.get('www.something.com', '/') # ===> Failure
95e82980 »
2009-11-24 Revert "WebMock module has to be included manually and is not include…
313
8859deb0 »
2010-04-03 Change the locahost option syntax and update README
314 ### External requests can be disabled while allowing localhost
221e8cd5 »
2011-05-18 Document setting up a WebMock development environment
315
316 WebMock.disable_net_connect!(:allow_localhost => true)
317
318 Net::HTTP.get('www.something.com', '/') # ===> Failure
319
320 Net::HTTP.get('localhost:9887', '/') # ===> Allowed. Perhaps to Selenium?
95e82980 »
2009-11-24 Revert "WebMock module has to be included manually and is not include…
321
e1d431ea »
2011-08-09 Updated README with info about :allow with specified port number
322 ### External requests can be disabled while allowing any hostname or port
67a08191 »
2010-09-06 Version 1.3.5 changelog + updated docs
323
e1d431ea »
2011-08-09 Updated README with info about :allow with specified port number
324 WebMock.disable_net_connect!(:allow => "www.example.org:8080")
67a08191 »
2010-09-06 Version 1.3.5 changelog + updated docs
325
e1d431ea »
2011-08-09 Updated README with info about :allow with specified port number
326 RestClient.get('www.something.com', '/') # ===> Failure
327
328 RestClient.get('www.example.org', '/') # ===> Failure.
67a08191 »
2010-09-06 Version 1.3.5 changelog + updated docs
329
e1d431ea »
2011-08-09 Updated README with info about :allow with specified port number
330 RestClient.get('www.example.org:8080', '/') # ===> Allowed
67a08191 »
2010-09-06 Version 1.3.5 changelog + updated docs
331
4e313c29 »
2010-11-02 Updated CHANGELOG and README with information about net_http_connect_…
332 ## Connecting on Net::HTTP.start
333
c92144fb »
2010-11-02 Fixed text formatting in README
334 HTTP protocol has 3 steps: connect, request and response (or 4 with close). Most Ruby HTTP client libraries
335 treat connect as a part of request step, with the exception of `Net::HTTP` which
336 allows opening connection to the server separately to the request, by using `Net::HTTP.start`.
4e313c29 »
2010-11-02 Updated CHANGELOG and README with information about net_http_connect_…
337
c92144fb »
2010-11-02 Fixed text formatting in README
338 WebMock API was also designed with connect being part of request step, and it only allows stubbing
339 requests, not connections. When `Net::HTTP.start` is called, WebMock doesn't know yet whether
340 a request is stubbed or not. WebMock by default delays a connection until the request is invoked,
341 so when there is no request, `Net::HTTP.start` doesn't do anything.
342 **This means that WebMock breaks the Net::HTTP behaviour by default!**
4e313c29 »
2010-11-02 Updated CHANGELOG and README with information about net_http_connect_…
343
079a719e »
2010-11-02 Updated changelog with version 1.5.0 changes
344 To workaround this issue, WebMock offers `:net_http_connect_on_start` option,
345 which can be passed to `WebMock.allow_net_connect!` and `WebMock#disable_net_connect!` methods, i.e.
4e313c29 »
2010-11-02 Updated CHANGELOG and README with information about net_http_connect_…
346
221e8cd5 »
2011-05-18 Document setting up a WebMock development environment
347 WebMock.allow_net_connect!(:net_http_connect_on_start => true)
4e313c29 »
2010-11-02 Updated CHANGELOG and README with information about net_http_connect_…
348
c92144fb »
2010-11-02 Fixed text formatting in README
349 This forces WebMock Net::HTTP adapter to always connect on `Net::HTTP.start`.
4e313c29 »
2010-11-02 Updated CHANGELOG and README with information about net_http_connect_…
350
95e82980 »
2009-11-24 Revert "WebMock module has to be included manually and is not include…
351 ## Setting Expectations
352
353 ### Setting expectations in Test::Unit
221e8cd5 »
2011-05-18 Document setting up a WebMock development environment
354 require 'webmock/test_unit'
95e82980 »
2009-11-24 Revert "WebMock module has to be included manually and is not include…
355
32c653c9 »
2009-12-11 Replaced google with example in README
356 stub_request(:any, "www.example.com")
95e82980 »
2009-11-24 Revert "WebMock module has to be included manually and is not include…
357
221e8cd5 »
2011-05-18 Document setting up a WebMock development environment
358 uri = URI.parse('http://www.example.com/')
95e82980 »
2009-11-24 Revert "WebMock module has to be included manually and is not include…
359 req = Net::HTTP::Post.new(uri.path)
221e8cd5 »
2011-05-18 Document setting up a WebMock development environment
360 req['Content-Length'] = 3
95e82980 »
2009-11-24 Revert "WebMock module has to be included manually and is not include…
361 res = Net::HTTP.start(uri.host, uri.port) {|http|
362 http.request(req, 'abc')
363 }
364
221e8cd5 »
2011-05-18 Document setting up a WebMock development environment
365 assert_requested :post, "http://www.example.com",
366 :headers => {'Content-Length' => 3}, :body => "abc", :times => 1 # ===> Success
367
368 assert_not_requested :get, "http://www.something.com" # ===> Success
95e82980 »
2009-11-24 Revert "WebMock module has to be included manually and is not include…
369
221e8cd5 »
2011-05-18 Document setting up a WebMock development environment
370 assert_requested(:post, "http://www.example.com", :times => 1) { |req| req.body == "abc" }
5313e02f »
2010-01-31 Added version 0.9.0 features to CHANGELOG and README
371
95e82980 »
2009-11-24 Revert "WebMock module has to be included manually and is not include…
372 ### Expecting real (not stubbed) requests
373
221e8cd5 »
2011-05-18 Document setting up a WebMock development environment
374 WebMock.allow_net_connect!
375
376 Net::HTTP.get('www.example.com', '/') # ===> Success
95e82980 »
2009-11-24 Revert "WebMock module has to be included manually and is not include…
377
221e8cd5 »
2011-05-18 Document setting up a WebMock development environment
378 assert_requested :get, "http://www.example.com" # ===> Success
95e82980 »
2009-11-24 Revert "WebMock module has to be included manually and is not include…
379
380
a6e08005 »
2011-06-06 allow expectations to be set on the stub itself
381 ### Setting expectations in RSpec on `WebMock` module
95e82980 »
2009-11-24 Revert "WebMock module has to be included manually and is not include…
382 This style is borrowed from [fakeweb-matcher](http://github.com/freelancing-god/fakeweb-matcher)
383
221e8cd5 »
2011-05-18 Document setting up a WebMock development environment
384 require 'webmock/rspec'
385
386 WebMock.should have_requested(:get, "www.example.com").with(:body => "abc", :headers => {'Content-Length' => 3}).twice
387
388 WebMock.should_not have_requested(:get, "www.something.com")
389
390 WebMock.should have_requested(:post, "www.example.com").with { |req| req.body == "abc" }
95e82980 »
2009-11-24 Revert "WebMock module has to be included manually and is not include…
391
221e8cd5 »
2011-05-18 Document setting up a WebMock development environment
392 WebMock.should have_requested(:get, "www.example.com").with(:query => {"a" => ["b", "c"]})
fa4ab291 »
2010-06-19 Updated README with info about setting expectations on query params o…
393
221e8cd5 »
2011-05-18 Document setting up a WebMock development environment
394 WebMock.should have_requested(:get, "www.example.com").
c109d957 »
2011-05-25 Fixed syntax error in README examples
395 with(:body => {"a" => ["b", "c"]}, :headers => {'Content-Type' => 'application/json'})
95e82980 »
2009-11-24 Revert "WebMock module has to be included manually and is not include…
396
a6e08005 »
2011-06-06 allow expectations to be set on the stub itself
397 ### Setting expectations in RSpec with `a_request`
95e82980 »
2009-11-24 Revert "WebMock module has to be included manually and is not include…
398
221e8cd5 »
2011-05-18 Document setting up a WebMock development environment
399 a_request(:post, "www.example.com").with(:body => "abc", :headers => {'Content-Length' => 3}).should have_been_made.once
95e82980 »
2009-11-24 Revert "WebMock module has to be included manually and is not include…
400
221e8cd5 »
2011-05-18 Document setting up a WebMock development environment
401 a_request(:post, "www.something.com").should have_been_made.times(3)
95e82980 »
2009-11-24 Revert "WebMock module has to be included manually and is not include…
402
221e8cd5 »
2011-05-18 Document setting up a WebMock development environment
403 a_request(:any, "www.example.com").should_not have_been_made
95e82980 »
2009-11-24 Revert "WebMock module has to be included manually and is not include…
404
221e8cd5 »
2011-05-18 Document setting up a WebMock development environment
405 a_request(:post, "www.example.com").with { |req| req.body == "abc" }.should have_been_made
6cb45ed1 »
2010-10-06 Renames WebMock#request to WebMock#a_request to avoid conflicts with …
406
221e8cd5 »
2011-05-18 Document setting up a WebMock development environment
407 a_request(:get, "www.example.com").with(:query => {"a" => ["b", "c"]}).should have_been_made
6cb45ed1 »
2010-10-06 Renames WebMock#request to WebMock#a_request to avoid conflicts with …
408
221e8cd5 »
2011-05-18 Document setting up a WebMock development environment
409 a_request(:post, "www.example.com").
c109d957 »
2011-05-25 Fixed syntax error in README examples
410 with(:body => {"a" => ["b", "c"]}, :headers => {'Content-Type' => 'application/json'}).should have_been_made
6cb45ed1 »
2010-10-06 Renames WebMock#request to WebMock#a_request to avoid conflicts with …
411
a6e08005 »
2011-06-06 allow expectations to be set on the stub itself
412 ### Setting expectations in RSpec on the stub
413
414 stub = stub_request(:get, "www.example.com")
415 # ... make requests ...
416 stub.should have_been_requested
417
95e82980 »
2009-11-24 Revert "WebMock module has to be included manually and is not include…
418 ## Clearing stubs and request history
419
473b7096 »
2010-11-03 Added WebMock.reset! method. WebMock.reset_webmock is now deprecated
420 If you want to reset all current stubs and history of requests use `WebMock.reset!`
95e82980 »
2009-11-24 Revert "WebMock module has to be included manually and is not include…
421
221e8cd5 »
2011-05-18 Document setting up a WebMock development environment
422 stub_request(:any, "www.example.com")
95e82980 »
2009-11-24 Revert "WebMock module has to be included manually and is not include…
423
221e8cd5 »
2011-05-18 Document setting up a WebMock development environment
424 Net::HTTP.get('www.example.com', '/') # ===> Success
95e82980 »
2009-11-24 Revert "WebMock module has to be included manually and is not include…
425
221e8cd5 »
2011-05-18 Document setting up a WebMock development environment
426 WebMock.reset!
95e82980 »
2009-11-24 Revert "WebMock module has to be included manually and is not include…
427
221e8cd5 »
2011-05-18 Document setting up a WebMock development environment
428 Net::HTTP.get('www.example.com', '/') # ===> Failure
95e82980 »
2009-11-24 Revert "WebMock module has to be included manually and is not include…
429
221e8cd5 »
2011-05-18 Document setting up a WebMock development environment
430 assert_not_requested :get, "www.example.com" # ===> Success
95e82980 »
2009-11-24 Revert "WebMock module has to be included manually and is not include…
431
17547a56 »
2011-08-14 Updated Changelog and README with info about enabling and disabling W…
432 ## Disabling and enabling WebMock or only some http client adapters
433
434 WebMock.disable! #disable WebMock (all adapters)
435 WebMock.disable!(:except => [:net_http]) #disable all adapters except Net::HTTP
436 WebMock.enable! #enable WebMock (all adapters)
437 WebMock.enable!(:except => [:patron]) #enable all adapters except Patron
438
95e82980 »
2009-11-24 Revert "WebMock module has to be included manually and is not include…
439
440 ## Matching requests
441
442 An executed request matches stubbed request if it passes following criteria:
443
4306efac »
2009-11-30 Ability to register and expect requests with empty body.
444 When request URI matches stubbed request URI string or Regexp pattern<br/>
95e82980 »
2009-11-24 Revert "WebMock module has to be included manually and is not include…
445 And request method is the same as stubbed request method or stubbed request method is :any<br/>
4306efac »
2009-11-30 Ability to register and expect requests with empty body.
446 And request body is the same as stubbed request body or stubbed request body is not specified<br/>
0016b136 »
2010-01-31 Minor changes to README
447 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/>
448 And request matches provided block or block is not provided
95e82980 »
2009-11-24 Revert "WebMock module has to be included manually and is not include…
449
450 ## Precedence of stubs
451
452 Always the last declared stub matching the request will be applied i.e:
453
221e8cd5 »
2011-05-18 Document setting up a WebMock development environment
454 stub_request(:get, "www.example.com").to_return(:body => "abc")
455 stub_request(:get, "www.example.com").to_return(:body => "def")
95e82980 »
2009-11-24 Revert "WebMock module has to be included manually and is not include…
456
221e8cd5 »
2011-05-18 Document setting up a WebMock development environment
457 Net::HTTP.get('www.example.com', '/') # ====> "def"
95e82980 »
2009-11-24 Revert "WebMock module has to be included manually and is not include…
458
459 ## Matching URIs
460
221e8cd5 »
2011-05-18 Document setting up a WebMock development environment
461 WebMock will match all different representations of the same URI.
95e82980 »
2009-11-24 Revert "WebMock module has to be included manually and is not include…
462
463 I.e all the following representations of the URI are equal:
464
32c653c9 »
2009-12-11 Replaced google with example in README
465 "www.example.com"
466 "www.example.com/"
467 "www.example.com:80"
468 "www.example.com:80/"
469 "http://www.example.com"
470 "http://www.example.com/"
471 "http://www.example.com:80"
472 "http://www.example.com:80/"
221e8cd5 »
2011-05-18 Document setting up a WebMock development environment
473
95e82980 »
2009-11-24 Revert "WebMock module has to be included manually and is not include…
474 The following URIs with basic authentication are also equal for WebMock
475
221e8cd5 »
2011-05-18 Document setting up a WebMock development environment
476 "a b:pass@www.example.com"
477 "a b:pass@www.example.com/"
478 "a b:pass@www.example.com:80"
479 "a b:pass@www.example.com:80/"
480 "http://a b:pass@www.example.com"
481 "http://a b:pass@www.example.com/"
482 "http://a b:pass@www.example.com:80"
483 "http://a b:pass@www.example.com:80/"
484 "a%20b:pass@www.example.com"
485 "a%20b:pass@www.example.com/"
486 "a%20b:pass@www.example.com:80"
487 "a%20b:pass@www.example.com:80/"
488 "http://a%20b:pass@www.example.com"
489 "http://a%20b:pass@www.example.com/"
490 "http://a%20b:pass@www.example.com:80"
491 "http://a%20b:pass@www.example.com:80/"
95e82980 »
2009-11-24 Revert "WebMock module has to be included manually and is not include…
492
493 or these
494
221e8cd5 »
2011-05-18 Document setting up a WebMock development environment
495 "www.example.com/my path/?a=my param&b=c"
496 "www.example.com/my%20path/?a=my%20param&b=c"
497 "www.example.com:80/my path/?a=my param&b=c"
498 "www.example.com:80/my%20path/?a=my%20param&b=c"
499 "http://www.example.com/my path/?a=my param&b=c"
500 "http://www.example.com/my%20path/?a=my%20param&b=c"
501 "http://www.example.com:80/my path/?a=my param&b=c"
502 "http://www.example.com:80/my%20path/?a=my%20param&b=c"
95e82980 »
2009-11-24 Revert "WebMock module has to be included manually and is not include…
503
504
505 If you provide Regexp to match URI, WebMock will try to match it against every valid form of the same url.
506
78414b7e »
2009-12-11 Improved examples with equality of URIs with the same parameters
507 I.e `/.*my param.*/` will match `www.example.com/my%20path` because it is equivalent of `www.example.com/my path`
95e82980 »
2009-11-24 Revert "WebMock module has to be included manually and is not include…
508
509
510 ## Matching headers
511
512 WebMock will match request headers against stubbed request headers in the following situations:
513
514 1. Stubbed request has headers specified and request headers are the same as stubbed headers <br/>
515 i.e stubbed headers: `{ 'Header1' => 'Value1', 'Header1' => 'Value1' }`, requested: `{ 'Header1' => 'Value1', 'Header1' => 'Value1' }`
516
517 2. Stubbed request has headers specified and stubbed request headers are a subset of request headers <br/>
518 i.e stubbed headers: `{ 'Header1' => 'Value1' }`, requested: `{ 'Header1' => 'Value1', 'Header1' => 'Value1' }`
519
520 3. Stubbed request has no headers <br/>
521 i.e stubbed headers: `nil`, requested: `{ 'Header1' => 'Value1', 'Header1' => 'Value1' }`
522
523 WebMock normalises headers and treats all forms of same headers as equal:
524 i.e the following two sets of headers are equal:
525
526 `{ "Header1" => "value1", :content_length => 123, :X_CuStOm_hEAder => :value }`
527
528 `{ :header1 => "value1", "Content-Length" => 123, "x-cuSTOM-HeAder" => "value" }`
529
176482c1 »
2010-04-28 Updated README with 1.1.0 changes
530 ## Recording real requests and responses and replaying them later
531
532 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.
95e82980 »
2009-11-24 Revert "WebMock module has to be included manually and is not include…
533
53311327 »
2010-06-19 Updated README with information about request callbacks
534 ## Request callbacks
535
354af66a »
2010-06-21 Updated CHANGELOG width version 1.3.0 changes
536 ####WebMock can invoke callbacks stubbed or real requests:
53311327 »
2010-06-19 Updated README with information about request callbacks
537
538 WebMock.after_request do |request_signature, response|
354af66a »
2010-06-21 Updated CHANGELOG width version 1.3.0 changes
539 puts "Request #{request_signature} was made and #{response} was returned"
53311327 »
2010-06-19 Updated README with information about request callbacks
540 end
541
542 #### invoke callbacks for real requests only and except requests made with Patron
543
544 WebMock.after_request(:except => [:patron], :real_requests_only => true) do |request_signature, response|
354af66a »
2010-06-21 Updated CHANGELOG width version 1.3.0 changes
545 puts "Request #{request_signature} was made and #{response} was returned"
53311327 »
2010-06-19 Updated README with information about request callbacks
546 end
547
95e82980 »
2009-11-24 Revert "WebMock module has to be included manually and is not include…
548 ## Bugs and Issues
549
550 Please submit them here [http://github.com/bblimke/webmock/issues](http://github.com/bblimke/webmock/issues)
551
552 ## Suggestions
553
554 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)
555
556 I'm particularly interested in how the DSL could be improved.
557
221e8cd5 »
2011-05-18 Document setting up a WebMock development environment
558 ## Development
559
560 In order to work on Webmock you first need to fork and clone the repo.
561 Please do any work on a dedicated branch and rebase against master
562 before sending a pull request.
563
564 #### Running Tests
565
566 We use RVM in order to test WebMock against 1.8.6, REE, 1.8.7, 1.9.2 and
567 jRuby. You can get RVM setup for WebMock development using the
568 following commands (if you don't have these version of Ruby installed
569 use `rvm install` to install each of them).
570
571 rvm use --create 1.8.6@webmock
572 gem install jeweler bundler
573 bundle install
574
575 rvm use --create ree@webmock
576 gem install jeweler bundler
577 bundle install
578
579 rvm use --create 1.8.7@webmock
580 gem install jeweler bundler
581 bundle install
582
583 rvm use --create 1.9.2@webmock
584 gem install jeweler bundler
585 bundle install
586
587 rvm use --create jruby@webmock
588 gem install jeweler bundler
589 bundle install
590
591 These commands will create a gemset named WebMock for each of the
592 supported versions of Ruby and `bundle install` all dependencies.
593
594 With the supported versions of Ruby installed RVM will run specs across
595 all version with just one command.
596
597 bundle exec rvm 1.8.6@webmock,ree@webmock,1.8.7@webmock,1.9.2@webmock,jruby@webmock rspec spec/**/*_spec.rb
598
599 This command is wrapped up in to a rake task and can be invoked like so:
600
601 rake spec:rubies
602
95e82980 »
2009-11-24 Revert "WebMock module has to be included manually and is not include…
603 ## Credits
604
00424d99 »
2010-03-26 Updated credits in README
605 The initial lines of this project were written during New Bamboo [Hack Day](http://blog.new-bamboo.co.uk/2009/11/13/hackday-results)
95e82980 »
2009-11-24 Revert "WebMock module has to be included manually and is not include…
606 Thanks to my fellow [Bambinos](http://new-bamboo.co.uk/) for all the great suggestions!
607
00424d99 »
2010-03-26 Updated credits in README
608 People who submitted patches and new features or suggested improvements. Many thanks to these people:
609
610 * Ben Pickles
611 * Mark Evans
612 * Ivan Vega
613 * Piotr Usewicz
614 * Nick Plante
615 * Nick Quaranto
616 * Diego E. "Flameeyes" Pettenò
617 * Niels Meersschaert
618 * Mack Earnhardt
619 * Arvicco
620 * Sergio Gil
621 * Jeffrey Jones
622 * Tekin Suleyman
623 * Tom Ward
624 * Nadim Bitar
ee7b3b4b »
2010-04-29 Fixed spelling
625 * Myron Marston
9c8c4635 »
2010-05-14 Updated README with information about RSpec 2 support.
626 * Sam Phillips
2e88f99b »
2010-05-21 Updated info on how to include WebMock in Test::Unit and RSpec withou…
627 * Jose Angel Cortinas
52698d89 »
2010-05-24 Updated credits
628 * Razic
fd2472a8 »
2010-05-26 Version 1.2.2 changelog
629 * Steve Tooke
fb18a017 »
2010-06-19 Updated README with info about matching request body against hash
630 * Nathaniel Bibler
15f888ee »
2010-07-09 The less hacky way to get the stream behaviour working for em-http-re…
631 * Martyn Loughran
60fa2445 »
2010-07-09 Updated changelog
632 * Muness Alrubaie
67a08191 »
2010-09-06 Version 1.3.5 changelog + updated docs
633 * Charles Li
634 * Ryan Bigg
aaf774b6 »
2010-10-04 Added info to changelog and readme about Curb support, implemented by…
635 * Pete Higgins
ffbcdb3c »
2010-10-30 Updated changelog with the latest changes in master branch
636 * Hans de Graaff
4e313c29 »
2010-11-02 Updated CHANGELOG and README with information about net_http_connect_…
637 * Alastair Brunton
e6af079b »
2011-01-12 Updated 1.6.2 changelog and credits
638 * Sam Stokes
639 * Eugene Bolshakov
7fd377b4 »
2011-08-09 Updated list of contributors in README
640 * James Conroy-Finn
641 * Salvador Fuentes Jr
642 * Alex Rothenberg
643 * Aidan Feldman
644 * Steve Hull
645 * Jay Adkisson
646 * Zach Dennis
647 * Nikita Fedyashev
648 * Lin Jen-Shin
00424d99 »
2010-03-26 Updated credits in README
649
221e8cd5 »
2011-05-18 Document setting up a WebMock development environment
650 For a full list of contributors you can visit the
651 [contributors](https://github.com/bblimke/webmock/contributors) page.
652
00424d99 »
2010-03-26 Updated credits in README
653 ## Background
654
95e82980 »
2009-11-24 Revert "WebMock module has to be included manually and is not include…
655 Thank you Fakeweb! This library was inspired by [FakeWeb](fakeweb.rubyforge.org).
221e8cd5 »
2011-05-18 Document setting up a WebMock development environment
656 I imported some solutions from that project to WebMock. I also copied some code i.e Net:HTTP adapter.
95e82980 »
2009-11-24 Revert "WebMock module has to be included manually and is not include…
657 Fakeweb architecture unfortunately didn't allow me to extend it easily with the features I needed.
658 I also preferred some things to work differently i.e request stub precedence.
659
660 ## Copyright
661
bf18e896 »
2010-05-24 Changed license from GPL to MIT
662 Copyright (c) 2009-2010 Bartosz Blimke. See LICENSE for details.
Something went wrong with that request. Please try again.