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
|
# frozen_string_literal: true
require 'test/unit'
require 'open-uri'
require 'stringio'
require_relative 'utils'
class TestOpenURIProxy < Test::Unit::TestCase
include TestOpenURIUtils
def with_env(h)
begin
old = {}
h.each_key {|k| old[k] = ENV[k] }
h.each {|k, v| ENV[k] = v }
yield
ensure
h.each_key {|k| ENV[k] = old[k] }
end
end
def setup
@proxies = %w[http_proxy HTTP_PROXY ftp_proxy FTP_PROXY no_proxy]
@old_proxies = @proxies.map {|k| ENV[k] }
@proxies.each {|k| ENV[k] = nil }
end
def teardown
@proxies.each_with_index {|k, i| ENV[k] = @old_proxies[i] }
end
def test_proxy
with_http {|srv, url|
proxy_log = StringIO.new(''.dup)
proxy_access_log = StringIO.new(''.dup)
proxy_auth_log = ''.dup
proxy_host = '127.0.0.1'
proxy = SimpleHTTPProxyServer.new(proxy_host, 0, lambda {|req, res|
proxy_auth_log << req.request_line
}, proxy_log, proxy_access_log)
proxy_port = proxy.instance_variable_get(:@server).addr[1]
proxy_url = "http://#{proxy_host}:#{proxy_port}/"
begin
proxy_thread = proxy.start
srv.mount_proc("/proxy", lambda { |req, res| res.body = "proxy" } )
URI.open("#{url}/proxy", :proxy=>proxy_url) {|f|
assert_equal("200", f.status[0])
assert_equal("proxy", f.read)
}
assert_match(/#{Regexp.quote url}/, proxy_auth_log); proxy_auth_log.clear
URI.open("#{url}/proxy", :proxy=>URI(proxy_url)) {|f|
assert_equal("200", f.status[0])
assert_equal("proxy", f.read)
}
assert_match(/#{Regexp.quote url}/, proxy_auth_log); proxy_auth_log.clear
URI.open("#{url}/proxy", :proxy=>nil) {|f|
assert_equal("200", f.status[0])
assert_equal("proxy", f.read)
}
assert_equal("", proxy_auth_log); proxy_auth_log.clear
assert_raise(ArgumentError) {
URI.open("#{url}/proxy", :proxy=>:invalid) {}
}
assert_equal("", proxy_auth_log); proxy_auth_log.clear
with_env("http_proxy"=>proxy_url) {
# should not use proxy for 127.0.0.0/8.
URI.open("#{url}/proxy") {|f|
assert_equal("200", f.status[0])
assert_equal("proxy", f.read)
}
}
assert_equal("", proxy_auth_log); proxy_auth_log.clear
ensure
proxy.shutdown
proxy_thread.join
end
assert_equal("", proxy_log.string)
}
end
def test_proxy_http_basic_authentication_failure
with_http {|srv, url|
proxy_log = StringIO.new(''.dup)
proxy_access_log = StringIO.new(''.dup)
proxy_auth_log = ''.dup
proxy_host = '127.0.0.1'
proxy = SimpleHTTPProxyServer.new(proxy_host, 0, lambda {|req, res|
proxy_auth_log << req.request_line
if req["Proxy-Authorization"] != "Basic #{['user:pass'].pack('m').chomp}"
raise ProxyAuthenticationRequired
end
}, proxy_log, proxy_access_log)
proxy_port = proxy.instance_variable_get(:@server).addr[1]
proxy_url = "http://#{proxy_host}:#{proxy_port}/"
begin
th = proxy.start
srv.mount_proc("/proxy", lambda { |req, res| res.body = "proxy" } )
exc = assert_raise(OpenURI::HTTPError) { URI.open("#{url}/proxy", :proxy=>proxy_url) {} }
assert_equal("407", exc.io.status[0])
assert_match(/#{Regexp.quote url}/, proxy_auth_log); proxy_auth_log.clear
ensure
proxy.shutdown
th.join
end
assert_match(/ERROR ProxyAuthenticationRequired/, proxy_log.string)
}
end
def test_proxy_http_basic_authentication_success
with_http {|srv, url|
proxy_log = StringIO.new(''.dup)
proxy_access_log = StringIO.new(''.dup)
proxy_auth_log = ''.dup
proxy_host = '127.0.0.1'
proxy = SimpleHTTPProxyServer.new(proxy_host, 0, lambda {|req, res|
proxy_auth_log << req.request_line
if req["Proxy-Authorization"] != "Basic #{['user:pass'].pack('m').chomp}"
raise ProxyAuthenticationRequired
end
}, proxy_log, proxy_access_log)
proxy_port = proxy.instance_variable_get(:@server).addr[1]
proxy_url = "http://#{proxy_host}:#{proxy_port}/"
begin
th = proxy.start
srv.mount_proc("/proxy", lambda { |req, res| res.body = "proxy" } )
URI.open("#{url}/proxy",
:proxy_http_basic_authentication=>[proxy_url, "user", "pass"]) {|f|
assert_equal("200", f.status[0])
assert_equal("proxy", f.read)
}
assert_match(/#{Regexp.quote url}/, proxy_auth_log); proxy_auth_log.clear
assert_raise(ArgumentError) {
URI.open("#{url}/proxy",
:proxy_http_basic_authentication=>[true, "user", "pass"]) {}
}
assert_equal("", proxy_auth_log); proxy_auth_log.clear
ensure
proxy.shutdown
th.join
end
assert_equal("", proxy_log.string)
}
end
def test_authenticated_proxy_http_basic_authentication_success
with_http {|srv, url|
proxy_log = StringIO.new(''.dup)
proxy_access_log = StringIO.new(''.dup)
proxy_auth_log = ''.dup
proxy_host = '127.0.0.1'
proxy = SimpleHTTPProxyServer.new(proxy_host, 0, lambda {|req, res|
proxy_auth_log << req.request_line
if req["Proxy-Authorization"] != "Basic #{['user:pass'].pack('m').chomp}"
raise ProxyAuthenticationRequired
end
}, proxy_log, proxy_access_log)
proxy_port = proxy.instance_variable_get(:@server).addr[1]
proxy_url = "http://user:pass@#{proxy_host}:#{proxy_port}/"
begin
th = proxy.start
srv.mount_proc("/proxy", lambda { |req, res| res.body = "proxy" } )
URI.open("#{url}/proxy", :proxy => proxy_url) {|f|
assert_equal("200", f.status[0])
assert_equal("proxy", f.read)
}
assert_match(/#{Regexp.quote url}/, proxy_auth_log); proxy_auth_log.clear
assert_equal("", proxy_auth_log); proxy_auth_log.clear
ensure
proxy.shutdown
th.join
end
assert_equal("", proxy_log.string)
}
end
end
|