* 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
This commit is contained in:
akr 2014-11-09 14:01:20 +00:00
parent 0d07bc203c
commit ad58f04833
8 changed files with 203 additions and 169 deletions

View File

@ -1,3 +1,9 @@
Sun Nov 9 22:46:13 2014 Tanaka Akira <akr@fsij.org>
* test/open-uri: Test server log in server thread.
* test/webrick: Ditto.
Sun Nov 9 22:28:34 2014 Tanaka Akira <akr@fsij.org> Sun Nov 9 22:28:34 2014 Tanaka Akira <akr@fsij.org>
* lib/webrick/httpstatus.rb: require webrick/accesslog for AccessLog. * lib/webrick/httpstatus.rb: require webrick/accesslog for AccessLog.

View File

@ -13,7 +13,7 @@ class TestOpenURI < Test::Unit::TestCase
def NullLog.<<(arg) def NullLog.<<(arg)
end end
def with_http(log_is_empty=true) def with_http(log_tester=lambda {|log| assert_equal([], log) })
log = [] log = []
logger = WEBrick::Log.new(log, WEBrick::BasicLog::WARN) logger = WEBrick::Log.new(log, WEBrick::BasicLog::WARN)
Dir.mktmpdir {|dr| Dir.mktmpdir {|dr|
@ -25,17 +25,22 @@ class TestOpenURI < Test::Unit::TestCase
:BindAddress => '127.0.0.1', :BindAddress => '127.0.0.1',
:Port => 0}) :Port => 0})
_, port, _, host = srv.listeners[0].addr _, port, _, host = srv.listeners[0].addr
begin server_thread = srv.start
th = srv.start server_thread2 = Thread.new {
yield srv, dr, "http://#{host}:#{port}", th, log server_thread.join
ensure if log_tester
srv.shutdown log_tester.call(log)
th.join
end end
} }
if log_is_empty client_thread = Thread.new {
assert_equal([], log) begin
yield srv, dr, "http://#{host}:#{port}", server_thread, log
ensure
srv.shutdown
end end
}
assert_join_threads([client_thread, server_thread2])
}
end end
def with_env(h) def with_env(h)
@ -81,21 +86,13 @@ class TestOpenURI < Test::Unit::TestCase
end end
def test_404 def test_404
with_http(false) {|srv, dr, url, server_thread, server_log| log_tester = lambda {|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_equal(1, server_log.length)
assert_match(%r{ERROR `/not-exist' not found}, server_log[0]) assert_match(%r{ERROR `/not-exist' not found}, server_log[0])
} }
assert_join_threads([client_thread, server_thread2]) 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 end
@ -475,42 +472,26 @@ class TestOpenURI < Test::Unit::TestCase
end end
def test_redirect_auth_failure_r2 def test_redirect_auth_failure_r2
with_http(false) {|srv, dr, url, server_thread, server_log| log_tester = lambda {|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_equal(1, server_log.length)
assert_match(/ERROR WEBrick::HTTPStatus::Unauthorized/, server_log[0]) assert_match(/ERROR WEBrick::HTTPStatus::Unauthorized/, server_log[0])
} }
assert_join_threads([client_thread, server_thread2]) with_http(log_tester) {|srv, dr, url, server_thread, server_log|
setup_redirect_auth(srv, url)
exc = assert_raise(OpenURI::HTTPError) { open("#{url}/r2/") {} }
assert_equal("401", exc.io.status[0])
} }
end end
def test_redirect_auth_failure_r1 def test_redirect_auth_failure_r1
with_http(false) {|srv, dr, url, server_thread, server_log| log_tester = lambda {|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_equal(1, server_log.length)
assert_match(/ERROR WEBrick::HTTPStatus::Unauthorized/, server_log[0]) assert_match(/ERROR WEBrick::HTTPStatus::Unauthorized/, server_log[0])
} }
assert_join_threads([client_thread, server_thread2]) with_http(log_tester) {|srv, dr, url, server_thread, server_log|
setup_redirect_auth(srv, url)
exc = assert_raise(OpenURI::HTTPError) { open("#{url}/r1/", :http_basic_authentication=>['user', 'pass']) {} }
assert_equal("401", exc.io.status[0])
} }
end end

View File

