Skip to content

HTTPS clone URL

Subversion checkout URL

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