summaryrefslogtreecommitdiff
path: root/test/open-uri
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
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')
-rw-r--r--test/open-uri/test_open-uri.rb93
-rw-r--r--test/open-uri/test_ssl.rb124
2 files changed, 93 insertions, 124 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
diff --git a/test/open-uri/test_ssl.rb b/test/open-uri/test_ssl.rb
index 154bcf4..d1ea571 100644
--- a/test/open-uri/test_ssl.rb
+++ b/test/open-uri/test_ssl.rb
@@ -18,7 +18,7 @@ class TestOpenURISSL
def NullLog.<<(arg)
end
- def with_https(log_is_empty=true)
+ def with_https(log_tester=lambda {|log| assert_equal([], log) })
log = []
logger = WEBrick::Log.new(log, WEBrick::BasicLog::WARN)
Dir.mktmpdir {|dr|
@@ -34,16 +34,22 @@ class TestOpenURISSL
:BindAddress => '127.0.0.1',
:Port => 0})
_, port, _, host = srv.listeners[0].addr
- begin
- th = srv.start
- yield srv, dr, "https://#{host}:#{port}", th, log
- ensure
- srv.shutdown
- th.join
- end
- if log_is_empty
- assert_equal([], log)
- end
+ threads = []
+ server_thread = srv.start
+ threads << Thread.new {
+ server_thread.join
+ if log_tester
+ log_tester.call(log)
+ end
+ }
+ threads << Thread.new {
+ begin
+ yield srv, dr, "https://#{host}:#{port}", server_thread, log, threads
+ ensure
+ srv.shutdown
+ end
+ }
+ assert_join_threads(threads)
}
end
@@ -85,28 +91,20 @@ class TestOpenURISSL
end
def test_validation_failure
- with_https(false) {|srv, dr, url, server_thread, server_log|
- client_thread = Thread.new {
- begin
- setup_validation(srv, dr)
- assert_raise(OpenSSL::SSL::SSLError) { open("#{url}/data") {} }
- ensure
- srv.shutdown
- end
- }
- server_thread2 = Thread.new {
- server_thread.join
- assert_equal(1, server_log.length)
- assert_match(/ERROR OpenSSL::SSL::SSLError:/, server_log[0])
- }
- assert_join_threads([client_thread, server_thread2])
+ log_tester = lambda {|server_log|
+ assert_equal(1, server_log.length)
+ assert_match(/ERROR OpenSSL::SSL::SSLError:/, server_log[0])
+ }
+ with_https(log_tester) {|srv, dr, url, server_thread, server_log|
+ setup_validation(srv, dr)
+ assert_raise(OpenSSL::SSL::SSLError) { open("#{url}/data") {} }
}
end
- def with_https_proxy
+ def with_https_proxy(proxy_log_tester=lambda {|proxy_log, proxy_access_log| assert_equal([], proxy_log) })
proxy_log = []
proxy_logger = WEBrick::Log.new(proxy_log, WEBrick::BasicLog::WARN)
- with_https {|srv, dr, url, server_thread, server_log|
+ with_https {|srv, dr, url, server_thread, server_log, threads|
srv.mount_proc("/proxy", lambda { |req, res| res.body = "proxy" } )
cacert_filename = "#{dr}/cacert.pem"
open(cacert_filename, "w") {|f| f << CA_CERT }
@@ -121,60 +119,50 @@ class TestOpenURISSL
:BindAddress => '127.0.0.1',
:Port => 0})
_, proxy_port, _, proxy_host = proxy.listeners[0].addr
+ proxy_thread = proxy.start
+ threads << Thread.new {
+ proxy_thread.join
+ if proxy_log_tester
+ proxy_log_tester.call(proxy_log, proxy_access_log)
+ end
+ }
begin
- proxy_thread = proxy.start
- yield srv, dr, url, server_thread, server_log, cacert_filename, cacert_directory, proxy, proxy_host, proxy_port, proxy_thread, proxy_access_log, proxy_log
+ yield srv, dr, url, cacert_filename, cacert_directory, proxy_host, proxy_port
ensure
proxy.shutdown
- proxy_thread.join
end
}
- assert_equal([], proxy_log)
end
def test_proxy_cacert_file
- with_https_proxy {|srv, dr, url, server_thread, server_log, cacert_filename, cacert_directory, proxy, proxy_host, proxy_port, proxy_thread, proxy_access_log, proxy_log|
- client_thread = Thread.new {
- begin
- open("#{url}/proxy", :proxy=>"http://#{proxy_host}:#{proxy_port}/", :ssl_ca_cert => cacert_filename) {|f|
- assert_equal("200", f.status[0])
- assert_equal("proxy", f.read)
- }
- ensure
- proxy.shutdown
- srv.shutdown
- end
- }
- proxy_thread2 = Thread.new {
- proxy_thread.join
- assert_equal(1, proxy_access_log.length)
- assert_match(%r[CONNECT #{url.sub(%r{\Ahttps://}, '')} ], proxy_access_log[0])
- assert_equal([], proxy_log)
+ url = nil
+ proxy_log_tester = lambda {|proxy_log, proxy_access_log|
+ assert_equal(1, proxy_access_log.length)
+ assert_match(%r[CONNECT #{url.sub(%r{\Ahttps://}, '')} ], proxy_access_log[0])
+ assert_equal([], proxy_log)
+ }
+ with_https_proxy(proxy_log_tester) {|srv, dr, url_, cacert_filename, cacert_directory, proxy_host, proxy_port|
+ url = url_
+ open("#{url}/proxy", :proxy=>"http://#{proxy_host}:#{proxy_port}/", :ssl_ca_cert => cacert_filename) {|f|
+ assert_equal("200", f.status[0])
+ assert_equal("proxy", f.read)
}
- assert_join_threads([client_thread, proxy_thread2, server_thread])
}
end
def test_proxy_cacert_dir
- with_https_proxy {|srv, dr, url, server_thread, server_log, cacert_filename, cacert_directory, proxy, proxy_host, proxy_port, proxy_thread, proxy_access_log, proxy_log|
- client_thread = Thread.new {
- begin
- open("#{url}/proxy", :proxy=>"http://#{proxy_host}:#{proxy_port}/", :ssl_ca_cert => cacert_directory) {|f|
- assert_equal("200", f.status[0])
- assert_equal("proxy", f.read)
- }
- ensure
- proxy.shutdown
- srv.shutdown
- end
- }
- proxy_thread2 = Thread.new {
- proxy_thread.join
- assert_equal(1, proxy_access_log.length)
- assert_match(%r[CONNECT #{url.sub(%r{\Ahttps://}, '')} ], proxy_access_log[0])
- assert_equal([], proxy_log)
+ url = nil
+ proxy_log_tester = lambda {|proxy_log, proxy_access_log|
+ assert_equal(1, proxy_access_log.length)
+ assert_match(%r[CONNECT #{url.sub(%r{\Ahttps://}, '')} ], proxy_access_log[0])
+ assert_equal([], proxy_log)
+ }
+ with_https_proxy(proxy_log_tester) {|srv, dr, url_, cacert_filename, cacert_directory, proxy_host, proxy_port|
+ url = url_
+ open("#{url}/proxy", :proxy=>"http://#{proxy_host}:#{proxy_port}/", :ssl_ca_cert => cacert_directory) {|f|
+ assert_equal("200", f.status[0])
+ assert_equal("proxy", f.read)
}
- assert_join_threads([client_thread, proxy_thread2, server_thread])
}
end