summaryrefslogtreecommitdiff
path: root/test/open-uri/test_open-uri.rb
diff options
context:
space:
mode:
authorakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-11-09 14:01:20 +0000
committerakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-11-09 14:01:20 +0000
commitad58f04833b6d9f6b0d01c5fc8adc61df6d088cc (patch)
treef2484838869a97b049adf8b1f487513f5554569c /test/open-uri/test_open-uri.rb
parent0d07bc203c421e9ebda8ef3ae50230f6b1866a71 (diff)
* test/open-uri: Test server log in server thread.
* test/webrick: Ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@48347 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test/open-uri/test_open-uri.rb')
-rw-r--r--test/open-uri/test_open-uri.rb93
1 files changed, 37 insertions, 56 deletions
diff --git a/test/open-uri/test_open-uri.rb b/test/open-uri/test_open-uri.rb
index d2d8840..3545f5d 100644
--- a/test/open-uri/test_open-uri.rb
+++ b/test/open-uri/test_open-uri.rb
@@ -13,7 +13,7 @@ class TestOpenURI < Test::Unit::TestCase
def NullLog.<<(arg)
end
- def with_http(log_is_empty=true)
+ def with_http(log_tester=lambda {|log| assert_equal([], log) })
log = []
logger = WEBrick::Log.new(log, WEBrick::BasicLog::WARN)
Dir.mktmpdir {|dr|
@@ -25,17 +25,22 @@ class TestOpenURI < Test::Unit::TestCase
:BindAddress => '127.0.0.1',
:Port => 0})
_, port, _, host = srv.listeners[0].addr
- begin
- th = srv.start
- yield srv, dr, "http://#{host}:#{port}", th, log
- ensure
- srv.shutdown
- th.join
- end
+ server_thread = srv.start
+ server_thread2 = Thread.new {
+ server_thread.join
+ if log_tester
+ log_tester.call(log)
+ end
+ }
+ client_thread = Thread.new {
+ begin
+ yield srv, dr, "http://#{host}:#{port}", server_thread, log
+ ensure
+ srv.shutdown
+ end
+ }
+ assert_join_threads([client_thread, server_thread2])
}
- if log_is_empty
- assert_equal([], log)
- end
end
def with_env(h)
@@ -81,21 +86,13 @@ class TestOpenURI < Test::Unit::TestCase
end
def test_404
- 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_equal(1, server_log.length)
- assert_match(%r{ERROR `/not-exist' not found}, server_log[0])
- }
- assert_join_threads([client_thread, server_thread2])
+ log_tester = lambda {|server_log|
+ assert_equal(1, server_log.length)
+ assert_match(%r{ERROR `/not-exist' not found}, server_log[0])
+ }
+ with_http(log_tester) {|srv, dr, url, server_thread, server_log|
+ exc = assert_raise(OpenURI::HTTPError) { open("#{url}/not-exist") {} }
+ assert_equal("404", exc.io.status[0])
}
end
@@ -475,42 +472,26 @@ class TestOpenURI < Test::Unit::TestCase
end
def test_redirect_auth_failure_r2
- with_http(false) {|srv, dr, url, server_thread, server_log|
+ log_tester = lambda {|server_log|
+ assert_equal(1, server_log.length)
+ assert_match(/ERROR WEBrick::HTTPStatus::Unauthorized/, server_log[0])
+ }
+ with_http(log_tester) {|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_equal(1, server_log.length)
- assert_match(/ERROR WEBrick::HTTPStatus::Unauthorized/, server_log[0])
- }
- assert_join_threads([client_thread, server_thread2])
+ exc = assert_raise(OpenURI::HTTPError) { open("#{url}/r2/") {} }
+ assert_equal("401", exc.io.status[0])
}
end
def test_redirect_auth_failure_r1
- with_http(false) {|srv, dr, url, server_thread, server_log|
+ log_tester = lambda {|server_log|
+ assert_equal(1, server_log.length)
+ assert_match(/ERROR WEBrick::HTTPStatus::Unauthorized/, server_log[0])
+ }
+ with_http(log_tester) {|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_equal(1, server_log.length)
- assert_match(/ERROR WEBrick::HTTPStatus::Unauthorized/, server_log[0])
- }
- assert_join_threads([client_thread, server_thread2])
+ exc = assert_raise(OpenURI::HTTPError) { open("#{url}/r1/", :http_basic_authentication=>['user', 'pass']) {} }
+ assert_equal("401", exc.io.status[0])
}
end