2015-12-16 08:07:31 +03:00
|
|
|
# frozen_string_literal: false
|
2005-01-07 14:05:22 +03:00
|
|
|
require "webrick"
|
2005-01-10 10:58:55 +03:00
|
|
|
begin
|
|
|
|
require "webrick/https"
|
|
|
|
rescue LoadError
|
|
|
|
end
|
2005-01-07 14:05:22 +03:00
|
|
|
require "webrick/httpproxy"
|
|
|
|
|
|
|
|
module TestWEBrick
|
|
|
|
NullWriter = Object.new
|
|
|
|
def NullWriter.<<(msg)
|
|
|
|
puts msg if $DEBUG
|
|
|
|
return self
|
|
|
|
end
|
|
|
|
|
2013-04-18 17:56:09 +04:00
|
|
|
class WEBrick::HTTPServlet::CGIHandler
|
|
|
|
remove_const :Ruby
|
2017-04-25 12:23:04 +03:00
|
|
|
require "envutil" unless defined?(EnvUtil)
|
2013-04-18 17:56:09 +04:00
|
|
|
Ruby = EnvUtil.rubybin
|
|
|
|
remove_const :CGIRunner
|
|
|
|
CGIRunner = "\"#{Ruby}\" \"#{WEBrick::Config::LIBDIR}/httpservlet/cgi_runner.rb\"" # :nodoc:
|
|
|
|
end
|
|
|
|
|
2008-05-18 17:33:24 +04:00
|
|
|
RubyBin = "\"#{EnvUtil.rubybin}\""
|
2011-01-20 05:41:09 +03:00
|
|
|
RubyBin << " --disable-gems"
|
2008-05-18 17:33:24 +04:00
|
|
|
RubyBin << " \"-I#{File.expand_path("../..", File.dirname(__FILE__))}/lib\""
|
|
|
|
RubyBin << " \"-I#{File.dirname(EnvUtil.rubybin)}/.ext/common\""
|
|
|
|
RubyBin << " \"-I#{File.dirname(EnvUtil.rubybin)}/.ext/#{RUBY_PLATFORM}\""
|
|
|
|
|
2017-04-25 12:23:04 +03:00
|
|
|
require "test/unit" unless defined?(Test::Unit)
|
2014-11-08 18:38:33 +03:00
|
|
|
include Test::Unit::Assertions
|
|
|
|
extend Test::Unit::Assertions
|
|
|
|
|
2005-01-07 14:05:22 +03:00
|
|
|
module_function
|
|
|
|
|
2014-11-09 17:01:20 +03:00
|
|
|
DefaultLogTester = lambda {|log, access_log| assert_equal([], log) }
|
|
|
|
|
|
|
|
def start_server(klass, config={}, log_tester=DefaultLogTester, &block)
|
2014-11-09 14:51:06 +03:00
|
|
|
log_ary = []
|
2014-11-09 17:01:20 +03:00
|
|
|
access_log_ary = []
|
|
|
|
log = proc { "webrick log start:\n" + (log_ary+access_log_ary).join.gsub(/^/, " ").chomp + "\nwebrick log end" }
|
2016-05-09 03:05:32 +03:00
|
|
|
config = ({
|
2005-01-07 14:05:22 +03:00
|
|
|
:BindAddress => "127.0.0.1", :Port => 0,
|
2007-12-31 17:17:41 +03:00
|
|
|
:ServerType => Thread,
|
2014-11-09 14:51:06 +03:00
|
|
|
:Logger => WEBrick::Log.new(log_ary, WEBrick::BasicLog::WARN),
|
2014-11-09 17:01:20 +03:00
|
|
|
:AccessLog => [[access_log_ary, ""]]
|
2005-01-07 14:05:22 +03:00
|
|
|
}.update(config))
|
2016-05-09 03:05:32 +03:00
|
|
|
server = capture_io {break klass.new(config)}
|
2014-11-08 18:38:33 +03:00
|
|
|
server_thread = server.start
|
2014-11-09 17:01:20 +03:00
|
|
|
server_thread2 = Thread.new {
|
|
|
|
server_thread.join
|
|
|
|
if log_tester
|
|
|
|
log_tester.call(log_ary, access_log_ary)
|
|
|
|
end
|
|
|
|
}
|
2014-11-08 18:38:33 +03:00
|
|
|
addr = server.listeners[0].addr
|
|
|
|
client_thread = Thread.new {
|
|
|
|
begin
|
|
|
|
block.yield([server, addr[3], addr[1], log])
|
|
|
|
ensure
|
|
|
|
server.shutdown
|
|
|
|
end
|
|
|
|
}
|
2014-11-09 17:01:20 +03:00
|
|
|
assert_join_threads([client_thread, server_thread2])
|
2005-01-07 14:05:22 +03:00
|
|
|
end
|
|
|
|
|
2014-11-09 17:01:20 +03:00
|
|
|
def start_httpserver(config={}, log_tester=DefaultLogTester, &block)
|
|
|
|
start_server(WEBrick::HTTPServer, config, log_tester, &block)
|
2005-01-07 14:05:22 +03:00
|
|
|
end
|
|
|
|
|
2014-11-09 17:01:20 +03:00
|
|
|
def start_httpproxy(config={}, log_tester=DefaultLogTester, &block)
|
|
|
|
start_server(WEBrick::HTTPProxyServer, config, log_tester, &block)
|
2005-01-07 14:05:22 +03:00
|
|
|
end
|
|
|
|
end
|