@ -18,7 +18,7 @@ class TestOpenURISSL
def NullLog.<<(arg) def NullLog.<<(arg)
end end
def with_https(log_is_empty=true) def with_https(log_tester=lambda {|log| assert_equal([], log) })
log = [] log = []
logger = WEBrick::Log.new(log, WEBrick::BasicLog::WARN) logger = WEBrick::Log.new(log, WEBrick::BasicLog::WARN)
Dir.mktmpdir {|dr| Dir.mktmpdir {|dr|
@ -34,17 +34,23 @@ class TestOpenURISSL
:BindAddress => '127.0.0.1', :BindAddress => '127.0.0.1',
:Port => 0}) :Port => 0})
_, port, _, host = srv.listeners[0].addr _, port, _, host = srv.listeners[0].addr
threads = []
server_thread = srv.start
threads << Thread.new {
server_thread.join
if log_tester
log_tester.call(log)
end
}
threads << Thread.new {
begin begin
th = srv.start yield srv, dr, "https://#{host}:#{port}", server_thread, log, threads
yield srv, dr, "https://#{host}:#{port}", th, log
ensure ensure
srv.shutdown srv.shutdown
th.join
end
if log_is_empty
assert_equal([], log)
end end
} }
assert_join_threads(threads)
}
end end
def setup def setup
@ -85,28 +91,20 @@ class TestOpenURISSL
end end
def test_validation_failure def test_validation_failure
with_https(false) {|srv, dr, url, server_thread, server_log| log_tester = lambda {|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_equal(1, server_log.length)
assert_match(/ERROR OpenSSL::SSL::SSLError:/, server_log[0]) assert_match(/ERROR OpenSSL::SSL::SSLError:/, server_log[0])
} }
assert_join_threads([client_thread, server_thread2]) with_https(log_tester) {|srv, dr, url, server_thread, server_log|
setup_validation(srv, dr)
assert_raise(OpenSSL::SSL::SSLError) { open("#{url}/data") {} }
} }
end 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_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, threads|
srv.mount_proc("/proxy", lambda { |req, res| res.body = "proxy" } ) srv.mount_proc("/proxy", lambda { |req, res| res.body = "proxy" } )
cacert_filename = "#{dr}/cacert.pem" cacert_filename = "#{dr}/cacert.pem"
open(cacert_filename, "w") {|f| f << CA_CERT } open(cacert_filename, "w") {|f| f << CA_CERT }
@ -121,60 +119,50 @@ class TestOpenURISSL
: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
begin
proxy_thread = proxy.start 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 threads << Thread.new {
ensure
proxy.shutdown
proxy_thread.join proxy_thread.join
if proxy_log_tester
proxy_log_tester.call(proxy_log, proxy_access_log)
end
}
begin
yield srv, dr, url, cacert_filename, cacert_directory, proxy_host, proxy_port
ensure
proxy.shutdown
end end
} }
assert_equal([], proxy_log)
end end
def test_proxy_cacert_file 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| url = nil
client_thread = Thread.new { proxy_log_tester = lambda {|proxy_log, proxy_access_log|
begin 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| open("#{url}/proxy", :proxy=>"http://#{proxy_host}:#{proxy_port}/", :ssl_ca_cert => cacert_filename) {|f|
assert_equal("200", f.status[0]) assert_equal("200", f.status[0])
assert_equal("proxy", f.read) 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)
}
assert_join_threads([client_thread, proxy_thread2, server_thread])
} }
end end
def test_proxy_cacert_dir 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| url = nil
client_thread = Thread.new { proxy_log_tester = lambda {|proxy_log, proxy_access_log|
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_equal(1, proxy_access_log.length)
assert_match(%r[CONNECT #{url.sub(%r{\Ahttps://}, '')} ], proxy_access_log[0]) assert_match(%r[CONNECT #{url.sub(%r{\Ahttps://}, '')} ], proxy_access_log[0])
assert_equal([], proxy_log) assert_equal([], proxy_log)
} }
assert_join_threads([client_thread, proxy_thread2, server_thread]) 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)
}
} }
end end

View File

@ -6,7 +6,7 @@ require "test/unit"
class TestWEBrickCGI < Test::Unit::TestCase class TestWEBrickCGI < Test::Unit::TestCase
CRLF = "\r\n" CRLF = "\r\n"
def start_cgi_server(&block) def start_cgi_server(log_tester=TestWEBrick::DefaultLogTester, &block)
config = { config = {
:CGIInterpreter => TestWEBrick::RubyBin, :CGIInterpreter => TestWEBrick::RubyBin,
:DocumentRoot => File.dirname(__FILE__), :DocumentRoot => File.dirname(__FILE__),
@ -23,7 +23,7 @@ class TestWEBrickCGI < Test::Unit::TestCase
if RUBY_PLATFORM =~ /mswin|mingw|cygwin|bccwin32/ if RUBY_PLATFORM =~ /mswin|mingw|cygwin|bccwin32/
config[:CGIPathEnv] = ENV['PATH'] # runtime dll may not be in system dir. config[:CGIPathEnv] = ENV['PATH'] # runtime dll may not be in system dir.
end 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) block.call(server, addr, port, log)
} }
end end
@ -90,7 +90,10 @@ class TestWEBrickCGI < Test::Unit::TestCase
end end
def test_bad_request 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) sock = TCPSocket.new(addr, port)
begin begin
sock << "POST /webrick.cgi HTTP/1.0" << CRLF 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 DumpPat = /#{Regexp.quote(CtrlSeq.dump[1...-1])}/o
def test_bad_uri 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| res = TCPSocket.open(addr, port) {|sock|
sock << "GET /#{CtrlSeq}#{CRLF}#{CRLF}" sock << "GET /#{CtrlSeq}#{CRLF}#{CRLF}"
sock.close_write sock.close_write
@ -125,7 +132,11 @@ class TestWEBrickCGI < Test::Unit::TestCase
end end
def test_bad_header 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| res = TCPSocket.open(addr, port) {|sock|
sock << "GET / HTTP/1.0#{CRLF}#{CtrlSeq}#{CRLF}#{CRLF}" sock << "GET / HTTP/1.0#{CRLF}#{CtrlSeq}#{CRLF}#{CRLF}"
sock.close_write sock.close_write

View File

@ -165,8 +165,13 @@ class WEBrick::TestFileHandler < Test::Unit::TestCase
def test_non_disclosure_name def test_non_disclosure_name
config = { :DocumentRoot => File.dirname(__FILE__), } 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__) 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) http = Net::HTTP.new(addr, port)
doc_root_opts = server[:DocumentRootOptions] doc_root_opts = server[:DocumentRootOptions]
doc_root_opts[:NondisclosureName] = %w(.ht* *~ test_*) doc_root_opts[:NondisclosureName] = %w(.ht* *~ test_*)
@ -189,7 +194,12 @@ class WEBrick::TestFileHandler < Test::Unit::TestCase
def test_directory_traversal def test_directory_traversal
config = { :DocumentRoot => File.dirname(__FILE__), } 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) http = Net::HTTP.new(addr, port)
req = Net::HTTP::Get.new("/../../") req = Net::HTTP::Get.new("/../../")
http.request(req){|res| assert_equal("400", res.code, log.call) } 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__), :DocumentRoot => File.dirname(__FILE__),
:CGIPathEnv => ENV['PATH'], :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) http = Net::HTTP.new(addr, port)
if windows? if windows?
fname = nil fname = nil
@ -260,7 +275,11 @@ class WEBrick::TestFileHandler < Test::Unit::TestCase
end 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) http = Net::HTTP.new(addr, port)
req = Net::HTTP::Get.new("/webrick.cgi/test") req = Net::HTTP::Get.new("/webrick.cgi/test")

