sandro-fakeweb 1.2.5
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +163 -0
- data/LICENSE.txt +281 -0
- data/README.rdoc +216 -0
- data/Rakefile +78 -0
- data/lib/fake_web.rb +204 -0
- data/lib/fake_web/ext/net_http.rb +76 -0
- data/lib/fake_web/fixture.rb +60 -0
- data/lib/fake_web/registry.rb +127 -0
- data/lib/fake_web/responder.rb +113 -0
- data/lib/fake_web/response.rb +10 -0
- data/lib/fake_web/stub_socket.rb +15 -0
- data/lib/fake_web/utility.rb +44 -0
- data/lib/fakeweb.rb +2 -0
- data/test/fixtures/google_response_from_curl +12 -0
- data/test/fixtures/google_response_with_transfer_encoding +17 -0
- data/test/fixtures/google_response_without_transfer_encoding +11 -0
- data/test/fixtures/test_example.txt +1 -0
- data/test/fixtures/test_txt_file +3 -0
- data/test/test_allow_net_connect.rb +85 -0
- data/test/test_deprecations.rb +54 -0
- data/test/test_fake_authentication.rb +92 -0
- data/test/test_fake_web.rb +535 -0
- data/test/test_fake_web_fixture.rb +64 -0
- data/test/test_fake_web_generate_fixtures.rb +71 -0
- data/test/test_fake_web_open_uri.rb +58 -0
- data/test/test_helper.rb +87 -0
- data/test/test_missing_open_uri.rb +25 -0
- data/test/test_precedence.rb +79 -0
- data/test/test_query_string.rb +45 -0
- data/test/test_regexes.rb +152 -0
- data/test/test_response_headers.rb +73 -0
- data/test/test_trailing_slashes.rb +53 -0
- data/test/test_utility.rb +91 -0
- metadata +127 -0
@@ -0,0 +1,535 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "test_helper")
|
2
|
+
|
3
|
+
class TestFakeWeb < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def test_register_uri
|
6
|
+
FakeWeb.register_uri(:get, 'http://mock/test_example.txt', :body => "example")
|
7
|
+
assert FakeWeb.registered_uri?(:get, 'http://mock/test_example.txt')
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_register_uri_with_wrong_number_of_arguments
|
11
|
+
assert_raises ArgumentError do
|
12
|
+
FakeWeb.register_uri("http://example.com")
|
13
|
+
end
|
14
|
+
assert_raises ArgumentError do
|
15
|
+
FakeWeb.register_uri(:get, "http://example.com", "/example", :body => "example")
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_registered_uri_with_wrong_number_of_arguments
|
20
|
+
assert_raises ArgumentError do
|
21
|
+
FakeWeb.registered_uri?
|
22
|
+
end
|
23
|
+
assert_raises ArgumentError do
|
24
|
+
FakeWeb.registered_uri?(:get, "http://example.com", "/example")
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_response_for_with_wrong_number_of_arguments
|
29
|
+
assert_raises ArgumentError do
|
30
|
+
FakeWeb.response_for
|
31
|
+
end
|
32
|
+
assert_raises ArgumentError do
|
33
|
+
FakeWeb.response_for(:get, "http://example.com", "/example")
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_register_uri_without_domain_name
|
38
|
+
assert_raises URI::InvalidURIError do
|
39
|
+
FakeWeb.register_uri(:get, 'test_example2.txt', File.dirname(__FILE__) + '/fixtures/test_example.txt')
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_register_uri_with_port_and_check_with_port
|
44
|
+
FakeWeb.register_uri(:get, 'http://example.com:3000/', :body => 'foo')
|
45
|
+
assert FakeWeb.registered_uri?(:get, 'http://example.com:3000/')
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_register_uri_with_port_and_check_without_port
|
49
|
+
FakeWeb.register_uri(:get, 'http://example.com:3000/', :body => 'foo')
|
50
|
+
assert !FakeWeb.registered_uri?(:get, 'http://example.com/')
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_register_uri_with_default_port_for_http_and_check_without_port
|
54
|
+
FakeWeb.register_uri(:get, 'http://example.com:80/', :body => 'foo')
|
55
|
+
assert FakeWeb.registered_uri?(:get, 'http://example.com/')
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_register_uri_with_default_port_for_https_and_check_without_port
|
59
|
+
FakeWeb.register_uri(:get, 'https://example.com:443/', :body => 'foo')
|
60
|
+
assert FakeWeb.registered_uri?(:get, 'https://example.com/')
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_register_uri_with_no_port_for_http_and_check_with_default_port
|
64
|
+
FakeWeb.register_uri(:get, 'http://example.com/', :body => 'foo')
|
65
|
+
assert FakeWeb.registered_uri?(:get, 'http://example.com:80/')
|
66
|
+
end
|
67
|
+
|
68
|
+
def test_register_uri_with_no_port_for_https_and_check_with_default_port
|
69
|
+
FakeWeb.register_uri(:get, 'https://example.com/', :body => 'foo')
|
70
|
+
assert FakeWeb.registered_uri?(:get, 'https://example.com:443/')
|
71
|
+
end
|
72
|
+
|
73
|
+
def test_register_uri_with_no_port_for_https_and_check_with_443_on_http
|
74
|
+
FakeWeb.register_uri(:get, 'https://example.com/', :body => 'foo')
|
75
|
+
assert !FakeWeb.registered_uri?(:get, 'http://example.com:443/')
|
76
|
+
end
|
77
|
+
|
78
|
+
def test_register_uri_with_no_port_for_http_and_check_with_80_on_https
|
79
|
+
FakeWeb.register_uri(:get, 'http://example.com/', :body => 'foo')
|
80
|
+
assert !FakeWeb.registered_uri?(:get, 'https://example.com:80/')
|
81
|
+
end
|
82
|
+
|
83
|
+
def test_register_uri_for_any_method_explicitly
|
84
|
+
FakeWeb.register_uri(:any, "http://example.com/rpc_endpoint", :body => "OK")
|
85
|
+
assert FakeWeb.registered_uri?(:get, "http://example.com/rpc_endpoint")
|
86
|
+
assert FakeWeb.registered_uri?(:post, "http://example.com/rpc_endpoint")
|
87
|
+
assert FakeWeb.registered_uri?(:put, "http://example.com/rpc_endpoint")
|
88
|
+
assert FakeWeb.registered_uri?(:delete, "http://example.com/rpc_endpoint")
|
89
|
+
assert FakeWeb.registered_uri?(:any, "http://example.com/rpc_endpoint")
|
90
|
+
capture_stderr do # silence deprecation warning
|
91
|
+
assert FakeWeb.registered_uri?("http://example.com/rpc_endpoint")
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
def test_register_uri_for_get_method_only
|
96
|
+
FakeWeb.register_uri(:get, "http://example.com/users", :body => "User list")
|
97
|
+
assert FakeWeb.registered_uri?(:get, "http://example.com/users")
|
98
|
+
assert !FakeWeb.registered_uri?(:post, "http://example.com/users")
|
99
|
+
assert !FakeWeb.registered_uri?(:put, "http://example.com/users")
|
100
|
+
assert !FakeWeb.registered_uri?(:delete, "http://example.com/users")
|
101
|
+
assert !FakeWeb.registered_uri?(:any, "http://example.com/users")
|
102
|
+
capture_stderr do # silence deprecation warning
|
103
|
+
assert !FakeWeb.registered_uri?("http://example.com/users")
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
def test_clean_registry_affects_registered_uri
|
108
|
+
FakeWeb.register_uri(:get, "http://example.com", :body => "registered")
|
109
|
+
assert FakeWeb.registered_uri?(:get, "http://example.com")
|
110
|
+
FakeWeb.clean_registry
|
111
|
+
assert !FakeWeb.registered_uri?(:get, "http://example.com")
|
112
|
+
end
|
113
|
+
|
114
|
+
def test_clean_registry_affects_net_http_requests
|
115
|
+
FakeWeb.register_uri(:get, "http://example.com", :body => "registered")
|
116
|
+
response = Net::HTTP.start("example.com") { |query| query.get("/") }
|
117
|
+
assert_equal "registered", response.body
|
118
|
+
FakeWeb.clean_registry
|
119
|
+
assert_raise FakeWeb::NetConnectNotAllowedError do
|
120
|
+
Net::HTTP.start("example.com") { |query| query.get("/") }
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
def test_response_for_with_registered_uri
|
125
|
+
FakeWeb.register_uri(:get, 'http://mock/test_example.txt', :body => File.dirname(__FILE__) + '/fixtures/test_example.txt')
|
126
|
+
assert_equal 'test example content', FakeWeb.response_for(:get, 'http://mock/test_example.txt').body
|
127
|
+
end
|
128
|
+
|
129
|
+
def test_response_for_with_unknown_uri
|
130
|
+
assert_nil FakeWeb.response_for(:get, 'http://example.com/')
|
131
|
+
end
|
132
|
+
|
133
|
+
def test_response_for_with_put_method
|
134
|
+
FakeWeb.register_uri(:put, "http://example.com", :body => "response")
|
135
|
+
assert_equal 'response', FakeWeb.response_for(:put, "http://example.com").body
|
136
|
+
end
|
137
|
+
|
138
|
+
def test_response_for_with_any_method_explicitly
|
139
|
+
FakeWeb.register_uri(:any, "http://example.com", :body => "response")
|
140
|
+
assert_equal 'response', FakeWeb.response_for(:get, "http://example.com").body
|
141
|
+
assert_equal 'response', FakeWeb.response_for(:any, "http://example.com").body
|
142
|
+
end
|
143
|
+
|
144
|
+
def test_content_for_registered_uri_with_port_and_request_with_port
|
145
|
+
FakeWeb.register_uri(:get, 'http://example.com:3000/', :body => 'test example content')
|
146
|
+
response = Net::HTTP.start('example.com', 3000) { |http| http.get('/') }
|
147
|
+
assert_equal 'test example content', response.body
|
148
|
+
end
|
149
|
+
|
150
|
+
def test_content_for_registered_uri_with_default_port_for_http_and_request_without_port
|
151
|
+
FakeWeb.register_uri(:get, 'http://example.com:80/', :body => 'test example content')
|
152
|
+
response = Net::HTTP.start('example.com') { |http| http.get('/') }
|
153
|
+
assert_equal 'test example content', response.body
|
154
|
+
end
|
155
|
+
|
156
|
+
def test_content_for_registered_uri_with_no_port_for_http_and_request_with_default_port
|
157
|
+
FakeWeb.register_uri(:get, 'http://example.com/', :body => 'test example content')
|
158
|
+
response = Net::HTTP.start('example.com', 80) { |http| http.get('/') }
|
159
|
+
assert_equal 'test example content', response.body
|
160
|
+
end
|
161
|
+
|
162
|
+
def test_content_for_registered_uri_with_default_port_for_https_and_request_with_default_port
|
163
|
+
FakeWeb.register_uri(:get, 'https://example.com:443/', :body => 'test example content')
|
164
|
+
http = Net::HTTP.new('example.com', 443)
|
165
|
+
http.use_ssl = true
|
166
|
+
response = http.get('/')
|
167
|
+
assert_equal 'test example content', response.body
|
168
|
+
end
|
169
|
+
|
170
|
+
def test_content_for_registered_uri_with_no_port_for_https_and_request_with_default_port
|
171
|
+
FakeWeb.register_uri(:get, 'https://example.com/', :body => 'test example content')
|
172
|
+
http = Net::HTTP.new('example.com', 443)
|
173
|
+
http.use_ssl = true
|
174
|
+
response = http.get('/')
|
175
|
+
assert_equal 'test example content', response.body
|
176
|
+
end
|
177
|
+
|
178
|
+
def test_content_for_registered_uris_with_ports_on_same_domain_and_request_without_port
|
179
|
+
FakeWeb.register_uri(:get, 'http://example.com:3000/', :body => 'port 3000')
|
180
|
+
FakeWeb.register_uri(:get, 'http://example.com/', :body => 'port 80')
|
181
|
+
response = Net::HTTP.start('example.com') { |http| http.get('/') }
|
182
|
+
assert_equal 'port 80', response.body
|
183
|
+
end
|
184
|
+
|
185
|
+
def test_content_for_registered_uris_with_ports_on_same_domain_and_request_with_port
|
186
|
+
FakeWeb.register_uri(:get, 'http://example.com:3000/', :body => 'port 3000')
|
187
|
+
FakeWeb.register_uri(:get, 'http://example.com/', :body => 'port 80')
|
188
|
+
response = Net::HTTP.start('example.com', 3000) { |http| http.get('/') }
|
189
|
+
assert_equal 'port 3000', response.body
|
190
|
+
end
|
191
|
+
|
192
|
+
def test_content_for_registered_uri_with_get_method_only
|
193
|
+
FakeWeb.allow_net_connect = false
|
194
|
+
FakeWeb.register_uri(:get, "http://example.com/", :body => "test example content")
|
195
|
+
http = Net::HTTP.new('example.com')
|
196
|
+
assert_equal 'test example content', http.get('/').body
|
197
|
+
assert_raises(FakeWeb::NetConnectNotAllowedError) { http.post('/', nil) }
|
198
|
+
assert_raises(FakeWeb::NetConnectNotAllowedError) { http.put('/', nil) }
|
199
|
+
assert_raises(FakeWeb::NetConnectNotAllowedError) { http.delete('/') }
|
200
|
+
end
|
201
|
+
|
202
|
+
def test_content_for_registered_uri_with_any_method_explicitly
|
203
|
+
FakeWeb.allow_net_connect = false
|
204
|
+
FakeWeb.register_uri(:any, "http://example.com/", :body => "test example content")
|
205
|
+
http = Net::HTTP.new('example.com')
|
206
|
+
assert_equal 'test example content', http.get('/').body
|
207
|
+
assert_equal 'test example content', http.post('/', nil).body
|
208
|
+
assert_equal 'test example content', http.put('/', nil).body
|
209
|
+
assert_equal 'test example content', http.delete('/').body
|
210
|
+
end
|
211
|
+
|
212
|
+
def test_content_for_registered_uri_with_any_method_implicitly
|
213
|
+
FakeWeb.allow_net_connect = false
|
214
|
+
capture_stderr do # silence deprecation warning
|
215
|
+
FakeWeb.register_uri("http://example.com/", :body => "test example content")
|
216
|
+
end
|
217
|
+
|
218
|
+
http = Net::HTTP.new('example.com')
|
219
|
+
assert_equal 'test example content', http.get('/').body
|
220
|
+
assert_equal 'test example content', http.post('/', nil).body
|
221
|
+
assert_equal 'test example content', http.put('/', nil).body
|
222
|
+
assert_equal 'test example content', http.delete('/').body
|
223
|
+
end
|
224
|
+
|
225
|
+
def test_mock_request_with_block
|
226
|
+
FakeWeb.register_uri(:get, 'http://mock/test_example.txt', :body => File.dirname(__FILE__) + '/fixtures/test_example.txt')
|
227
|
+
response = Net::HTTP.start('mock') { |http| http.get('/test_example.txt') }
|
228
|
+
assert_equal 'test example content', response.body
|
229
|
+
end
|
230
|
+
|
231
|
+
def test_request_with_registered_body_yields_the_response_body_to_a_request_block
|
232
|
+
FakeWeb.register_uri(:get, "http://example.com", :body => "content")
|
233
|
+
body = nil
|
234
|
+
Net::HTTP.start("example.com") do |http|
|
235
|
+
http.get("/") do |response_body|
|
236
|
+
body = response_body
|
237
|
+
end
|
238
|
+
end
|
239
|
+
assert_equal "content", body
|
240
|
+
end
|
241
|
+
|
242
|
+
def test_request_with_registered_response_yields_the_response_body_to_a_request_block
|
243
|
+
fake_response = Net::HTTPOK.new('1.1', '200', 'OK')
|
244
|
+
fake_response.instance_variable_set(:@body, "content")
|
245
|
+
FakeWeb.register_uri(:get, 'http://example.com', :response => fake_response)
|
246
|
+
body = nil
|
247
|
+
Net::HTTP.start("example.com") do |http|
|
248
|
+
http.get("/") do |response_body|
|
249
|
+
body = response_body
|
250
|
+
end
|
251
|
+
end
|
252
|
+
assert_equal "content", body
|
253
|
+
end
|
254
|
+
|
255
|
+
def test_mock_request_with_undocumented_full_uri_argument_style
|
256
|
+
FakeWeb.register_uri(:get, 'http://mock/test_example.txt', :body => File.dirname(__FILE__) + '/fixtures/test_example.txt')
|
257
|
+
response = Net::HTTP.start('mock') { |query| query.get('http://mock/test_example.txt') }
|
258
|
+
assert_equal 'test example content', response.body
|
259
|
+
end
|
260
|
+
|
261
|
+
def test_mock_request_with_undocumented_full_uri_argument_style_and_query
|
262
|
+
FakeWeb.register_uri(:get, 'http://mock/test_example.txt?a=b', :body => 'test query content')
|
263
|
+
response = Net::HTTP.start('mock') { |query| query.get('http://mock/test_example.txt?a=b') }
|
264
|
+
assert_equal 'test query content', response.body
|
265
|
+
end
|
266
|
+
|
267
|
+
def test_mock_post
|
268
|
+
FakeWeb.register_uri(:post, 'http://mock/test_example.txt', :body => File.dirname(__FILE__) + '/fixtures/test_example.txt')
|
269
|
+
response = Net::HTTP.start('mock') { |query| query.post('/test_example.txt', '') }
|
270
|
+
assert_equal 'test example content', response.body
|
271
|
+
end
|
272
|
+
|
273
|
+
def test_mock_post_with_string_as_registered_uri
|
274
|
+
FakeWeb.register_uri(:post, 'http://mock/test_string.txt', :body => 'foo')
|
275
|
+
response = Net::HTTP.start('mock') { |query| query.post('/test_string.txt', '') }
|
276
|
+
assert_equal 'foo', response.body
|
277
|
+
end
|
278
|
+
|
279
|
+
def test_mock_get_with_request_as_registered_uri
|
280
|
+
fake_response = Net::HTTPOK.new('1.1', '200', 'OK')
|
281
|
+
FakeWeb.register_uri(:get, 'http://mock/test_response', :response => fake_response)
|
282
|
+
response = Net::HTTP.start('mock') { |query| query.get('/test_response') }
|
283
|
+
assert_equal fake_response, response
|
284
|
+
end
|
285
|
+
|
286
|
+
def test_mock_get_with_request_from_file_as_registered_uri
|
287
|
+
FakeWeb.register_uri(:get, 'http://www.google.com/', :response => File.dirname(__FILE__) + '/fixtures/google_response_without_transfer_encoding')
|
288
|
+
response = Net::HTTP.start('www.google.com') { |query| query.get('/') }
|
289
|
+
assert_equal '200', response.code
|
290
|
+
assert response.body.include?('<title>Google</title>')
|
291
|
+
end
|
292
|
+
|
293
|
+
def test_mock_post_with_request_from_file_as_registered_uri
|
294
|
+
FakeWeb.register_uri(:post, 'http://www.google.com/', :response => File.dirname(__FILE__) + '/fixtures/google_response_without_transfer_encoding')
|
295
|
+
response = Net::HTTP.start('www.google.com') { |query| query.post('/', '') }
|
296
|
+
assert_equal "200", response.code
|
297
|
+
assert response.body.include?('<title>Google</title>')
|
298
|
+
end
|
299
|
+
|
300
|
+
def test_proxy_request
|
301
|
+
FakeWeb.register_uri(:get, 'http://www.example.com/', :body => "hello world")
|
302
|
+
FakeWeb.register_uri(:get, 'http://your.proxy.host/', :body => "lala")
|
303
|
+
|
304
|
+
response = nil
|
305
|
+
Net::HTTP::Proxy('your.proxy.host', 8080).start('www.example.com') do |http|
|
306
|
+
response = http.get('/')
|
307
|
+
end
|
308
|
+
assert_equal "hello world", response.body
|
309
|
+
end
|
310
|
+
|
311
|
+
def test_https_request
|
312
|
+
FakeWeb.register_uri(:get, 'https://www.example.com/', :body => "Hello World")
|
313
|
+
http = Net::HTTP.new('www.example.com', 443)
|
314
|
+
http.use_ssl = true
|
315
|
+
response = http.get('/')
|
316
|
+
assert_equal "Hello World", response.body
|
317
|
+
end
|
318
|
+
|
319
|
+
def test_register_unimplemented_response
|
320
|
+
FakeWeb.register_uri(:get, 'http://mock/unimplemented', :response => 1)
|
321
|
+
assert_raises StandardError do
|
322
|
+
Net::HTTP.start('mock') { |q| q.get('/unimplemented') }
|
323
|
+
end
|
324
|
+
end
|
325
|
+
|
326
|
+
def test_real_http_request
|
327
|
+
FakeWeb.allow_net_connect = true
|
328
|
+
setup_expectations_for_real_apple_hot_news_request
|
329
|
+
|
330
|
+
resp = nil
|
331
|
+
Net::HTTP.start('images.apple.com') do |query|
|
332
|
+
resp = query.get('/main/rss/hotnews/hotnews.rss')
|
333
|
+
end
|
334
|
+
assert resp.body.include?('Apple')
|
335
|
+
assert resp.body.include?('News')
|
336
|
+
end
|
337
|
+
|
338
|
+
def test_real_http_request_with_undocumented_full_uri_argument_style
|
339
|
+
FakeWeb.allow_net_connect = true
|
340
|
+
setup_expectations_for_real_apple_hot_news_request(:path => 'http://images.apple.com/main/rss/hotnews/hotnews.rss')
|
341
|
+
|
342
|
+
resp = nil
|
343
|
+
Net::HTTP.start('images.apple.com') do |query|
|
344
|
+
resp = query.get('http://images.apple.com/main/rss/hotnews/hotnews.rss')
|
345
|
+
end
|
346
|
+
assert resp.body.include?('Apple')
|
347
|
+
assert resp.body.include?('News')
|
348
|
+
end
|
349
|
+
|
350
|
+
def test_real_https_request
|
351
|
+
FakeWeb.allow_net_connect = true
|
352
|
+
setup_expectations_for_real_apple_hot_news_request(:port => 443)
|
353
|
+
|
354
|
+
http = Net::HTTP.new('images.apple.com', 443)
|
355
|
+
http.use_ssl = true
|
356
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE # silence certificate warning
|
357
|
+
response = http.get('/main/rss/hotnews/hotnews.rss')
|
358
|
+
assert response.body.include?('Apple')
|
359
|
+
assert response.body.include?('News')
|
360
|
+
end
|
361
|
+
|
362
|
+
def test_real_request_on_same_domain_as_mock
|
363
|
+
FakeWeb.allow_net_connect = true
|
364
|
+
setup_expectations_for_real_apple_hot_news_request
|
365
|
+
|
366
|
+
FakeWeb.register_uri(:get, 'http://images.apple.com/test_string.txt', :body => 'foo')
|
367
|
+
|
368
|
+
resp = nil
|
369
|
+
Net::HTTP.start('images.apple.com') do |query|
|
370
|
+
resp = query.get('/main/rss/hotnews/hotnews.rss')
|
371
|
+
end
|
372
|
+
assert resp.body.include?('Apple')
|
373
|
+
assert resp.body.include?('News')
|
374
|
+
end
|
375
|
+
|
376
|
+
def test_mock_request_on_real_domain
|
377
|
+
FakeWeb.register_uri(:get, 'http://images.apple.com/test_string.txt', :body => 'foo')
|
378
|
+
resp = nil
|
379
|
+
Net::HTTP.start('images.apple.com') do |query|
|
380
|
+
resp = query.get('/test_string.txt')
|
381
|
+
end
|
382
|
+
assert_equal 'foo', resp.body
|
383
|
+
end
|
384
|
+
|
385
|
+
def test_mock_post_that_raises_exception
|
386
|
+
FakeWeb.register_uri(:post, 'http://mock/raising_exception.txt', :exception => StandardError)
|
387
|
+
assert_raises(StandardError) do
|
388
|
+
Net::HTTP.start('mock') do |query|
|
389
|
+
query.post('/raising_exception.txt', 'some data')
|
390
|
+
end
|
391
|
+
end
|
392
|
+
end
|
393
|
+
|
394
|
+
def test_mock_post_that_raises_an_http_error
|
395
|
+
FakeWeb.register_uri(:post, 'http://mock/raising_exception.txt', :exception => Net::HTTPError)
|
396
|
+
assert_raises(Net::HTTPError) do
|
397
|
+
Net::HTTP.start('mock') do |query|
|
398
|
+
query.post('/raising_exception.txt', '')
|
399
|
+
end
|
400
|
+
end
|
401
|
+
end
|
402
|
+
|
403
|
+
def test_raising_an_exception_that_requires_an_argument_to_instantiate
|
404
|
+
FakeWeb.register_uri(:get, "http://example.com/timeout.txt", :exception => Timeout::Error)
|
405
|
+
assert_raises(Timeout::Error) do
|
406
|
+
Net::HTTP.get(URI.parse("http://example.com/timeout.txt"))
|
407
|
+
end
|
408
|
+
end
|
409
|
+
|
410
|
+
def test_mock_instance_syntax
|
411
|
+
FakeWeb.register_uri(:get, 'http://mock/test_example.txt', :body => File.dirname(__FILE__) + '/fixtures/test_example.txt')
|
412
|
+
response = nil
|
413
|
+
uri = URI.parse('http://mock/test_example.txt')
|
414
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
415
|
+
response = http.start do
|
416
|
+
http.get(uri.path)
|
417
|
+
end
|
418
|
+
|
419
|
+
assert_equal 'test example content', response.body
|
420
|
+
end
|
421
|
+
|
422
|
+
def test_mock_via_nil_proxy
|
423
|
+
response = nil
|
424
|
+
proxy_address = nil
|
425
|
+
proxy_port = nil
|
426
|
+
FakeWeb.register_uri(:get, 'http://mock/test_example.txt', :body => File.dirname(__FILE__) + '/fixtures/test_example.txt')
|
427
|
+
uri = URI.parse('http://mock/test_example.txt')
|
428
|
+
http = Net::HTTP::Proxy(proxy_address, proxy_port).new(
|
429
|
+
uri.host, (uri.port or 80))
|
430
|
+
response = http.start do
|
431
|
+
http.get(uri.path)
|
432
|
+
end
|
433
|
+
|
434
|
+
assert_equal 'test example content', response.body
|
435
|
+
end
|
436
|
+
|
437
|
+
def test_response_type
|
438
|
+
FakeWeb.register_uri(:get, 'http://mock/test_example.txt', :body => "test")
|
439
|
+
response = Net::HTTP.start('mock') { |http| http.get('/test_example.txt') }
|
440
|
+
assert_kind_of Net::HTTPSuccess, response
|
441
|
+
end
|
442
|
+
|
443
|
+
def test_mock_request_that_raises_an_http_error_with_a_specific_status
|
444
|
+
FakeWeb.register_uri(:get, 'http://mock/raising_exception.txt', :exception => Net::HTTPError, :status => ['404', 'Not Found'])
|
445
|
+
exception = assert_raises(Net::HTTPError) do
|
446
|
+
Net::HTTP.start('mock') { |http| http.get('/raising_exception.txt') }
|
447
|
+
end
|
448
|
+
assert_equal '404', exception.response.code
|
449
|
+
assert_equal 'Not Found', exception.response.msg
|
450
|
+
end
|
451
|
+
|
452
|
+
def test_mock_rotate_responses
|
453
|
+
FakeWeb.register_uri(:get, 'http://mock/multiple_test_example.txt',
|
454
|
+
[ {:body => File.dirname(__FILE__) + '/fixtures/test_example.txt', :times => 2},
|
455
|
+
{:body => "thrice", :times => 3},
|
456
|
+
{:body => "ever_more"} ])
|
457
|
+
|
458
|
+
uri = URI.parse('http://mock/multiple_test_example.txt')
|
459
|
+
2.times { assert_equal 'test example content', Net::HTTP.get(uri) }
|
460
|
+
3.times { assert_equal 'thrice', Net::HTTP.get(uri) }
|
461
|
+
4.times { assert_equal 'ever_more', Net::HTTP.get(uri) }
|
462
|
+
end
|
463
|
+
|
464
|
+
def test_mock_request_using_response_with_transfer_encoding_header_has_valid_transfer_encoding_header
|
465
|
+
FakeWeb.register_uri(:get, 'http://www.google.com/', :response => File.dirname(__FILE__) + '/fixtures/google_response_with_transfer_encoding')
|
466
|
+
response = Net::HTTP.start('www.google.com') { |query| query.get('/') }
|
467
|
+
assert_not_nil response['transfer-encoding']
|
468
|
+
assert response['transfer-encoding'] == 'chunked'
|
469
|
+
end
|
470
|
+
|
471
|
+
def test_mock_request_using_response_without_transfer_encoding_header_does_not_have_a_transfer_encoding_header
|
472
|
+
FakeWeb.register_uri(:get, 'http://www.google.com/', :response => File.dirname(__FILE__) + '/fixtures/google_response_without_transfer_encoding')
|
473
|
+
response = nil
|
474
|
+
response = Net::HTTP.start('www.google.com') { |query| query.get('/') }
|
475
|
+
assert !response.key?('transfer-encoding')
|
476
|
+
end
|
477
|
+
|
478
|
+
def test_mock_request_using_response_from_curl_has_original_transfer_encoding_header
|
479
|
+
FakeWeb.register_uri(:get, 'http://www.google.com/', :response => File.dirname(__FILE__) + '/fixtures/google_response_from_curl')
|
480
|
+
response = Net::HTTP.start('www.google.com') { |query| query.get('/') }
|
481
|
+
assert_not_nil response['transfer-encoding']
|
482
|
+
assert response['transfer-encoding'] == 'chunked'
|
483
|
+
end
|
484
|
+
|
485
|
+
def test_txt_file_should_have_three_lines
|
486
|
+
FakeWeb.register_uri(:get, 'http://www.google.com/', :body => File.dirname(__FILE__) + '/fixtures/test_txt_file')
|
487
|
+
response = Net::HTTP.start('www.google.com') { |query| query.get('/') }
|
488
|
+
assert response.body.split(/\n/).size == 3, "response has #{response.body.split(/\n/).size} lines should have 3"
|
489
|
+
end
|
490
|
+
|
491
|
+
def test_requiring_fakeweb_instead_of_fake_web
|
492
|
+
require "fakeweb"
|
493
|
+
end
|
494
|
+
|
495
|
+
def test_registering_with_string_containing_null_byte
|
496
|
+
# Regression test for File.exists? raising an ArgumentError ("string
|
497
|
+
# contains null byte") since :response first tries to find by filename.
|
498
|
+
# The string should be treated as a response body, instead, and an
|
499
|
+
# EOFError is raised when the byte is encountered.
|
500
|
+
FakeWeb.register_uri(:get, "http://example.com", :response => "test\0test")
|
501
|
+
assert_raise EOFError do
|
502
|
+
Net::HTTP.get(URI.parse("http://example.com"))
|
503
|
+
end
|
504
|
+
|
505
|
+
FakeWeb.register_uri(:get, "http://example.com", :body => "test\0test")
|
506
|
+
body = Net::HTTP.get(URI.parse("http://example.com"))
|
507
|
+
assert_equal "test\0test", body
|
508
|
+
end
|
509
|
+
|
510
|
+
def test_registering_with_string_that_is_a_directory_name
|
511
|
+
# Similar to above, but for Errno::EISDIR being raised since File.exists?
|
512
|
+
# returns true for directories
|
513
|
+
FakeWeb.register_uri(:get, "http://example.com", :response => File.dirname(__FILE__))
|
514
|
+
assert_raise EOFError do
|
515
|
+
body = Net::HTTP.get(URI.parse("http://example.com"))
|
516
|
+
end
|
517
|
+
|
518
|
+
FakeWeb.register_uri(:get, "http://example.com", :body => File.dirname(__FILE__))
|
519
|
+
body = Net::HTTP.get(URI.parse("http://example.com"))
|
520
|
+
assert_equal File.dirname(__FILE__), body
|
521
|
+
end
|
522
|
+
|
523
|
+
def test_http_version_from_string_response
|
524
|
+
FakeWeb.register_uri(:get, "http://example.com", :body => "example")
|
525
|
+
response = Net::HTTP.start("example.com") { |http| http.get("/") }
|
526
|
+
assert_equal "1.0", response.http_version
|
527
|
+
end
|
528
|
+
|
529
|
+
def test_http_version_from_file_response
|
530
|
+
FakeWeb.register_uri(:get, "http://example.com", :body => File.dirname(__FILE__) + '/fixtures/test_example.txt')
|
531
|
+
response = Net::HTTP.start("example.com") { |http| http.get("/") }
|
532
|
+
assert_equal "1.0", response.http_version
|
533
|
+
end
|
534
|
+
|
535
|
+
end
|