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