Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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