forked from entp/ruby-openid
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_fetchers.rb
554 lines (463 loc) · 13.9 KB
/
test_fetchers.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
# -*- coding: utf-8 -*-
require "test_helper"
require 'net/http'
require 'webrick'
require 'openid/fetchers'
require 'stringio'
begin
require 'net/https'
rescue LoadError
# We need these names for testing.
module OpenSSL
module SSL
class SSLError < StandardError; end
end
end
end
module HttpResultAssertions
def assert_http_result_is(expected, result)
assert_equal expected.code, result.code
assert_equal expected.body, result.body
assert_equal expected.final_url, result.final_url
end
end
class BogusFetcher
RESPONSE = "bogus"
def fetch(url, body=nil, headers=nil, redirect_limit=5)
return BogusFetcher::RESPONSE
end
end
class FetcherTestCase < Test::Unit::TestCase
include HttpResultAssertions
include OpenID::TestUtil
@@test_header_name = 'X-test-header'
@@test_header_value = 'marmoset'
class ExpectedResponse < Net::HTTPResponse
attr_reader :final_url
def initialize(code, final_url, body="the expected body",
httpv="1.1", msg=nil)
super(httpv, code, msg)
@code = code
@body = body
@final_url = final_url
end
def body
@body
end
end
@@cases =
[
# path, status code, expected url (nil = default to path)
['/success', 200, nil],
['/notfound', 404, nil],
['/badreq', 400, nil],
['/forbidden', 403, nil],
['/error', 500, nil],
['/server_error', 503, nil],
['/301redirect', 200, '/success'],
['/302redirect', 200, '/success'],
['/303redirect', 200, '/success'],
['/307redirect', 200, '/success'],
]
def _redirect_with_code(code)
lambda { |req, resp|
resp.status = code
resp['Location'] = _uri_build('/success')
}
end
def _respond_with_code(code)
lambda { |req, resp|
resp.status = code
resp.body = "the expected body"
}
end
def _require_header
lambda { |req, resp|
assert_equal @@test_header_value, req[@@test_header_name]
assert_match 'ruby-openid', req['User-agent']
}
end
def _require_post
lambda { |req, resp|
assert_equal 'POST', req.request_method
assert_equal "postbody\n", req.body
}
end
def _redirect_loop
lambda { |req, resp|
@_redirect_counter += 1
resp.status = 302
resp['Location'] = _uri_build('/redirect_loop')
resp.body = "Fetched #{@_redirect_counter} times."
assert_block("Fetched too many times.") { @_redirect_counter < 10 }
}
end
UTF8_PAGE_CONTENT = <<-EOHTML
<html>
<head><title>UTF-8</title></head>
<body>こんにちは</body>
</html>
EOHTML
def _utf8_page
lambda { |req, resp|
resp['Content-Type'] = "text/html; charset=utf-8"
body = UTF8_PAGE_CONTENT.dup
body.force_encoding("ASCII-8BIT") if body.respond_to?(:force_encoding)
resp.body = body
}
end
def setup
@fetcher = OpenID::StandardFetcher.new
@logfile = StringIO.new
@weblog = WEBrick::Log.new(logfile=@logfile)
@server = WEBrick::HTTPServer.new(:Port => 0,
:Logger => @weblog,
:AccessLog => [])
@server_thread = Thread.new {
@server.mount_proc('/success', _respond_with_code(200))
@server.mount_proc('/301redirect', _redirect_with_code(301))
@server.mount_proc('/302redirect', _redirect_with_code(302))
@server.mount_proc('/303redirect', _redirect_with_code(303))
@server.mount_proc('/307redirect', _redirect_with_code(307))
@server.mount_proc('/badreq', _respond_with_code(400))
@server.mount_proc('/forbidden', _respond_with_code(403))
@server.mount_proc('/notfound', _respond_with_code(404))
@server.mount_proc('/error', _respond_with_code(500))
@server.mount_proc('/server_error', _respond_with_code(503))
@server.mount_proc('/require_header', _require_header)
@server.mount_proc('/redirect_to_reqheader') { |req, resp|
resp.status = 302
resp['Location'] = _uri_build('/require_header')
}
@server.mount_proc('/post', _require_post)
@server.mount_proc('/redirect_loop', _redirect_loop)
@server.mount_proc('/utf8_page', _utf8_page)
@server.start
}
@uri = _uri_build
sleep 0.2
end
def _uri_build(path='/')
u = URI::HTTP.build({
:host => @server.config[:ServerName],
:port => @server.config[:Port],
:path => path,
})
return u.to_s
end
def teardown
@server.shutdown
# Sleep a little because sometimes this blocks forever.
@server_thread.join
end
=begin
# XXX This test no longer works since we're not dealing with URI
# objects internally.
def test_final_url_tainted
uri = _uri_build('/301redirect')
result = @fetcher.fetch(uri)
final_url = URI::parse(result.final_url)
assert final_url.host.tainted?
assert final_url.path.tainted?
end
=end
def test_headers
headers = {
@@test_header_name => @@test_header_value
}
uri = _uri_build('/require_header')
result = @fetcher.fetch(uri, nil, headers)
# The real test runs under the WEBrick handler _require_header,
# this just checks the return code from that.
assert_equal '200', result.code, @logfile.string
end
def test_headers_after_redirect
headers = {
@@test_header_name => @@test_header_value
}
uri = _uri_build('/redirect_to_reqheader')
result = @fetcher.fetch(uri, nil, headers)
# The real test runs under the WEBrick handler _require_header,
# this just checks the return code from that.
assert_equal '200', result.code, @logfile.string
end
def test_post
uri = _uri_build('/post')
result = @fetcher.fetch(uri, "postbody\n")
# The real test runs under the WEBrick handler _require_header,
# this just checks the return code from that.
assert_equal '200', result.code, @logfile.string
end
def test_redirect_limit
@_redirect_counter = 0
uri = _uri_build('/redirect_loop')
assert_raise(OpenID::HTTPRedirectLimitReached) {
@fetcher.fetch(uri)
}
end
def test_utf8_page
uri = _uri_build('/utf8_page')
response = @fetcher.fetch(uri)
assert_equal(UTF8_PAGE_CONTENT, response.body)
if response.body.respond_to?(:encoding)
assert_equal(Encoding::UTF_8, response.body.encoding)
end
end
def test_cases
for path, expected_code, expected_url in @@cases
uri = _uri_build(path)
if expected_url.nil?
expected_url = uri
else
expected_url = _uri_build(expected_url)
end
expected = ExpectedResponse.new(expected_code.to_s, expected_url)
result = @fetcher.fetch(uri)
begin
assert_http_result_is expected, result
rescue Test::Unit::AssertionFailedError => err
if result.code == '500' && expected_code != 500
# Looks like our WEBrick harness broke.
msg = <<EOF
Status #{result.code} from case #{path}. Logs:
#{@logfile.string}
EOF
raise msg
end
# Wrap failure messages so we can tell which case failed.
new_msg = "#{path}: #{err.message.to_s}"
new_err = Test::Unit::AssertionFailedError.new(new_msg)
new_err.set_backtrace(err.backtrace)
raise new_err
end
end
end
def test_https_no_openssl
# Override supports_ssl? to always claim that connections don't
# support SSL. Test the behavior of fetch() for HTTPS URLs in
# that case.
f = OpenID::StandardFetcher.new
f.extend(OpenID::InstanceDefExtension)
f.instance_def(:supports_ssl?) do |conn|
false
end
begin
f.fetch("https://someurl.com/")
flunk("Expected RuntimeError")
rescue RuntimeError => why
assert_equal(why.to_s, "SSL support not found; cannot fetch https://someurl.com/")
end
end
class FakeConnection < Net::HTTP
attr_reader :use_ssl, :ca_file
def initialize *args
super
@ca_file = nil
end
def use_ssl=(v)
@use_ssl = v
end
def ca_file=(ca_file)
@ca_file = ca_file
end
end
def test_ssl_with_ca_file
f = OpenID::StandardFetcher.new
ca_file = "BOGUS"
f.ca_file = ca_file
f.extend(OpenID::InstanceDefExtension)
f.instance_def(:make_http) do |uri|
FakeConnection.new(uri.host, uri.port)
end
testcase = self
f.instance_def(:set_verified) do |conn, verified|
testcase.assert(verified)
end
conn = f.make_connection(URI::parse("https://someurl.com"))
assert_equal(conn.ca_file, ca_file)
end
def test_ssl_without_ca_file
f = OpenID::StandardFetcher.new
f.extend(OpenID::InstanceDefExtension)
f.instance_def(:make_http) do |uri|
FakeConnection.new(uri.host, uri.port)
end
testcase = self
f.instance_def(:set_verified) do |conn, verified|
testcase.assert(!verified)
end
conn = nil
assert_log_matches(/making https request to https:\/\/someurl.com without verifying/) {
conn = f.make_connection(URI::parse("https://someurl.com"))
}
assert(conn.ca_file.nil?)
end
def test_make_http_nil
f = OpenID::StandardFetcher.new
f.extend(OpenID::InstanceDefExtension)
f.instance_def(:make_http) do |uri|
nil
end
assert_raise(RuntimeError) {
f.make_connection(URI::parse("http://example.com/"))
}
end
def test_make_http_invalid
f = OpenID::StandardFetcher.new
f.extend(OpenID::InstanceDefExtension)
f.instance_def(:make_http) do |uri|
"not a Net::HTTP object"
end
assert_raise(RuntimeError) {
f.make_connection(URI::parse("http://example.com/"))
}
end
class BrokenSSLConnection
def start(&block)
raise OpenSSL::SSL::SSLError
end
end
def test_sslfetchingerror
f = OpenID::StandardFetcher.new
f.extend(OpenID::InstanceDefExtension)
f.instance_def(:make_connection) do |uri|
BrokenSSLConnection.new
end
assert_raise(OpenID::SSLFetchingError) {
f.fetch("https://bogus.com/")
}
end
class TimeoutConnection
def start(&block)
raise Timeout::Error
end
end
def test_fetchingerror
f = OpenID::StandardFetcher.new
f.extend(OpenID::InstanceDefExtension)
assert_raise(OpenID::FetchingError) {
f.fetch("https://bogus2010.com/")
}
end
class TestingException < OpenID::FetchingError; end
class NoSSLSupportConnection
def supports_ssl?
false
end
def start
yield
end
def request_get(*args)
raise TestingException
end
def post_connection_check(hostname)
raise RuntimeError
end
def use_ssl?
true
end
end
class NoUseSSLConnection < NoSSLSupportConnection
def use_ssl?
false
end
end
def test_post_connection_check_no_support_ssl
f = OpenID::StandardFetcher.new
f.extend(OpenID::InstanceDefExtension)
f.instance_def(:make_connection) do |uri|
NoSSLSupportConnection.new
end
# post_connection_check should not be called.
assert_raise(TestingException) {
f.fetch("https://bogus.com/")
}
end
def test_post_connection_check_no_use_ssl
f = OpenID::StandardFetcher.new
f.extend(OpenID::InstanceDefExtension)
f.instance_def(:make_connection) do |uri|
NoUseSSLConnection.new
end
# post_connection_check should not be called.
assert_raise(TestingException) {
f.fetch("https://bogus.com/")
}
end
class PostConnectionCheckException < OpenID::FetchingError; end
class UseSSLConnection < NoSSLSupportConnection
def use_ssl?
true
end
def post_connection_check(hostname)
raise PostConnectionCheckException
end
end
def test_post_connection_check
f = OpenID::StandardFetcher.new
f.extend(OpenID::InstanceDefExtension)
f.instance_def(:make_connection) do |uri|
UseSSLConnection.new
end
f.instance_def(:supports_ssl?) do |conn|
true
end
# post_connection_check should be called.
assert_raise(PostConnectionCheckException) {
f.fetch("https://bogus.com/")
}
end
end
class DefaultFetcherTest < Test::Unit::TestCase
def setup
OpenID.fetcher = nil
end
def test_default_fetcher
assert(OpenID.fetcher.is_a?(OpenID::StandardFetcher))
# A custom fetcher can be set
OpenID.fetcher = BogusFetcher.new
# A test fetch should call the new fetcher
assert(OpenID.fetch('not-a-url') == BogusFetcher::RESPONSE)
# Set the fetcher to nil again
OpenID.fetcher = nil
assert(OpenID.fetcher.is_a?(OpenID::StandardFetcher))
end
end
class ProxyTest < Test::Unit::TestCase
def test_proxy_unreachable
begin
f = OpenID::StandardFetcher.new('127.0.0.1', 1)
# If this tries to connect to the proxy (on port 1), I expect
# a 'connection refused' error. If it tries to contact the below
# URI first, it will get some other sort of error.
f.fetch("http://unittest.invalid")
rescue OpenID::FetchingError => why
# XXX: Is this a translatable string that is going to break?
if why.message =~ /Connection refused/
return
end
raise why
end
flunk "expected Connection Refused, but it passed."
end
def test_proxy_env
ENV['http_proxy'] = 'http://127.0.0.1:3128/'
OpenID.fetcher_use_env_http_proxy
# make_http just to give us something with readable attributes to inspect.
conn = OpenID.fetcher.make_http(URI.parse('http://127.0.0.2'))
assert_equal('127.0.0.1', conn.proxy_address)
assert_equal(3128, conn.proxy_port)
end
# These aren't fully automated tests, but if you start a proxy
# on port 8888 (tinyproxy's default) and check its logs...
# def test_proxy
# f = OpenID::StandardFetcher.new('127.0.0.1', 8888)
# result = f.fetch("http://www.example.com/")
# assert_match(/RFC.*2606/, result.body)
# end
# def test_proxy_https
# f = OpenID::StandardFetcher.new('127.0.0.1', 8888)
# result = f.fetch("https://www.myopenid.com/")
# assert_match(/myOpenID/, result.body)
# end
end