summaryrefslogtreecommitdiff
path: root/test/open-uri/test_open-uri.rb
diff options
context:
space:
mode:
authorakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-11-08 13:41:49 +0000
committerakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-11-08 13:41:49 +0000
commit8222432c9db1432995538a5d355d014c6fd1a0e9 (patch)
treebc17bb140a11b2e788d441f37e820ff8fdf9ffc2 /test/open-uri/test_open-uri.rb
parenta21e5f9cf0eb2687e345279c3243461289466845 (diff)
Use assert_join_threads.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@48317 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test/open-uri/test_open-uri.rb')
-rw-r--r--test/open-uri/test_open-uri.rb156
1 files changed, 120 insertions, 36 deletions
diff --git a/test/open-uri/test_open-uri.rb b/test/open-uri/test_open-uri.rb
index d30d898..816d7fa 100644
--- a/test/open-uri/test_open-uri.rb
+++ b/test/open-uri/test_open-uri.rb
@@ -27,7 +27,7 @@ class TestOpenURI < Test::Unit::TestCase
_, port, _, host = srv.listeners[0].addr
begin
th = srv.start
- yield srv, dr, "http://#{host}:#{port}", log
+ yield srv, dr, "http://#{host}:#{port}", th, log
ensure
srv.shutdown
th.join
@@ -81,10 +81,20 @@ class TestOpenURI < Test::Unit::TestCase
end
def test_404
- with_http(false) {|srv, dr, url, log|
- exc = assert_raise(OpenURI::HTTPError) { open("#{url}/not-exist") {} }
- assert_equal("404", exc.io.status[0])
- assert_match(%r{ERROR `/not-exist' not found}, log.string)
+ with_http(false) {|srv, dr, url, server_thread, server_log|
+ client_thread = Thread.new {
+ begin
+ exc = assert_raise(OpenURI::HTTPError) { open("#{url}/not-exist") {} }
+ assert_equal("404", exc.io.status[0])
+ ensure
+ srv.shutdown
+ end
+ }
+ server_thread2 = Thread.new {
+ server_thread.join
+ assert_match(%r{ERROR `/not-exist' not found}, server_log.string)
+ }
+ assert_join_threads([client_thread, server_thread2])
}
end
@@ -234,13 +244,15 @@ class TestOpenURI < Test::Unit::TestCase
def test_proxy
with_http {|srv, dr, url|
- log = ''
+ proxy_log = StringIO.new('')
+ proxy_logger = WEBrick::Log.new(proxy_log, WEBrick::BasicLog::WARN)
+ proxy_auth_log = ''
proxy = WEBrick::HTTPProxyServer.new({
:ServerType => Thread,
- :Logger => WEBrick::Log.new(NullLog),
+ :Logger => proxy_logger,
:AccessLog => [[NullLog, ""]],
:ProxyAuthProc => lambda {|req, res|
- log << req.request_line
+ proxy_auth_log << req.request_line
},
:BindAddress => '127.0.0.1',
:Port => 0})
@@ -253,21 +265,21 @@ class TestOpenURI < Test::Unit::TestCase
assert_equal("200", f.status[0])
assert_equal("proxy", f.read)
}
- assert_match(/#{Regexp.quote url}/, log); log.clear
+ assert_match(/#{Regexp.quote url}/, proxy_auth_log); proxy_auth_log.clear
open("#{url}/proxy", :proxy=>URI(proxy_url)) {|f|
assert_equal("200", f.status[0])
assert_equal("proxy", f.read)
}
- assert_match(/#{Regexp.quote url}/, log); log.clear
+ assert_match(/#{Regexp.quote url}/, proxy_auth_log); proxy_auth_log.clear
open("#{url}/proxy", :proxy=>nil) {|f|
assert_equal("200", f.status[0])
assert_equal("proxy", f.read)
}
- assert_equal("", log); log.clear
+ assert_equal("", proxy_auth_log); proxy_auth_log.clear
assert_raise(ArgumentError) {
open("#{url}/proxy", :proxy=>:invalid) {}
}
- assert_equal("", log); log.clear
+ 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.
open("#{url}/proxy") {|f|
@@ -275,23 +287,26 @@ class TestOpenURI < Test::Unit::TestCase
assert_equal("proxy", f.read)
}
}
- assert_equal("", log); log.clear
+ 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
+ def test_proxy_http_basic_authentication_failure
with_http {|srv, dr, url|
- log = ''
+ proxy_log = StringIO.new('')
+ proxy_logger = WEBrick::Log.new(proxy_log, WEBrick::BasicLog::WARN)
+ proxy_auth_log = ''
proxy = WEBrick::HTTPProxyServer.new({
:ServerType => Thread,
- :Logger => WEBrick::Log.new(NullLog),
+ :Logger => proxy_logger,
:AccessLog => [[NullLog, ""]],
:ProxyAuthProc => lambda {|req, res|
- log << req.request_line
+ proxy_auth_log << req.request_line
if req["Proxy-Authorization"] != "Basic #{['user:pass'].pack('m').chomp}"
raise WEBrick::HTTPStatus::ProxyAuthenticationRequired
end
@@ -305,22 +320,53 @@ class TestOpenURI < Test::Unit::TestCase
srv.mount_proc("/proxy", lambda { |req, res| res.body = "proxy" } )
exc = assert_raise(OpenURI::HTTPError) { open("#{url}/proxy", :proxy=>proxy_url) {} }
assert_equal("407", exc.io.status[0])
- assert_match(/#{Regexp.quote url}/, log); log.clear
+ assert_match(/#{Regexp.quote url}/, proxy_auth_log); proxy_auth_log.clear
+ ensure
+ proxy.shutdown
+ th.join
+ end
+ assert_match(/ERROR WEBrick::HTTPStatus::ProxyAuthenticationRequired/, proxy_log.string)
+ }
+ end
+
+ def test_proxy_http_basic_authentication_success
+ with_http {|srv, dr, url|
+ proxy_log = StringIO.new('')
+ proxy_logger = WEBrick::Log.new(proxy_log, WEBrick::BasicLog::WARN)
+ proxy_auth_log = ''
+ proxy = WEBrick::HTTPProxyServer.new({
+ :ServerType => Thread,
+ :Logger => proxy_logger,
+ :AccessLog => [[NullLog, ""]],
+ :ProxyAuthProc => lambda {|req, res|
+ proxy_auth_log << req.request_line
+ if req["Proxy-Authorization"] != "Basic #{['user:pass'].pack('m').chomp}"
+ raise WEBrick::HTTPStatus::ProxyAuthenticationRequired
+ end
+ },
+ :BindAddress => '127.0.0.1',
+ :Port => 0})
+ _, proxy_port, _, proxy_host = proxy.listeners[0].addr
+ proxy_url = "http://#{proxy_host}:#{proxy_port}/"
+ begin
+ th = proxy.start
+ srv.mount_proc("/proxy", lambda { |req, res| res.body = "proxy" } )
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}/, log); log.clear
+ assert_match(/#{Regexp.quote url}/, proxy_auth_log); proxy_auth_log.clear
assert_raise(ArgumentError) {
open("#{url}/proxy",
:proxy_http_basic_authentication=>[true, "user", "pass"]) {}
}
- assert_equal("", log); log.clear
+ assert_equal("", proxy_auth_log); proxy_auth_log.clear
ensure
proxy.shutdown
th.join
end
+ assert_equal("", proxy_log.string)
}
end
@@ -405,25 +451,63 @@ class TestOpenURI < Test::Unit::TestCase
}
end
- def test_redirect_auth
- with_http(false) {|srv, dr, url, log|
- srv.mount_proc("/r1/") {|req, res| res.status = 301; res["location"] = "#{url}/r2" }
- srv.mount_proc("/r2/") {|req, res|
- if req["Authorization"] != "Basic #{['user:pass'].pack('m').chomp}"
- raise WEBrick::HTTPStatus::Unauthorized
- end
- res.body = "r2"
- }
- exc = assert_raise(OpenURI::HTTPError) { open("#{url}/r2/") {} }
- assert_equal("401", exc.io.status[0])
- assert_match(/ERROR WEBrick::HTTPStatus::Unauthorized/, log.string)
- log.rewind; log.truncate(0)
+ def setup_redirect_auth(srv, url)
+ srv.mount_proc("/r1/") {|req, res|
+ res.status = 301
+ res["location"] = "#{url}/r2"
+ }
+ srv.mount_proc("/r2/") {|req, res|
+ if req["Authorization"] != "Basic #{['user:pass'].pack('m').chomp}"
+ raise WEBrick::HTTPStatus::Unauthorized
+ end
+ res.body = "r2"
+ }
+ end
+
+ def test_redirect_auth_success
+ with_http {|srv, dr, url|
+ setup_redirect_auth(srv, url)
open("#{url}/r2/", :http_basic_authentication=>['user', 'pass']) {|f|
assert_equal("r2", f.read)
}
- exc = assert_raise(OpenURI::HTTPError) { open("#{url}/r1/", :http_basic_authentication=>['user', 'pass']) {} }
- assert_equal("401", exc.io.status[0])
- assert_match(/ERROR WEBrick::HTTPStatus::Unauthorized/, log.string)
+ }
+ end
+
+ def test_redirect_auth_failure_r2
+ with_http(false) {|srv, dr, url, server_thread, server_log|
+ setup_redirect_auth(srv, url)
+ client_thread = Thread.new {
+ begin
+ exc = assert_raise(OpenURI::HTTPError) { open("#{url}/r2/") {} }
+ assert_equal("401", exc.io.status[0])
+ ensure
+ srv.shutdown
+ end
+ }
+ server_thread2 = Thread.new {
+ server_thread.join
+ assert_match(/ERROR WEBrick::HTTPStatus::Unauthorized/, server_log.string)
+ }
+ assert_join_threads([client_thread, server_thread2])
+ }
+ end
+
+ def test_redirect_auth_failure_r1
+ with_http(false) {|srv, dr, url, server_thread, server_log|
+ setup_redirect_auth(srv, url)
+ client_thread = Thread.new {
+ begin
+ exc = assert_raise(OpenURI::HTTPError) { open("#{url}/r1/", :http_basic_authentication=>['user', 'pass']) {} }
+ assert_equal("401", exc.io.status[0])
+ ensure
+ srv.shutdown
+ end
+ }
+ server_thread2 = Thread.new {
+ server_thread.join
+ assert_match(/ERROR WEBrick::HTTPStatus::Unauthorized/, server_log.string)
+ }
+ assert_join_threads([client_thread, server_thread2])
}
end