ivanvr-fakeweb 1.2.5.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,526 @@
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", "request body", "/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", "request body", "/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_an_http_error
386
+ FakeWeb.register_uri(:post, 'http://mock/raising_exception.txt', :exception => Net::HTTPError)
387
+ assert_raises(Net::HTTPError) do
388
+ Net::HTTP.start('mock') do |query|
389
+ query.post('/raising_exception.txt', '')
390
+ end
391
+ end
392
+ end
393
+
394
+ def test_raising_an_exception_that_requires_an_argument_to_instantiate
395
+ FakeWeb.register_uri(:get, "http://example.com/timeout.txt", :exception => Timeout::Error)
396
+ assert_raises(Timeout::Error) do
397
+ Net::HTTP.get(URI.parse("http://example.com/timeout.txt"))
398
+ end
399
+ end
400
+
401
+ def test_mock_instance_syntax
402
+ FakeWeb.register_uri(:get, 'http://mock/test_example.txt', :body => File.dirname(__FILE__) + '/fixtures/test_example.txt')
403
+ response = nil
404
+ uri = URI.parse('http://mock/test_example.txt')
405
+ http = Net::HTTP.new(uri.host, uri.port)
406
+ response = http.start do
407
+ http.get(uri.path)
408
+ end
409
+
410
+ assert_equal 'test example content', response.body
411
+ end
412
+
413
+ def test_mock_via_nil_proxy
414
+ response = nil
415
+ proxy_address = nil
416
+ proxy_port = nil
417
+ FakeWeb.register_uri(:get, 'http://mock/test_example.txt', :body => File.dirname(__FILE__) + '/fixtures/test_example.txt')
418
+ uri = URI.parse('http://mock/test_example.txt')
419
+ http = Net::HTTP::Proxy(proxy_address, proxy_port).new(
420
+ uri.host, (uri.port or 80))
421
+ response = http.start do
422
+ http.get(uri.path)
423
+ end
424
+
425
+ assert_equal 'test example content', response.body
426
+ end
427
+
428
+ def test_response_type
429
+ FakeWeb.register_uri(:get, 'http://mock/test_example.txt', :body => "test")
430
+ response = Net::HTTP.start('mock') { |http| http.get('/test_example.txt') }
431
+ assert_kind_of Net::HTTPSuccess, response
432
+ end
433
+
434
+ def test_mock_request_that_raises_an_http_error_with_a_specific_status
435
+ FakeWeb.register_uri(:get, 'http://mock/raising_exception.txt', :exception => Net::HTTPError, :status => ['404', 'Not Found'])
436
+ exception = assert_raises(Net::HTTPError) do
437
+ Net::HTTP.start('mock') { |http| http.get('/raising_exception.txt') }
438
+ end
439
+ assert_equal '404', exception.response.code
440
+ assert_equal 'Not Found', exception.response.msg
441
+ end
442
+
443
+ def test_mock_rotate_responses
444
+ FakeWeb.register_uri(:get, 'http://mock/multiple_test_example.txt',
445
+ [ {:body => File.dirname(__FILE__) + '/fixtures/test_example.txt', :times => 2},
446
+ {:body => "thrice", :times => 3},
447
+ {:body => "ever_more"} ])
448
+
449
+ uri = URI.parse('http://mock/multiple_test_example.txt')
450
+ 2.times { assert_equal 'test example content', Net::HTTP.get(uri) }
451
+ 3.times { assert_equal 'thrice', Net::HTTP.get(uri) }
452
+ 4.times { assert_equal 'ever_more', Net::HTTP.get(uri) }
453
+ end
454
+
455
+ def test_mock_request_using_response_with_transfer_encoding_header_has_valid_transfer_encoding_header
456
+ FakeWeb.register_uri(:get, 'http://www.google.com/', :response => File.dirname(__FILE__) + '/fixtures/google_response_with_transfer_encoding')
457
+ response = Net::HTTP.start('www.google.com') { |query| query.get('/') }
458
+ assert_not_nil response['transfer-encoding']
459
+ assert response['transfer-encoding'] == 'chunked'
460
+ end
461
+
462
+ def test_mock_request_using_response_without_transfer_encoding_header_does_not_have_a_transfer_encoding_header
463
+ FakeWeb.register_uri(:get, 'http://www.google.com/', :response => File.dirname(__FILE__) + '/fixtures/google_response_without_transfer_encoding')
464
+ response = nil
465
+ response = Net::HTTP.start('www.google.com') { |query| query.get('/') }
466
+ assert !response.key?('transfer-encoding')
467
+ end
468
+
469
+ def test_mock_request_using_response_from_curl_has_original_transfer_encoding_header
470
+ FakeWeb.register_uri(:get, 'http://www.google.com/', :response => File.dirname(__FILE__) + '/fixtures/google_response_from_curl')
471
+ response = Net::HTTP.start('www.google.com') { |query| query.get('/') }
472
+ assert_not_nil response['transfer-encoding']
473
+ assert response['transfer-encoding'] == 'chunked'
474
+ end
475
+
476
+ def test_txt_file_should_have_three_lines
477
+ FakeWeb.register_uri(:get, 'http://www.google.com/', :body => File.dirname(__FILE__) + '/fixtures/test_txt_file')
478
+ response = Net::HTTP.start('www.google.com') { |query| query.get('/') }
479
+ assert response.body.split(/\n/).size == 3, "response has #{response.body.split(/\n/).size} lines should have 3"
480
+ end
481
+
482
+ def test_requiring_fakeweb_instead_of_fake_web
483
+ require "fakeweb"
484
+ end
485
+
486
+ def test_registering_with_string_containing_null_byte
487
+ # Regression test for File.exists? raising an ArgumentError ("string
488
+ # contains null byte") since :response first tries to find by filename.
489
+ # The string should be treated as a response body, instead, and an
490
+ # EOFError is raised when the byte is encountered.
491
+ FakeWeb.register_uri(:get, "http://example.com", :response => "test\0test")
492
+ assert_raise EOFError do
493
+ Net::HTTP.get(URI.parse("http://example.com"))
494
+ end
495
+
496
+ FakeWeb.register_uri(:get, "http://example.com", :body => "test\0test")
497
+ body = Net::HTTP.get(URI.parse("http://example.com"))
498
+ assert_equal "test\0test", body
499
+ end
500
+
501
+ def test_registering_with_string_that_is_a_directory_name
502
+ # Similar to above, but for Errno::EISDIR being raised since File.exists?
503
+ # returns true for directories
504
+ FakeWeb.register_uri(:get, "http://example.com", :response => File.dirname(__FILE__))
505
+ assert_raise EOFError do
506
+ body = Net::HTTP.get(URI.parse("http://example.com"))
507
+ end
508
+
509
+ FakeWeb.register_uri(:get, "http://example.com", :body => File.dirname(__FILE__))
510
+ body = Net::HTTP.get(URI.parse("http://example.com"))
511
+ assert_equal File.dirname(__FILE__), body
512
+ end
513
+
514
+ def test_http_version_from_string_response
515
+ FakeWeb.register_uri(:get, "http://example.com", :body => "example")
516
+ response = Net::HTTP.start("example.com") { |http| http.get("/") }
517
+ assert_equal "1.0", response.http_version
518
+ end
519
+
520
+ def test_http_version_from_file_response
521
+ FakeWeb.register_uri(:get, "http://example.com", :body => File.dirname(__FILE__) + '/fixtures/test_example.txt')
522
+ response = Net::HTTP.start("example.com") { |http| http.get("/") }
523
+ assert_equal "1.0", response.http_version
524
+ end
525
+
526
+ end
@@ -0,0 +1,58 @@
1
+ require File.join(File.dirname(__FILE__), "test_helper")
2
+
3
+ class TestFakeWebOpenURI < Test::Unit::TestCase
4
+
5
+ def test_content_for_registered_uri
6
+ FakeWeb.register_uri(:get, 'http://mock/test_example.txt', :body => File.dirname(__FILE__) + '/fixtures/test_example.txt')
7
+ assert_equal 'test example content', FakeWeb.response_for(:get, 'http://mock/test_example.txt').body
8
+ end
9
+
10
+ def test_mock_open
11
+ FakeWeb.register_uri(:get, 'http://mock/test_example.txt', :body => File.dirname(__FILE__) + '/fixtures/test_example.txt')
12
+ assert_equal 'test example content', open('http://mock/test_example.txt').read
13
+ end
14
+
15
+ def test_mock_open_with_string_as_registered_uri
16
+ FakeWeb.register_uri(:get, 'http://mock/test_string.txt', :body => 'foo')
17
+ assert_equal 'foo', open('http://mock/test_string.txt').string
18
+ end
19
+
20
+ def test_real_open
21
+ FakeWeb.allow_net_connect = true
22
+ setup_expectations_for_real_apple_hot_news_request
23
+ resp = open('http://images.apple.com/main/rss/hotnews/hotnews.rss')
24
+ assert_equal "200", resp.status.first
25
+ body = resp.read
26
+ assert body.include?('Apple')
27
+ assert body.include?('News')
28
+ end
29
+
30
+ def test_mock_open_that_raises_exception
31
+ FakeWeb.register_uri(:get, 'http://mock/raising_exception.txt', :exception => StandardError)
32
+ assert_raises(StandardError) do
33
+ open('http://mock/raising_exception.txt')
34
+ end
35
+ end
36
+
37
+ def test_mock_open_that_raises_an_http_error
38
+ FakeWeb.register_uri(:get, 'http://mock/raising_exception.txt', :exception => OpenURI::HTTPError)
39
+ assert_raises(OpenURI::HTTPError) do
40
+ open('http://mock/raising_exception.txt')
41
+ end
42
+ end
43
+
44
+ def test_mock_open_that_raises_an_http_error_with_a_specific_status
45
+ FakeWeb.register_uri(:get, 'http://mock/raising_exception.txt', :exception => OpenURI::HTTPError, :status => ['123', 'jodel'])
46
+ exception = assert_raises(OpenURI::HTTPError) do
47
+ open('http://mock/raising_exception.txt')
48
+ end
49
+ assert_equal '123', exception.io.code
50
+ assert_equal 'jodel', exception.io.message
51
+ end
52
+
53
+ def test_mock_open_with_block
54
+ FakeWeb.register_uri(:get, 'http://mock/test_example.txt', :body => File.dirname(__FILE__) + '/fixtures/test_example.txt')
55
+ body = open('http://mock/test_example.txt') { |f| f.readlines }
56
+ assert_equal 'test example content', body.first
57
+ end
58
+ end
@@ -0,0 +1,74 @@
1
+ $:.unshift "#{File.dirname(__FILE__)}/../lib"
2
+
3
+ require 'test/unit'
4
+ require 'open-uri'
5
+ require 'fake_web'
6
+ require 'rubygems'
7
+ require 'mocha'
8
+
9
+
10
+ # Give all tests a common setup and teardown that prevents shared state
11
+ class Test::Unit::TestCase
12
+ def setup
13
+ FakeWeb.clean_registry
14
+ @original_allow_net_connect = FakeWeb.allow_net_connect?
15
+ FakeWeb.allow_net_connect = false
16
+ end
17
+
18
+ def teardown
19
+ FakeWeb.allow_net_connect = @original_allow_net_connect
20
+ end
21
+ end
22
+
23
+
24
+ module FakeWebTestHelper
25
+
26
+ def capture_stderr
27
+ $stderr = StringIO.new
28
+ yield
29
+ $stderr.rewind && $stderr.read
30
+ ensure
31
+ $stderr = STDERR
32
+ end
33
+
34
+ # Sets several expectations (using Mocha) that a real HTTP request makes it
35
+ # past FakeWeb to the socket layer. You can use this when you need to check
36
+ # that a request isn't handled by FakeWeb.
37
+ def setup_expectations_for_real_request(options = {})
38
+ # Socket handling
39
+ if options[:port] == 443
40
+ socket = mock("SSLSocket")
41
+ OpenSSL::SSL::SSLSocket.expects(:===).with(socket).returns(true).at_least_once
42
+ OpenSSL::SSL::SSLSocket.expects(:new).with(socket, instance_of(OpenSSL::SSL::SSLContext)).returns(socket).at_least_once
43
+ socket.stubs(:sync_close=).returns(true)
44
+ socket.expects(:connect).with().at_least_once
45
+ else
46
+ socket = mock("TCPSocket")
47
+ Socket.expects(:===).with(socket).returns(true)
48
+ end
49
+
50
+ TCPSocket.expects(:open).with(options[:host], options[:port]).returns(socket).at_least_once
51
+ socket.stubs(:closed?).returns(false)
52
+ socket.stubs(:close).returns(true)
53
+
54
+ # Request/response handling
55
+ request_parts = ["#{options[:method]} #{options[:path]} HTTP/1.1", "Host: #{options[:host]}"]
56
+ socket.expects(:write).with(all_of(includes(request_parts[0]), includes(request_parts[1]))).returns(100)
57
+
58
+ socket.expects(:sysread).at_least_once.returns("HTTP/1.1 #{options[:response_code]} #{options[:response_message]}\nContent-Length: #{options[:response_body].length}\n\n#{options[:response_body]}").then.raises(EOFError)
59
+ end
60
+
61
+
62
+ # A helper that calls #setup_expectations_for_real_request for you, using
63
+ # defaults for our commonly used test request to images.apple.com.
64
+ def setup_expectations_for_real_apple_hot_news_request(options = {})
65
+ defaults = { :host => "images.apple.com", :port => 80, :method => "GET",
66
+ :path => "/main/rss/hotnews/hotnews.rss",
67
+ :response_code => 200, :response_message => "OK",
68
+ :response_body => "<title>Apple Hot News</title>" }
69
+ setup_expectations_for_real_request(defaults.merge(options))
70
+ end
71
+
72
+ end
73
+
74
+ Test::Unit::TestCase.send(:include, FakeWebTestHelper)