* test/webrick: Store log in an array.

* test/net/http: Ditto.

* test/open-uri: Ditto.



git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@48341 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
akr 2014-11-09 11:51:06 +00:00
parent 9b559f194c
commit 070c310e87
9 changed files with 45 additions and 33 deletions

View File

@ -1,3 +1,11 @@
Sun Nov 9 20:29:01 2014 Tanaka Akira <akr@fsij.org>
* test/webrick: Store log in an array.
* test/net/http: Ditto.
* test/open-uri: Ditto.
Sun Nov 9 18:35:36 2014 Tanaka Akira <akr@fsij.org> Sun Nov 9 18:35:36 2014 Tanaka Akira <akr@fsij.org>
* test/xmlrpc: Refine log test. * test/xmlrpc: Refine log test.

View File

@ -277,7 +277,7 @@ module TestNetHTTP_version_1_1_methods
end end
} }
assert_equal 1, i assert_equal 1, i
@log_pattern = nil # server may encount ECONNRESET @log_tester = nil # server may encount ECONNRESET
end end
def test_get__implicit_start def test_get__implicit_start

View File

@ -117,7 +117,10 @@ class TestNetHTTPS < Test::Unit::TestCase
end end
} }
assert_match(/certificate verify failed/, ex.message) assert_match(/certificate verify failed/, ex.message)
@log_pattern = /ERROR OpenSSL::SSL::SSLError:/ @log_tester = lambda {|log|
assert_equal(1, log.length)
assert_match(/ERROR OpenSSL::SSL::SSLError:/, log[0])
}
end end
def test_identity_verify_failure def test_identity_verify_failure

View File

@ -36,14 +36,14 @@ module TestNetHTTPUtils
@server.shutdown @server.shutdown
@server_thread.join @server_thread.join
end end
assert_match(@log_pattern, @log.string) if @log_pattern @log_tester.call(@log) if @log_tester
# resume global state # resume global state
Net::HTTP.version_1_2 Net::HTTP.version_1_2
end end
def spawn_server def spawn_server
@log = StringIO.new('') @log = []
@log_pattern = /\A\z/ @log_tester = lambda {|log| assert_equal([], log ) }
@config = self.class::CONFIG @config = self.class::CONFIG
server_config = { server_config = {
:BindAddress => config('host'), :BindAddress => config('host'),

View File

@ -14,7 +14,7 @@ class TestOpenURI < Test::Unit::TestCase
end end
def with_http(log_is_empty=true) def with_http(log_is_empty=true)
log = StringIO.new('') log = []
logger = WEBrick::Log.new(log, WEBrick::BasicLog::WARN) logger = WEBrick::Log.new(log, WEBrick::BasicLog::WARN)
Dir.mktmpdir {|dr| Dir.mktmpdir {|dr|
srv = WEBrick::HTTPServer.new({ srv = WEBrick::HTTPServer.new({
@ -34,7 +34,7 @@ class TestOpenURI < Test::Unit::TestCase
end end
} }
if log_is_empty if log_is_empty
assert_equal("", log.string) assert_equal([], log)
end end
end end
@ -92,7 +92,8 @@ class TestOpenURI < Test::Unit::TestCase
} }
server_thread2 = Thread.new { server_thread2 = Thread.new {
server_thread.join server_thread.join
assert_match(%r{ERROR `/not-exist' not found}, server_log.string) assert_equal(1, server_log.length)
assert_match(%r{ERROR `/not-exist' not found}, server_log[0])
} }
assert_join_threads([client_thread, server_thread2]) assert_join_threads([client_thread, server_thread2])
} }
@ -486,7 +487,8 @@ class TestOpenURI < Test::Unit::TestCase
} }
server_thread2 = Thread.new { server_thread2 = Thread.new {
server_thread.join server_thread.join
assert_match(/ERROR WEBrick::HTTPStatus::Unauthorized/, server_log.string) assert_equal(1, server_log.length)
assert_match(/ERROR WEBrick::HTTPStatus::Unauthorized/, server_log[0])
} }
assert_join_threads([client_thread, server_thread2]) assert_join_threads([client_thread, server_thread2])
} }
@ -505,7 +507,8 @@ class TestOpenURI < Test::Unit::TestCase
} }
server_thread2 = Thread.new { server_thread2 = Thread.new {
server_thread.join server_thread.join
assert_match(/ERROR WEBrick::HTTPStatus::Unauthorized/, server_log.string) assert_equal(1, server_log.length)
assert_match(/ERROR WEBrick::HTTPStatus::Unauthorized/, server_log[0])
} }
assert_join_threads([client_thread, server_thread2]) assert_join_threads([client_thread, server_thread2])
} }

