Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 544 lines (341 sloc) 17.387 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
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
fb18a01 Updated README with info about matching request body against hash
Bartosz Blimke authored
16
17 Supported HTTP libraries
18 ------------------------
19
3d84ef6 Added EM-HTTP-Request to the list of supported http clients
Bartosz Blimke authored
20 * 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
21 * HTTPClient
22 * Patron
3d84ef6 Added EM-HTTP-Request to the list of supported http clients
Bartosz Blimke authored
23 * EM-HTTP-Request
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
24
f25a213 @bblimke Improved README on how to include WebMock in Test::Unit, RSpec and Cu…
authored
25 ##Installation
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
26
27 gem install webmock --source http://gemcutter.org
28
f25a213 @bblimke Improved README on how to include WebMock in Test::Unit, RSpec and Cu…
authored
29 ### Test::Unit
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
30
f25a213 @bblimke Improved README on how to include WebMock in Test::Unit, RSpec and Cu…
authored
31 Add the following code to `test/test_helper.rb`
32
33 require 'webmock/test_unit'
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
34
2e88f99 Updated info on how to include WebMock in Test::Unit and RSpec withou…
Bartosz Blimke authored
35 class Test::Unit::TestCase
f25a213 @bblimke Improved README on how to include WebMock in Test::Unit, RSpec and Cu…
authored
36 config.include WebMock
2e88f99 Updated info on how to include WebMock in Test::Unit and RSpec withou…
Bartosz Blimke authored
37 end
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
38
f25a213 @bblimke Improved README on how to include WebMock in Test::Unit, RSpec and Cu…
authored
39 ### RSpec
40
41 Add the following code to `spec/spec_helper`:
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
42
43 require 'webmock/rspec'
44
2e88f99 Updated info on how to include WebMock in Test::Unit and RSpec withou…
Bartosz Blimke authored
45 Spec::Runner.configure do |config|
46 config.include WebMock
47 end
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
48
f25a213 @bblimke Improved README on how to include WebMock in Test::Unit, RSpec and Cu…
authored
49 ### RSpec 2
3ca443e WebMock module has to be included manually and is not included by RSp…
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 `spec/spec_helper`:
52
53 require 'webmock/rspec'
54
55 RSpec.configure do |config|
56 config.include WebMock
57 end
58
59 ### Cucumber
60
61 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
62
780beb7 @bblimke Documented simpler way to include WebMock in Cucumber
authored
63 require 'webmock/rspec'
64 World(WebMock, WebMock::Matchers)
f25a213 @bblimke Improved README on how to include WebMock in Test::Unit, RSpec and Cu…
authored
65
66 You can also use WebMock outside a test framework:
67
68 require 'webmock'
3ca443e WebMock module has to be included manually and is not included by RSp…
Bartosz Blimke authored
69 include WebMock
f25a213 @bblimke Improved README on how to include WebMock in Test::Unit, RSpec and Cu…
authored
70 include WebMock::Matchers
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
71
72 ## Examples
73
74
75
76 ## Stubbing
77
78
79 ### Stubbed request based on uri only and with the default response
80
32c653c Replaced google with example in README
Bartosz Blimke authored
81 stub_request(:any, "www.example.com")
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
82
32c653c Replaced google with example in README
Bartosz Blimke authored
83 Net::HTTP.get("www.example.com", "/") # ===> Success
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
84
85 ### Stubbing requests based on method, uri, body and headers
86
32c653c Replaced google with example in README
Bartosz Blimke authored
87 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
88
32c653c Replaced google with example in README
Bartosz Blimke authored
89 uri = URI.parse("http://www.example.com/")
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
90 req = Net::HTTP::Post.new(uri.path)
91 req['Content-Length'] = 3
92 res = Net::HTTP.start(uri.host, uri.port) {|http|
93 http.request(req, "abc")
94 } # ===> Success
95
5313e02 Added version 0.9.0 features to CHANGELOG and README
Bartosz Blimke authored
96 ### Matching request body and headers against regular expressions
97
98 stub_request(:post, "www.example.com").
99 with(:body => /^.*world$/, :headers => {"Content-Type" => /image\/.+/}).to_return(:body => "abc")
100
101 uri = URI.parse('http://www.example.com/')
102 req = Net::HTTP::Post.new(uri.path)
103 req['Content-Type'] = 'image/png'
104 res = Net::HTTP.start(uri.host, uri.port) {|http|
105 http.request(req, 'hello world')
106 } # ===> Success
fb18a01 Updated README with info about matching request body against hash
Bartosz Blimke authored
107
354af66 Updated CHANGELOG width version 1.3.0 changes
Bartosz Blimke authored
108 ### 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
109
110 stub_http_request(:post, "www.example.com").
111 with(:body => {:data => {:a => '1', :b => 'five'}})
112
113 RestClient.post('www.example.com', "data[a]=1&data[b]=five",
114 :content_type => 'application/x-www-form-urlencoded') # ===> Success
115
116 RestClient.post('www.example.com', '{"data":{"a":"1","b":"five"}}',
117 :content_type => 'application/json') # ===> Success
118
119 RestClient.post('www.example.com', '<data a="1" b="five" />',
120 :content_type => 'application/xml' ) # ===> Success
5313e02 Added version 0.9.0 features to CHANGELOG and README
Bartosz Blimke authored
121
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
122 ### Matching custom request headers
123
0016b13 Minor changes to README
Bartosz Blimke authored
124 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
125
32c653c Replaced google with example in README
Bartosz Blimke authored
126 uri = URI.parse('http://www.example.com/')
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
127 req = Net::HTTP::Post.new(uri.path)
128 req['Header-Name'] = 'Header-Value'
129 res = Net::HTTP.start(uri.host, uri.port) {|http|
130 http.request(req, 'abc')
131 } # ===> Success
132
176482c Updated README with 1.1.0 changes
Bartosz Blimke authored
133 ### Matching multiple headers with the same name
134
135 stub_http_request(:get, 'www.example.com').with(:headers => {'Accept' => ['image/jpeg', 'image/png'] })
136
137 req = Net::HTTP::Get.new("/")
138 req['Accept'] = ['image/png']
139 req.add_field('Accept', 'image/jpeg')
140 Net::HTTP.start("www.example.com") {|http| http.request(req) } # ===> Success
141
5313e02 Added version 0.9.0 features to CHANGELOG and README
Bartosz Blimke authored
142 ### Matching requests against provided block
143
0016b13 Minor changes to README
Bartosz Blimke authored
144 stub_request(:post, "www.example.com").with { |request| request.body == "abc" }
145 RestClient.post('www.example.com', 'abc') # ===> Success
5313e02 Added version 0.9.0 features to CHANGELOG and README
Bartosz Blimke authored
146
147 ### Request with basic authentication
148
149 stub_request(:get, "user:pass@www.example.com")
150
151 Net::HTTP.start('www.example.com') {|http|
152 req = Net::HTTP::Get.new('/')
153 req.basic_auth 'user', 'pass'
154 http.request(req)
155 } # ===> Success
156
157 ### Matching uris using regular expressions
158
159 stub_request(:any, /.*example.*/)
160
161 Net::HTTP.get('www.example.com', '/') # ===> Success
316263c Added info about declaring query params inside stubs as a hash
Bartosz Blimke authored
162
163 ### Matching query params using hash
5313e02 Added version 0.9.0 features to CHANGELOG and README
Bartosz Blimke authored
164
316263c Added info about declaring query params inside stubs as a hash
Bartosz Blimke authored
165 stub_http_request(:get, "www.example.com").with(:query => {"a" => ["b", "c"]})
166
167 RestClient.get("http://www.example.com/?a[]=b&a[]=c") # ===> Success
168
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
169 ### Stubbing with custom response
170
32c653c Replaced google with example in README
Bartosz Blimke authored
171 stub_request(:any, "www.example.com").to_return(:body => "abc", :status => 200, :headers => { 'Content-Length' => 3 } )
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
172
32c653c Replaced google with example in README
Bartosz Blimke authored
173 Net::HTTP.get("www.example.com", '/') # ===> "abc"
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
174
0f3bb21 Updated README regarding dynamic responses
Bartosz Blimke authored
175 ### Response with body specified as IO object
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
176
177 File.open('/tmp/response_body.txt', 'w') { |f| f.puts 'abc' }
178
32c653c Replaced google with example in README
Bartosz Blimke authored
179 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
180
32c653c Replaced google with example in README
Bartosz Blimke authored
181 Net::HTTP.get('www.example.com', '/') # ===> "abc\n"
5313e02 Added version 0.9.0 features to CHANGELOG and README
Bartosz Blimke authored
182
3314aad Added support for status messages
Bartosz Blimke authored
183 ### Response with custom status message
184
185 stub_request(:any, "www.example.com").to_return(:status => [500, "Internal Server Error"])
186
187 req = Net::HTTP::Get.new("/")
188 Net::HTTP.start("www.example.com") { |http| http.request(req) }.message # ===> "Internal Server Error"
af52fe7 Added support for declaring timeout errors using `to_timeout`
Bartosz Blimke authored
189
5313e02 Added version 0.9.0 features to CHANGELOG and README
Bartosz Blimke authored
190 ### Replaying raw responses recorded with `curl -is`
191
192 `curl -is www.example.com > /tmp/example_curl_-is_output.txt`
193 raw_response_file = File.new("/tmp/example_curl_-is_output.txt")
194
195 from file
196
197 stub_request(:get, "www.example.com").to_return(raw_response_file)
198
199 or string
200
201 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
202
0f3bb21 Updated README regarding dynamic responses
Bartosz Blimke authored
203 ### Responses dynamically evaluated from block
204
4e0568a Added information about dynamic responses to README
Bartosz Blimke authored
205 stub_request(:any, 'www.example.net').
0f3bb21 Updated README regarding dynamic responses
Bartosz Blimke authored
206 to_return { |request| {:body => request.body} }
4e0568a Added information about dynamic responses to README
Bartosz Blimke authored
207
0f3bb21 Updated README regarding dynamic responses
Bartosz Blimke authored
208 RestClient.post('www.example.net', 'abc') # ===> "abc\n"
5b1c97b @tomafro Added complete dynamic request, where headers, body and status can al…
tomafro authored
209
0f3bb21 Updated README regarding dynamic responses
Bartosz Blimke authored
210 ### Responses dynamically evaluated from lambda
5b1c97b @tomafro Added complete dynamic request, where headers, body and status can al…
tomafro authored
211
212 stub_request(:any, 'www.example.net').
213 to_return(lambda { |request| {:body => request.body} })
214
215 RestClient.post('www.example.net', 'abc') # ===> "abc\n"
4e0568a Added information about dynamic responses to README
Bartosz Blimke authored
216
0f3bb21 Updated README regarding dynamic responses
Bartosz Blimke authored
217 ### Responses with dynamically evaluated parts
218
219 stub_request(:any, 'www.example.net').
220 to_return(:body => lambda { |request| request.body })
221
222 RestClient.post('www.example.net', 'abc') # ===> "abc\n"
223
af52fe7 Added support for declaring timeout errors using `to_timeout`
Bartosz Blimke authored
224 ### Raising errors
225
207443e `to_raise()` now accepts exception instance or a string as argument i…
Bartosz Blimke authored
226 #### Exception declared by class
227
af52fe7 Added support for declaring timeout errors using `to_timeout`
Bartosz Blimke authored
228 stub_request(:any, 'www.example.net').to_raise(StandardError)
229
230 RestClient.post('www.example.net', 'abc') # ===> StandardError
207443e `to_raise()` now accepts exception instance or a string as argument i…
Bartosz Blimke authored
231
232 #### or by exception instance
233
234 stub_request(:any, 'www.example.net').to_raise(StandardError.new("some error"))
235
236 #### or by string
237
238 stub_request(:any, 'www.example.net').to_raise("some error")
af52fe7 Added support for declaring timeout errors using `to_timeout`
Bartosz Blimke authored
239
240 ### Raising timeout errors
241
242 stub_request(:any, 'www.example.net').to_timeout
243
244 RestClient.post('www.example.net', 'abc') # ===> RestClient::RequestTimeout
245
5313e02 Added version 0.9.0 features to CHANGELOG and README
Bartosz Blimke authored
246 ### Multiple responses for repeated requests
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
247
5313e02 Added version 0.9.0 features to CHANGELOG and README
Bartosz Blimke authored
248 stub_request(:get, "www.example.com").to_return({:body => "abc"}, {:body => "def"})
249 Net::HTTP.get('www.example.com', '/') # ===> "abc\n"
250 Net::HTTP.get('www.example.com', '/') # ===> "def\n"
251
252 #after all responses are used the last response will be returned infinitely
253
254 Net::HTTP.get('www.example.com', '/') # ===> "def\n"
bb41263 Improved section about basic authentication support in readme.
Bartosz Blimke authored
255
af52fe7 Added support for declaring timeout errors using `to_timeout`
Bartosz Blimke authored
256 ### 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
257
5313e02 Added version 0.9.0 features to CHANGELOG and README
Bartosz Blimke authored
258 stub_request(:get, "www.example.com").
0f3bb21 Updated README regarding dynamic responses
Bartosz Blimke authored
259 to_return({:body => "abc"}).then. #then() is just a syntactic sugar
5313e02 Added version 0.9.0 features to CHANGELOG and README
Bartosz Blimke authored
260 to_return({:body => "def"}).then.
261 to_raise(MyException)
262 Net::HTTP.get('www.example.com', '/') # ===> "abc\n"
263 Net::HTTP.get('www.example.com', '/') # ===> "def\n"
264 Net::HTTP.get('www.example.com', '/') # ===> MyException raised
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
265
5313e02 Added version 0.9.0 features to CHANGELOG and README
Bartosz Blimke authored
266 ### Specifying number of times given response should be returned
267
268 stub_request(:get, "www.example.com").
269 to_return({:body => "abc"}).times(2).then.
270 to_return({:body => "def"})
271
272 Net::HTTP.get('www.example.com', '/') # ===> "abc\n"
273 Net::HTTP.get('www.example.com', '/') # ===> "abc\n"
274 Net::HTTP.get('www.example.com', '/') # ===> "def\n"
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
275
276
277 ### Real requests to network can be allowed or disabled
278
279 WebMock.allow_net_connect!
280
32c653c Replaced google with example in README
Bartosz Blimke authored
281 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
282
32c653c Replaced google with example in README
Bartosz Blimke authored
283 Net::HTTP.get('www.example.com', '/') # ===> "abc"
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
284
285 Net::HTTP.get('www.something.com', '/') # ===> /.+Something.+/
286
287 WebMock.disable_net_connect!
288
289 Net::HTTP.get('www.something.com', '/') # ===> Failure
290
8859deb @macksmind Change the locahost option syntax and update README
macksmind authored
291 ### External requests can be disabled while allowing localhost
292
4c6c6a9 Fixed code formatting in README
Bartosz Blimke authored
293 WebMock.disable_net_connect!(:allow_localhost => true)
8859deb @macksmind Change the locahost option syntax and update README
macksmind authored
294
4c6c6a9 Fixed code formatting in README
Bartosz Blimke authored
295 Net::HTTP.get('www.something.com', '/') # ===> Failure
8859deb @macksmind Change the locahost option syntax and update README
macksmind authored
296
4c6c6a9 Fixed code formatting in README
Bartosz Blimke authored
297 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
298
67a0819 @bblimke Version 1.3.5 changelog + updated docs
authored
299 ### External requests can be disabled while allowing any hostname
300
301 WebMock.disable_net_connect!(:allow => "www.example.org")
302
303 Net::HTTP.get('www.something.com', '/') # ===> Failure
304
305 Net::HTTP.get('www.example.org', '/') # ===> Allowed.
306
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
307 ## Setting Expectations
308
309 ### Setting expectations in Test::Unit
310 require 'webmock/test_unit'
311
32c653c Replaced google with example in README
Bartosz Blimke authored
312 stub_request(:any, "www.example.com")
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
313
32c653c Replaced google with example in README
Bartosz Blimke authored
314 uri = URI.parse('http://www.example.com/')
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
315 req = Net::HTTP::Post.new(uri.path)
316 req['Content-Length'] = 3
317 res = Net::HTTP.start(uri.host, uri.port) {|http|
318 http.request(req, 'abc')
319 }
320
32c653c Replaced google with example in README
Bartosz Blimke authored
321 assert_requested :post, "http://www.example.com",
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
322 :headers => {'Content-Length' => 3}, :body => "abc", :times => 1 # ===> Success
323
324 assert_not_requested :get, "http://www.something.com" # ===> Success
325
5313e02 Added version 0.9.0 features to CHANGELOG and README
Bartosz Blimke authored
326 assert_requested(:post, "http://www.example.com", :times => 1) { |req| req.body == "abc" }
327
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
328 ### Expecting real (not stubbed) requests
329
330 WebMock.allow_net_connect!
331
32c653c Replaced google with example in README
Bartosz Blimke authored
332 Net::HTTP.get('www.example.com', '/') # ===> Success
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
333
32c653c Replaced google with example in README
Bartosz Blimke authored
334 assert_requested :get, "http://www.example.com" # ===> Success
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
335
336
337 ### Setting expectations in RSpec
338 This style is borrowed from [fakeweb-matcher](http://github.com/freelancing-god/fakeweb-matcher)
339
340 require 'webmock/rspec'
341
32c653c Replaced google with example in README
Bartosz Blimke authored
342 WebMock.should have_requested(:get, "www.example.com").with(:body => "abc", :headers => {'Content-Length' => 3}).twice
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
343
344 WebMock.should_not have_requested(:get, "www.something.com")
5313e02 Added version 0.9.0 features to CHANGELOG and README
Bartosz Blimke authored
345
346 WebMock.should have_requested(:post, "www.example.com").with { |req| req.body == "abc" }
fa4ab29 Updated README with info about setting expectations on query params o…
Bartosz Blimke authored
347
348 WebMock.should have_requested(:get, "www.example.com").with(:query => {"a" => ["b", "c"]})
349
350 WebMock.should have_requested(:get, "www.example.com").
351 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
352
353 ### Different way of setting expectations in RSpec
354
32c653c Replaced google with example in README
Bartosz Blimke authored
355 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
356
357 request(:post, "www.something.com").should have_been_made.times(3)
358
359 request(:any, "www.example.com").should_not have_been_made
360
5313e02 Added version 0.9.0 features to CHANGELOG and README
Bartosz Blimke authored
361 request(:post, "www.example.com").with { |req| req.body == "abc" }.should have_been_made
362
fa4ab29 Updated README with info about setting expectations on query params o…
Bartosz Blimke authored
363 request(:get, "www.example.com").with(:query => {"a" => ["b", "c"]}).should have_been_made
364
365 request(:post, "www.example.com").
366 with(:body => {"a" => ["b", "c"]}, :headers => 'Content-Type' => 'application/json').should have_been_made
367
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
368 ## Clearing stubs and request history
369
370 If you want to reset all current stubs and history of requests use `WebMock.reset_webmock`
371
32c653c Replaced google with example in README
Bartosz Blimke authored
372 stub_request(:any, "www.example.com")
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
373
32c653c Replaced google with example in README
Bartosz Blimke authored
374 Net::HTTP.get('www.example.com', '/') # ===> Success
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
375
376 reset_webmock
377
32c653c Replaced google with example in README
Bartosz Blimke authored
378 Net::HTTP.get('www.example.com', '/') # ===> Failure
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
379
32c653c Replaced google with example in README
Bartosz Blimke authored
380 assert_not_requested :get, "www.example.com" # ===> Success
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
381
382
383 ## Matching requests
384
385 An executed request matches stubbed request if it passes following criteria:
386
4306efa Ability to register and expect requests with empty body.
Bartosz Blimke authored
387 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
388 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
389 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
390 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/>
391 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
392
393 ## Precedence of stubs
394
395 Always the last declared stub matching the request will be applied i.e:
396
32c653c Replaced google with example in README
Bartosz Blimke authored
397 stub_request(:get, "www.example.com").to_return(:body => "abc")
398 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
399
32c653c Replaced google with example in README
Bartosz Blimke authored
400 Net::HTTP.get('www.example.com', '/') # ====> "def"
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
401
402 ## Matching URIs
403
404 WebMock will match all different representations of the same URI.
405
406 I.e all the following representations of the URI are equal:
407
32c653c Replaced google with example in README
Bartosz Blimke authored
408 "www.example.com"
409 "www.example.com/"
410 "www.example.com:80"
411 "www.example.com:80/"
412 "http://www.example.com"
413 "http://www.example.com/"
414 "http://www.example.com:80"
415 "http://www.example.com:80/"
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
416
417 The following URIs with basic authentication are also equal for WebMock
418
32c653c Replaced google with example in README
Bartosz Blimke authored
419 "a b:pass@www.example.com"
420 "a b:pass@www.example.com/"
421 "a b:pass@www.example.com:80"
422 "a b:pass@www.example.com:80/"
423 "http://a b:pass@www.example.com"
424 "http://a b:pass@www.example.com/"
425 "http://a b:pass@www.example.com:80"
426 "http://a b:pass@www.example.com:80/"
427 "a%20b:pass@www.example.com"
428 "a%20b:pass@www.example.com/"
429 "a%20b:pass@www.example.com:80"
430 "a%20b:pass@www.example.com:80/"
431 "http://a%20b:pass@www.example.com"
432 "http://a%20b:pass@www.example.com/"
433 "http://a%20b:pass@www.example.com:80"
434 "http://a%20b:pass@www.example.com:80/"
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
435
436 or these
437
78414b7 Improved examples with equality of URIs with the same parameters
Bartosz Blimke authored
438 "www.example.com/my path/?a=my param&b=c"
439 "www.example.com/my%20path/?a=my%20param&b=c"
440 "www.example.com:80/my path/?a=my param&b=c"
441 "www.example.com:80/my%20path/?a=my%20param&b=c"
442 "http://www.example.com/my path/?a=my param&b=c"
443 "http://www.example.com/my%20path/?a=my%20param&b=c"
444 "http://www.example.com:80/my path/?a=my param&b=c"
445 "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
446
447
448 If you provide Regexp to match URI, WebMock will try to match it against every valid form of the same url.
449
78414b7 Improved examples with equality of URIs with the same parameters
Bartosz Blimke authored
450 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
451
452
453 ## Matching headers
454
455 WebMock will match request headers against stubbed request headers in the following situations:
456
457 1. Stubbed request has headers specified and request headers are the same as stubbed headers <br/>
458 i.e stubbed headers: `{ 'Header1' => 'Value1', 'Header1' => 'Value1' }`, requested: `{ 'Header1' => 'Value1', 'Header1' => 'Value1' }`
459
460 2. Stubbed request has headers specified and stubbed request headers are a subset of request headers <br/>
461 i.e stubbed headers: `{ 'Header1' => 'Value1' }`, requested: `{ 'Header1' => 'Value1', 'Header1' => 'Value1' }`
462
463 3. Stubbed request has no headers <br/>
464 i.e stubbed headers: `nil`, requested: `{ 'Header1' => 'Value1', 'Header1' => 'Value1' }`
465
466 WebMock normalises headers and treats all forms of same headers as equal:
467 i.e the following two sets of headers are equal:
468
469 `{ "Header1" => "value1", :content_length => 123, :X_CuStOm_hEAder => :value }`
470
471 `{ :header1 => "value1", "Content-Length" => 123, "x-cuSTOM-HeAder" => "value" }`
472
176482c Updated README with 1.1.0 changes
Bartosz Blimke authored
473 ## Recording real requests and responses and replaying them later
474
475 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
476
5331132 Updated README with information about request callbacks
Bartosz Blimke authored
477 ## Request callbacks
478
354af66 Updated CHANGELOG width version 1.3.0 changes
Bartosz Blimke authored
479 ####WebMock can invoke callbacks stubbed or real requests:
5331132 Updated README with information about request callbacks
Bartosz Blimke authored
480
481 WebMock.after_request do |request_signature, response|
354af66 Updated CHANGELOG width version 1.3.0 changes
Bartosz Blimke authored
482 puts "Request #{request_signature} was made and #{response} was returned"
5331132 Updated README with information about request callbacks
Bartosz Blimke authored
483 end
484
485 #### invoke callbacks for real requests only and except requests made with Patron
486
487 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
488 puts "Request #{request_signature} was made and #{response} was returned"
5331132 Updated README with information about request callbacks
Bartosz Blimke authored
489 end
490
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
491 ## Bugs and Issues
492
493 Please submit them here [http://github.com/bblimke/webmock/issues](http://github.com/bblimke/webmock/issues)
494
495 ## Suggestions
496
497 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)
498
499 I'm particularly interested in how the DSL could be improved.
500
501 ## Credits
502
00424d9 Updated credits in README
Bartosz Blimke authored
503 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
504 Thanks to my fellow [Bambinos](http://new-bamboo.co.uk/) for all the great suggestions!
505
00424d9 Updated credits in README
Bartosz Blimke authored
506 People who submitted patches and new features or suggested improvements. Many thanks to these people:
507
508 * Ben Pickles
509 * Mark Evans
510 * Ivan Vega
511 * Piotr Usewicz
512 * Nick Plante
513 * Nick Quaranto
514 * Diego E. "Flameeyes" Pettenò
515 * Niels Meersschaert
516 * Mack Earnhardt
517 * Arvicco
518 * Sergio Gil
519 * Jeffrey Jones
520 * Tekin Suleyman
521 * Tom Ward
522 * Nadim Bitar
ee7b3b4 Fixed spelling
Bartosz Blimke authored
523 * Myron Marston
9c8c463 Updated README with information about RSpec 2 support.
Bartosz Blimke authored
524 * Sam Phillips
2e88f99 Updated info on how to include WebMock in Test::Unit and RSpec withou…
Bartosz Blimke authored
525 * Jose Angel Cortinas
52698d8 Updated credits
Bartosz Blimke authored
526 * Razic
fd2472a Version 1.2.2 changelog
Bartosz Blimke authored
527 * Steve Tooke
fb18a01 Updated README with info about matching request body against hash
Bartosz Blimke authored
528 * Nathaniel Bibler
15f888e @bblimke The less hacky way to get the stream behaviour working for em-http-re…
authored
529 * Martyn Loughran
60fa244 @bblimke Updated changelog
authored
530 * Muness Alrubaie
67a0819 @bblimke Version 1.3.5 changelog + updated docs
authored
531 * Charles Li
532 * Ryan Bigg
00424d9 Updated credits in README
Bartosz Blimke authored
533
534 ## Background
535
95e8298 Revert "WebMock module has to be included manually and is not include…
Bartosz Blimke authored
536 Thank you Fakeweb! This library was inspired by [FakeWeb](fakeweb.rubyforge.org).
00424d9 Updated credits in README
Bartosz Blimke authored
537 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
538 Fakeweb architecture unfortunately didn't allow me to extend it easily with the features I needed.
539 I also preferred some things to work differently i.e request stub precedence.
540
541 ## Copyright
542
bf18e89 Changed license from GPL to MIT
Bartosz Blimke authored
543 Copyright (c) 2009-2010 Bartosz Blimke. See LICENSE for details.
Something went wrong with that request. Please try again.