Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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