From ad58f04833b6d9f6b0d01c5fc8adc61df6d088cc Mon Sep 17 00:00:00 2001 From: akr Date: Sun, 9 Nov 2014 14:01:20 +0000 Subject: * 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 --- test/webrick/test_cgi.rb | 21 +++++++++++---- test/webrick/test_filehandler.rb | 27 ++++++++++++++++--- test/webrick/test_httpauth.rb | 56 +++++++++++++++++++++++----------------- test/webrick/test_httpserver.rb | 19 +++++++++++--- test/webrick/utils.rb | 26 ++++++++++++------- 5 files changed, 104 insertions(+), 45 deletions(-) (limited to 'test/webrick') diff --git a/test/webrick/test_cgi.rb b/test/webrick/test_cgi.rb index 72de8a0ca6..5507dfec41 100644 --- a/test/webrick/test_cgi.rb +++ b/test/webrick/test_cgi.rb @@ -6,7 +6,7 @@ require "test/unit" class TestWEBrickCGI < Test::Unit::TestCase CRLF = "\r\n" - def start_cgi_server(&block) + def start_cgi_server(log_tester=TestWEBrick::DefaultLogTester, &block) config = { :CGIInterpreter => TestWEBrick::RubyBin, :DocumentRoot => File.dirname(__FILE__), @@ -23,7 +23,7 @@ class TestWEBrickCGI < Test::Unit::TestCase if RUBY_PLATFORM =~ /mswin|mingw|cygwin|bccwin32/ config[:CGIPathEnv] = ENV['PATH'] # runtime dll may not be in system dir. end - TestWEBrick.start_httpserver(config){|server, addr, port, log| + TestWEBrick.start_httpserver(config, log_tester){|server, addr, port, log| block.call(server, addr, port, log) } end @@ -90,7 +90,10 @@ class TestWEBrickCGI < Test::Unit::TestCase end def test_bad_request - start_cgi_server{|server, addr, port, log| + log_tester = lambda {|log, access_log| + assert_match(/BadRequest/, log.join) + } + start_cgi_server(log_tester) {|server, addr, port, log| sock = TCPSocket.new(addr, port) begin sock << "POST /webrick.cgi HTTP/1.0" << CRLF @@ -111,7 +114,11 @@ class TestWEBrickCGI < Test::Unit::TestCase DumpPat = /#{Regexp.quote(CtrlSeq.dump[1...-1])}/o def test_bad_uri - start_cgi_server{|server, addr, port, log| + log_tester = lambda {|log, access_log| + assert_equal(1, log.length) + assert_match(/ERROR bad URI/, log[0]) + } + start_cgi_server(log_tester) {|server, addr, port, log| res = TCPSocket.open(addr, port) {|sock| sock << "GET /#{CtrlSeq}#{CRLF}#{CRLF}" sock.close_write @@ -125,7 +132,11 @@ class TestWEBrickCGI < Test::Unit::TestCase end def test_bad_header - start_cgi_server{|server, addr, port, log| + log_tester = lambda {|log, access_log| + assert_equal(1, log.length) + assert_match(/ERROR bad header/, log[0]) + } + start_cgi_server(log_tester) {|server, addr, port, log| res = TCPSocket.open(addr, port) {|sock| sock << "GET / HTTP/1.0#{CRLF}#{CtrlSeq}#{CRLF}#{CRLF}" sock.close_write diff --git a/test/webrick/test_filehandler.rb b/test/webrick/test_filehandler.rb index 9e50664233..f984efb3ac 100644 --- a/test/webrick/test_filehandler.rb +++ b/test/webrick/test_filehandler.rb @@ -165,8 +165,13 @@ class WEBrick::TestFileHandler < Test::Unit::TestCase def test_non_disclosure_name config = { :DocumentRoot => File.dirname(__FILE__), } + log_tester = lambda {|log, access_log| + log = log.reject {|s| /ERROR `.*' not found\./ =~ s } + log = log.reject {|s| /WARN the request refers nondisclosure name/ =~ s } + assert_equal([], log) + } this_file = File.basename(__FILE__) - TestWEBrick.start_httpserver(config) do |server, addr, port, log| + TestWEBrick.start_httpserver(config, log_tester) do |server, addr, port, log| http = Net::HTTP.new(addr, port) doc_root_opts = server[:DocumentRootOptions] doc_root_opts[:NondisclosureName] = %w(.ht* *~ test_*) @@ -189,7 +194,12 @@ class WEBrick::TestFileHandler < Test::Unit::TestCase def test_directory_traversal config = { :DocumentRoot => File.dirname(__FILE__), } - TestWEBrick.start_httpserver(config) do |server, addr, port, log| + log_tester = lambda {|log, access_log| + log = log.reject {|s| /ERROR bad URI/ =~ s } + log = log.reject {|s| /ERROR `.*' not found\./ =~ s } + assert_equal([], log) + } + TestWEBrick.start_httpserver(config, log_tester) do |server, addr, port, log| http = Net::HTTP.new(addr, port) req = Net::HTTP::Get.new("/../../") http.request(req){|res| assert_equal("400", res.code, log.call) } @@ -217,7 +227,12 @@ class WEBrick::TestFileHandler < Test::Unit::TestCase :DocumentRoot => File.dirname(__FILE__), :CGIPathEnv => ENV['PATH'], } - TestWEBrick.start_httpserver(config) do |server, addr, port, log| + log_tester = lambda {|log, access_log| + log = log.reject {|s| /ERROR `.*' not found\./ =~ s } + log = log.reject {|s| /WARN the request refers nondisclosure name/ =~ s } + assert_equal([], log) + } + TestWEBrick.start_httpserver(config, log_tester) do |server, addr, port, log| http = Net::HTTP.new(addr, port) if windows? fname = nil @@ -260,7 +275,11 @@ class WEBrick::TestFileHandler < Test::Unit::TestCase end }, } - TestWEBrick.start_httpserver(config) do |server, addr, port, log| + log_tester = lambda {|log, access_log| + log = log.reject {|s| /ERROR `.*' not found\./ =~ s } + assert_equal([], log) + } + TestWEBrick.start_httpserver(config, log_tester) do |server, addr, port, log| http = Net::HTTP.new(addr, port) req = Net::HTTP::Get.new("/webrick.cgi/test") diff --git a/test/webrick/test_httpauth.rb b/test/webrick/test_httpauth.rb index 051e252fec..27c37f3677 100644 --- a/test/webrick/test_httpauth.rb +++ b/test/webrick/test_httpauth.rb @@ -7,7 +7,11 @@ require_relative "utils" class TestWEBrickHTTPAuth < Test::Unit::TestCase def test_basic_auth - TestWEBrick.start_httpserver{|server, addr, port, log| + log_tester = lambda {|log, access_log| + assert_equal(1, log.length) + assert_match(/ERROR WEBrick::HTTPStatus::Unauthorized/, log[0]) + } + TestWEBrick.start_httpserver({}, log_tester) {|server, addr, port, log| realm = "WEBrick's realm" path = "/basic_auth" @@ -27,7 +31,19 @@ class TestWEBrickHTTPAuth < Test::Unit::TestCase end def test_basic_auth2 - log = TestWEBrick.start_httpserver{|server, addr, port, log| + log_tester = lambda {|log, access_log| + log.reject! {|line| /\A\s*\z/ =~ line } + pats = [ + /ERROR Basic WEBrick's realm: webrick: password unmatch\./, + /ERROR WEBrick::HTTPStatus::Unauthorized/ + ] + pats.each {|pat| + assert(!log.grep(pat).empty?, "webrick log doesn't have expected error: #{pat.inspect}") + log.reject! {|line| pat =~ line } + } + assert_equal([], log) + } + TestWEBrick.start_httpserver({}, log_tester) {|server, addr, port, log| realm = "WEBrick's realm" path = "/basic_auth2" @@ -61,16 +77,6 @@ class TestWEBrickHTTPAuth < Test::Unit::TestCase http.request(g){|res| assert_not_equal("hoge", res.body, log.call)} } } - log.reject! {|line| /\A\s*\z/ =~ line } - pats = [ - /ERROR Basic WEBrick's realm: webrick: password unmatch\./, - /ERROR WEBrick::HTTPStatus::Unauthorized/ - ] - pats.each {|pat| - assert(!log.grep(pat).empty?, "webrick log doesn't have expected error: #{pat.inspect}") - log.reject! {|line| pat =~ line } - } - assert_equal([], log) end def test_basic_auth3 @@ -102,7 +108,20 @@ class TestWEBrickHTTPAuth < Test::Unit::TestCase )/x def test_digest_auth - log = TestWEBrick.start_httpserver{|server, addr, port, log| + log_tester = lambda {|log, access_log| + log.reject! {|line| /\A\s*\z/ =~ line } + pats = [ + /ERROR Digest WEBrick's realm: no credentials in the request\./, + /ERROR WEBrick::HTTPStatus::Unauthorized/, + /ERROR Digest WEBrick's realm: webrick: digest unmatch\./ + ] + pats.each {|pat| + assert(!log.grep(pat).empty?, "webrick log doesn't have expected error: #{pat.inspect}") + log.reject! {|line| pat =~ line } + } + assert_equal([], log) + } + TestWEBrick.start_httpserver({}, log_tester) {|server, addr, port, log| realm = "WEBrick's realm" path = "/digest_auth" @@ -153,17 +172,6 @@ class TestWEBrickHTTPAuth < Test::Unit::TestCase end } } - log.reject! {|line| /\A\s*\z/ =~ line } - pats = [ - /ERROR Digest WEBrick's realm: no credentials in the request\./, - /ERROR WEBrick::HTTPStatus::Unauthorized/, - /ERROR Digest WEBrick's realm: webrick: digest unmatch\./ - ] - pats.each {|pat| - assert(!log.grep(pat).empty?, "webrick log doesn't have expected error: #{pat.inspect}") - log.reject! {|line| pat =~ line } - } - assert_equal([], log) end private diff --git a/test/webrick/test_httpserver.rb b/test/webrick/test_httpserver.rb index eae77ec9ba..5cd4ba5866 100644 --- a/test/webrick/test_httpserver.rb +++ b/test/webrick/test_httpserver.rb @@ -230,7 +230,11 @@ class TestWEBrickHTTPServer < Test::Unit::TestCase :StopCallback => Proc.new{ stopped += 1 }, :RequestCallback => Proc.new{|req, res| requested0 += 1 }, } - TestWEBrick.start_httpserver(config){|server, addr, port, log| + log_tester = lambda {|log, access_log| + assert(log.find {|s| %r{ERROR `/' not found\.} =~ s }) + assert_equal([], log.reject {|s| %r{ERROR `/' not found\.} =~ s }) + } + TestWEBrick.start_httpserver(config, log_tester){|server, addr, port, log| vhost_config = { :ServerName => "myhostname", :BindAddress => addr, @@ -333,7 +337,11 @@ class TestWEBrickHTTPServer < Test::Unit::TestCase config = { :ServerName => "localhost" } - TestWEBrick.start_httpserver(config){|server, addr, port, log| + log_tester = lambda {|log, access_log| + assert_equal(1, log.length) + assert_match(/WARN Could not determine content-length of response body./, log[0]) + } + TestWEBrick.start_httpserver(config, log_tester){|server, addr, port, log| server.mount_proc("/", lambda { |req, res| r,w = IO.pipe # Test for not setting chunked... @@ -362,7 +370,12 @@ class TestWEBrickHTTPServer < Test::Unit::TestCase :ServerName => "localhost", :RequestHandler => Proc.new{|req, res| requested += 1 }, } - TestWEBrick.start_httpserver(config){|server, addr, port, log| + log_tester = lambda {|log, access_log| + assert_equal(2, log.length) + assert_match(/WARN :RequestHandler is deprecated, please use :RequestCallback/, log[0]) + assert_match(%r{ERROR `/' not found\.}, log[1]) + } + TestWEBrick.start_httpserver(config, log_tester){|server, addr, port, log| Thread.pass while server.status != :Running http = Net::HTTP.new(addr, port) diff --git a/test/webrick/utils.rb b/test/webrick/utils.rb index e1c2344fb1..0e94ad34da 100644 --- a/test/webrick/utils.rb +++ b/test/webrick/utils.rb @@ -31,16 +31,25 @@ module TestWEBrick module_function - def start_server(klass, config={}, &block) + DefaultLogTester = lambda {|log, access_log| assert_equal([], log) } + + def start_server(klass, config={}, log_tester=DefaultLogTester, &block) log_ary = [] - log = proc { "webrick log start:\n" + log_ary.join.gsub(/^/, " ").chomp + "\nwebrick log end" } + access_log_ary = [] + log = proc { "webrick log start:\n" + (log_ary+access_log_ary).join.gsub(/^/, " ").chomp + "\nwebrick log end" } server = klass.new({ :BindAddress => "127.0.0.1", :Port => 0, :ServerType => Thread, :Logger => WEBrick::Log.new(log_ary, WEBrick::BasicLog::WARN), - :AccessLog => [[log_ary, ""]] + :AccessLog => [[access_log_ary, ""]] }.update(config)) server_thread = server.start + server_thread2 = Thread.new { + server_thread.join + if log_tester + log_tester.call(log_ary, access_log_ary) + end + } addr = server.listeners[0].addr client_thread = Thread.new { begin @@ -49,15 +58,14 @@ module TestWEBrick server.shutdown end } - assert_join_threads([client_thread, server_thread]) - log_ary + assert_join_threads([client_thread, server_thread2]) end - def start_httpserver(config={}, &block) - start_server(WEBrick::HTTPServer, config, &block) + def start_httpserver(config={}, log_tester=DefaultLogTester, &block) + start_server(WEBrick::HTTPServer, config, log_tester, &block) end - def start_httpproxy(config={}, &block) - start_server(WEBrick::HTTPProxyServer, config, &block) + def start_httpproxy(config={}, log_tester=DefaultLogTester, &block) + start_server(WEBrick::HTTPProxyServer, config, log_tester, &block) end end -- cgit v1.2.3