1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* 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>
* lib/webrick/httpstatus.rb: require webrick/accesslog for AccessLog.

View file

@ -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

View file

@ -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
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)
}
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
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
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)
}
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

View file

@ -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

View file

@ -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")

View file

@ -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

View file

@ -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)

View file

@ -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