From 070c310e87d04865da1b4f217432e22cfae5e380 Mon Sep 17 00:00:00 2001 From: akr Date: Sun, 9 Nov 2014 11:51:06 +0000 Subject: * 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 --- test/net/http/test_http.rb | 2 +- test/net/http/test_https.rb | 5 ++++- test/net/http/utils.rb | 6 +++--- test/open-uri/test_open-uri.rb | 13 ++++++++----- test/open-uri/test_ssl.rb | 23 +++++++++++++---------- test/webrick/test_httpauth.rb | 2 -- test/webrick/test_server.rb | 5 +++-- test/webrick/utils.rb | 14 +++++--------- 8 files changed, 37 insertions(+), 33 deletions(-) (limited to 'test') diff --git a/test/net/http/test_http.rb b/test/net/http/test_http.rb index bd0d960b96..90336b21ce 100644 --- a/test/net/http/test_http.rb +++ b/test/net/http/test_http.rb @@ -277,7 +277,7 @@ module TestNetHTTP_version_1_1_methods end } assert_equal 1, i - @log_pattern = nil # server may encount ECONNRESET + @log_tester = nil # server may encount ECONNRESET end def test_get__implicit_start diff --git a/test/net/http/test_https.rb b/test/net/http/test_https.rb index 89845c584a..bff2b4afe6 100644 --- a/test/net/http/test_https.rb +++ b/test/net/http/test_https.rb @@ -117,7 +117,10 @@ class TestNetHTTPS < Test::Unit::TestCase end } 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 def test_identity_verify_failure diff --git a/test/net/http/utils.rb b/test/net/http/utils.rb index 3bf7695532..dcd94693c2 100644 --- a/test/net/http/utils.rb +++ b/test/net/http/utils.rb @@ -36,14 +36,14 @@ module TestNetHTTPUtils @server.shutdown @server_thread.join end - assert_match(@log_pattern, @log.string) if @log_pattern + @log_tester.call(@log) if @log_tester # resume global state Net::HTTP.version_1_2 end def spawn_server - @log = StringIO.new('') - @log_pattern = /\A\z/ + @log = [] + @log_tester = lambda {|log| assert_equal([], log ) } @config = self.class::CONFIG server_config = { :BindAddress => config('host'), diff --git a/test/open-uri/test_open-uri.rb b/test/open-uri/test_open-uri.rb index 816d7fa3e2..d2d8840522 100644 --- a/test/open-uri/test_open-uri.rb +++ b/test/open-uri/test_open-uri.rb @@ -14,7 +14,7 @@ class TestOpenURI < Test::Unit::TestCase end def with_http(log_is_empty=true) - log = StringIO.new('') + log = [] logger = WEBrick::Log.new(log, WEBrick::BasicLog::WARN) Dir.mktmpdir {|dr| srv = WEBrick::HTTPServer.new({ @@ -34,7 +34,7 @@ class TestOpenURI < Test::Unit::TestCase end } if log_is_empty - assert_equal("", log.string) + assert_equal([], log) end end @@ -92,7 +92,8 @@ class TestOpenURI < Test::Unit::TestCase } server_thread2 = Thread.new { 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]) } @@ -486,7 +487,8 @@ class TestOpenURI < Test::Unit::TestCase } server_thread2 = Thread.new { 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]) } @@ -505,7 +507,8 @@ class TestOpenURI < Test::Unit::TestCase } server_thread2 = Thread.new { 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]) } diff --git a/test/open-uri/test_ssl.rb b/test/open-uri/test_ssl.rb index baffd1cda6..154bcf4ea7 100644 --- a/test/open-uri/test_ssl.rb +++ b/test/open-uri/test_ssl.rb @@ -19,7 +19,7 @@ class TestOpenURISSL end def with_https(log_is_empty=true) - log = StringIO.new('') + log = [] logger = WEBrick::Log.new(log, WEBrick::BasicLog::WARN) Dir.mktmpdir {|dr| srv = WEBrick::HTTPServer.new({ @@ -42,7 +42,7 @@ class TestOpenURISSL th.join end if log_is_empty - assert_equal("", log.string) + assert_equal([], log) end } end @@ -96,14 +96,15 @@ class TestOpenURISSL } server_thread2 = Thread.new { 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]) } end def with_https_proxy - proxy_log = StringIO.new('') + proxy_log = [] proxy_logger = WEBrick::Log.new(proxy_log, WEBrick::BasicLog::WARN) with_https {|srv, dr, url, server_thread, server_log| srv.mount_proc("/proxy", lambda { |req, res| res.body = "proxy" } ) @@ -116,7 +117,7 @@ class TestOpenURISSL proxy = WEBrick::HTTPProxyServer.new({ :ServerType => Thread, :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', :Port => 0}) _, proxy_port, _, proxy_host = proxy.listeners[0].addr @@ -128,7 +129,7 @@ class TestOpenURISSL proxy_thread.join end } - assert_equal('', proxy_log.string) + assert_equal([], proxy_log) end def test_proxy_cacert_file @@ -146,8 +147,9 @@ class TestOpenURISSL } proxy_thread2 = Thread.new { proxy_thread.join - assert_match(%r[CONNECT #{url.sub(%r{\Ahttps://}, '')} ], proxy_access_log.string) - assert_equal('', proxy_log.string) + assert_equal(1, proxy_access_log.length) + 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]) } @@ -168,8 +170,9 @@ class TestOpenURISSL } proxy_thread2 = Thread.new { proxy_thread.join - assert_match(%r[CONNECT #{url.sub(%r{\Ahttps://}, '')} ], proxy_access_log.string) - assert_equal('', proxy_log.string) + assert_equal(1, proxy_access_log.length) + 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]) } diff --git a/test/webrick/test_httpauth.rb b/test/webrick/test_httpauth.rb index bf47656a20..051e252fec 100644 --- a/test/webrick/test_httpauth.rb +++ b/test/webrick/test_httpauth.rb @@ -61,7 +61,6 @@ class TestWEBrickHTTPAuth < Test::Unit::TestCase http.request(g){|res| assert_not_equal("hoge", res.body, log.call)} } } - log = log.lines.to_a log.reject! {|line| /\A\s*\z/ =~ line } pats = [ /ERROR Basic WEBrick's realm: webrick: password unmatch\./, @@ -154,7 +153,6 @@ class TestWEBrickHTTPAuth < Test::Unit::TestCase end } } - log = log.lines.to_a log.reject! {|line| /\A\s*\z/ =~ line } pats = [ /ERROR Digest WEBrick's realm: no credentials in the request\./, diff --git a/test/webrick/test_server.rb b/test/webrick/test_server.rb index 40afdb050e..e46494a132 100644 --- a/test/webrick/test_server.rb +++ b/test/webrick/test_server.rb @@ -26,7 +26,7 @@ class TestWEBrickServer < Test::Unit::TestCase def test_start_exception stopped = 0 - log = StringIO.new('') + log = [] logger = WEBrick::Log.new(log, WEBrick::BasicLog::WARN) assert_raises(SignalException) do @@ -47,7 +47,8 @@ class TestWEBrickServer < Test::Unit::TestCase end assert_equal(stopped, 1) - assert_match(/FATAL SignalException: SIGTERM/, log.string) + assert_equal(1, log.length) + assert_match(/FATAL SignalException: SIGTERM/, log[0]) end def test_callbacks diff --git a/test/webrick/utils.rb b/test/webrick/utils.rb index b15ef9a408..e1c2344fb1 100644 --- a/test/webrick/utils.rb +++ b/test/webrick/utils.rb @@ -32,17 +32,13 @@ module TestWEBrick module_function def start_server(klass, config={}, &block) - log_string = "" - logger = Object.new - 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" } + log_ary = [] + log = proc { "webrick log start:\n" + log_ary.join.gsub(/^/, " ").chomp + "\nwebrick log end" } server = klass.new({ :BindAddress => "127.0.0.1", :Port => 0, :ServerType => Thread, - :Logger => WEBrick::Log.new(logger, WEBrick::BasicLog::WARN), - :AccessLog => [[logger, ""]] + :Logger => WEBrick::Log.new(log_ary, WEBrick::BasicLog::WARN), + :AccessLog => [[log_ary, ""]] }.update(config)) server_thread = server.start addr = server.listeners[0].addr @@ -54,7 +50,7 @@ module TestWEBrick end } assert_join_threads([client_thread, server_thread]) - log_string + log_ary end def start_httpserver(config={}, &block) -- cgit v1.2.3