Skip to content

HTTPS clone URL

Subversion checkout URL

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