View File

@ -7,7 +7,11 @@ require_relative "utils"
class TestWEBrickHTTPAuth < Test::Unit::TestCase class TestWEBrickHTTPAuth < Test::Unit::TestCase
def test_basic_auth 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" realm = "WEBrick's realm"
path = "/basic_auth" path = "/basic_auth"
@ -27,7 +31,19 @@ class TestWEBrickHTTPAuth < Test::Unit::TestCase
end end
def test_basic_auth2 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" realm = "WEBrick's realm"
path = "/basic_auth2" path = "/basic_auth2"
@ -61,16 +77,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.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 end
def test_basic_auth3 def test_basic_auth3
@ -102,7 +108,20 @@ class TestWEBrickHTTPAuth < Test::Unit::TestCase
)/x )/x
def test_digest_auth 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" realm = "WEBrick's realm"
path = "/digest_auth" path = "/digest_auth"
@ -153,17 +172,6 @@ class TestWEBrickHTTPAuth < Test::Unit::TestCase
end 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 end
private private

View File

@ -230,7 +230,11 @@ class TestWEBrickHTTPServer < Test::Unit::TestCase
:StopCallback => Proc.new{ stopped += 1 }, :StopCallback => Proc.new{ stopped += 1 },
:RequestCallback => Proc.new{|req, res| requested0 += 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 = { vhost_config = {
:ServerName => "myhostname", :ServerName => "myhostname",
:BindAddress => addr, :BindAddress => addr,
@ -333,7 +337,11 @@ class TestWEBrickHTTPServer < Test::Unit::TestCase
config = { config = {
:ServerName => "localhost" :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| server.mount_proc("/", lambda { |req, res|
r,w = IO.pipe r,w = IO.pipe
# Test for not setting chunked... # Test for not setting chunked...
@ -362,7 +370,12 @@ class TestWEBrickHTTPServer < Test::Unit::TestCase
:ServerName => "localhost", :ServerName => "localhost",
:RequestHandler => Proc.new{|req, res| requested += 1 }, :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 Thread.pass while server.status != :Running
http = Net::HTTP.new(addr, port) http = Net::HTTP.new(addr, port)

View File

@ -31,16 +31,25 @@ module TestWEBrick
module_function 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_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({ 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(log_ary, WEBrick::BasicLog::WARN), :Logger => WEBrick::Log.new(log_ary, WEBrick::BasicLog::WARN),
:AccessLog => [[log_ary, ""]] :AccessLog => [[access_log_ary, ""]]
}.update(config)) }.update(config))
server_thread = server.start 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 addr = server.listeners[0].addr
client_thread = Thread.new { client_thread = Thread.new {
begin begin
@ -49,15 +58,14 @@ module TestWEBrick
server.shutdown server.shutdown
end end
} }
assert_join_threads([client_thread, server_thread]) assert_join_threads([client_thread, server_thread2])
log_ary
end end
def start_httpserver(config={}, &block) def start_httpserver(config={}, log_tester=DefaultLogTester, &block)
start_server(WEBrick::HTTPServer, config, &block) start_server(WEBrick::HTTPServer, config, log_tester, &block)
end end
def start_httpproxy(config={}, &block) def start_httpproxy(config={}, log_tester=DefaultLogTester, &block)
start_server(WEBrick::HTTPProxyServer, config, &block) start_server(WEBrick::HTTPProxyServer, config, log_tester, &block)
end end
end end