View File

@ -19,7 +19,7 @@ class TestOpenURISSL
end end
def with_https(log_is_empty=true) def with_https(log_is_empty=true)
log = StringIO.new('') log = []
logger = WEBrick::Log.new(log, WEBrick::BasicLog::WARN) logger = WEBrick::Log.new(log, WEBrick::BasicLog::WARN)
Dir.mktmpdir {|dr| Dir.mktmpdir {|dr|
srv = WEBrick::HTTPServer.new({ srv = WEBrick::HTTPServer.new({
@ -42,7 +42,7 @@ class TestOpenURISSL
th.join th.join
end end
if log_is_empty if log_is_empty
assert_equal("", log.string) assert_equal([], log)
end end
} }
end end
@ -96,14 +96,15 @@ class TestOpenURISSL
} }
server_thread2 = Thread.new { server_thread2 = Thread.new {
server_thread.join server_thread.join
assert_match(/ERROR OpenSSL::SSL::SSLError:/, server_log.string) assert_equal(1, server_log.length)
assert_match(/ERROR OpenSSL::SSL::SSLError:/, server_log[0])
} }
assert_join_threads([client_thread, server_thread2]) assert_join_threads([client_thread, server_thread2])
} }
end end
def with_https_proxy def with_https_proxy
proxy_log = StringIO.new('') proxy_log = []
proxy_logger = WEBrick::Log.new(proxy_log, WEBrick::BasicLog::WARN) 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|
srv.mount_proc("/proxy", lambda { |req, res| res.body = "proxy" } ) srv.mount_proc("/proxy", lambda { |req, res| res.body = "proxy" } )
@ -116,7 +117,7 @@ class TestOpenURISSL
proxy = WEBrick::HTTPProxyServer.new({ proxy = WEBrick::HTTPProxyServer.new({
:ServerType => Thread, :ServerType => Thread,
:Logger => proxy_logger, :Logger => proxy_logger,
:AccessLog => [[proxy_access_log=StringIO.new, WEBrick::AccessLog::COMMON_LOG_FORMAT]], :AccessLog => [[proxy_access_log=[], WEBrick::AccessLog::COMMON_LOG_FORMAT]],
:BindAddress => '127.0.0.1', :BindAddress => '127.0.0.1',
:Port => 0}) :Port => 0})
_, proxy_port, _, proxy_host = proxy.listeners[0].addr _, proxy_port, _, proxy_host = proxy.listeners[0].addr
@ -128,7 +129,7 @@ class TestOpenURISSL
proxy_thread.join proxy_thread.join
end end
} }
assert_equal('', proxy_log.string) assert_equal([], proxy_log)
end end
def test_proxy_cacert_file def test_proxy_cacert_file
@ -146,8 +147,9 @@ class TestOpenURISSL
} }
proxy_thread2 = Thread.new { proxy_thread2 = Thread.new {
proxy_thread.join proxy_thread.join
assert_match(%r[CONNECT #{url.sub(%r{\Ahttps://}, '')} ], proxy_access_log.string) assert_equal(1, proxy_access_log.length)
assert_equal('', proxy_log.string) assert_match(%r[CONNECT #{url.sub(%r{\Ahttps://}, '')} ], proxy_access_log[0])
assert_equal([], proxy_log)
} }
assert_join_threads([client_thread, proxy_thread2, server_thread]) assert_join_threads([client_thread, proxy_thread2, server_thread])
} }
@ -168,8 +170,9 @@ class TestOpenURISSL
} }
proxy_thread2 = Thread.new { proxy_thread2 = Thread.new {
proxy_thread.join proxy_thread.join
assert_match(%r[CONNECT #{url.sub(%r{\Ahttps://}, '')} ], proxy_access_log.string) assert_equal(1, proxy_access_log.length)
assert_equal('', proxy_log.string) assert_match(%r[CONNECT #{url.sub(%r{\Ahttps://}, '')} ], proxy_access_log[0])
assert_equal([], proxy_log)
} }
assert_join_threads([client_thread, proxy_thread2, server_thread]) assert_join_threads([client_thread, proxy_thread2, server_thread])
} }

View File

@ -61,7 +61,6 @@ class TestWEBrickHTTPAuth < Test::Unit::TestCase
http.request(g){|res| assert_not_equal("hoge", res.body, log.call)} http.request(g){|res| assert_not_equal("hoge", res.body, log.call)}
} }
} }
log = log.lines.to_a
log.reject! {|line| /\A\s*\z/ =~ line } log.reject! {|line| /\A\s*\z/ =~ line }
pats = [ pats = [
/ERROR Basic WEBrick's realm: webrick: password unmatch\./, /ERROR Basic WEBrick's realm: webrick: password unmatch\./,
@ -154,7 +153,6 @@ class TestWEBrickHTTPAuth < Test::Unit::TestCase
end end
} }
} }
log = log.lines.to_a
log.reject! {|line| /\A\s*\z/ =~ line } log.reject! {|line| /\A\s*\z/ =~ line }
pats = [ pats = [
/ERROR Digest WEBrick's realm: no credentials in the request\./, /ERROR Digest WEBrick's realm: no credentials in the request\./,

View File

@ -26,7 +26,7 @@ class TestWEBrickServer < Test::Unit::TestCase
def test_start_exception def test_start_exception
stopped = 0 stopped = 0
log = StringIO.new('') log = []
logger = WEBrick::Log.new(log, WEBrick::BasicLog::WARN) logger = WEBrick::Log.new(log, WEBrick::BasicLog::WARN)
assert_raises(SignalException) do assert_raises(SignalException) do
@ -47,7 +47,8 @@ class TestWEBrickServer < Test::Unit::TestCase
end end
assert_equal(stopped, 1) assert_equal(stopped, 1)
assert_match(/FATAL SignalException: SIGTERM/, log.string) assert_equal(1, log.length)
assert_match(/FATAL SignalException: SIGTERM/, log[0])
end end
def test_callbacks def test_callbacks

View File

@ -32,17 +32,13 @@ module TestWEBrick
module_function module_function
def start_server(klass, config={}, &block) def start_server(klass, config={}, &block)
log_string = "" log_ary = []
logger = Object.new log = proc { "webrick log start:\n" + log_ary.join.gsub(/^/, " ").chomp + "\nwebrick log end" }
logger.instance_eval do
define_singleton_method(:<<) {|msg| log_string << msg }
end
log = proc { "webrick log start:\n" + log_string.gsub(/^/, " ").chomp + "\nwebrick log end" }
server = klass.new({ server = klass.new({
:BindAddress => "127.0.0.1", :Port => 0, :BindAddress => "127.0.0.1", :Port => 0,
:ServerType => Thread, :ServerType => Thread,
:Logger => WEBrick::Log.new(logger, WEBrick::BasicLog::WARN), :Logger => WEBrick::Log.new(log_ary, WEBrick::BasicLog::WARN),
:AccessLog => [[logger, ""]] :AccessLog => [[log_ary, ""]]
}.update(config)) }.update(config))
server_thread = server.start server_thread = server.start
addr = server.listeners[0].addr addr = server.listeners[0].addr
@ -54,7 +50,7 @@ module TestWEBrick
end end
} }
assert_join_threads([client_thread, server_thread]) assert_join_threads([client_thread, server_thread])
log_string log_ary
end end
def start_httpserver(config={}, &block) def start_httpserver(config={}, &block)