Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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