2009-09-12 13:00:51 -04:00
|
|
|
require_relative '../ruby/envutil'
|
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
|
|
|
|
|
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}\""
|
|
|
|
|
2005-01-07 06:05:22 -05:00
|
|
|
module_function
|
|
|
|
|
|
|
|
def start_server(klass, config={}, &block)
|
2008-10-29 07:48:35 -04:00
|
|
|
log_string = ""
|
|
|
|
logger = Object.new
|
|
|
|
logger.instance_eval do
|
|
|
|
define_singleton_method(:<<) {|msg| log_string << msg }
|
|
|
|
end
|
|
|
|
log = proc { "webrick log start:\n" + log_string.gsub(/^/, " ").chomp + "\nwebrick log end" }
|
2005-01-07 06:05:22 -05:00
|
|
|
server = klass.new({
|
|
|
|
:BindAddress => "127.0.0.1", :Port => 0,
|
2007-12-31 09:17:41 -05:00
|
|
|
:ShutdownSocketWithoutClose =>true,
|
|
|
|
:ServerType => Thread,
|
2008-10-29 07:48:35 -04:00
|
|
|
:Logger => WEBrick::Log.new(logger),
|
|
|
|
:AccessLog => [[logger, ""]]
|
2005-01-07 06:05:22 -05:00
|
|
|
}.update(config))
|
|
|
|
begin
|
2012-04-11 16:28:11 -04:00
|
|
|
server_thread = server.start
|
2005-01-07 06:05:22 -05:00
|
|
|
addr = server.listeners[0].addr
|
2008-10-29 07:48:35 -04:00
|
|
|
block.yield([server, addr[3], addr[1], log])
|
2005-01-07 06:05:22 -05:00
|
|
|
ensure
|
2007-12-31 09:17:41 -05:00
|
|
|
server.shutdown
|
2012-04-11 16:28:11 -04:00
|
|
|
|
|
|
|
server_thread.join
|
2005-01-07 06:05:22 -05:00
|
|
|
end
|
2008-10-29 07:48:35 -04:00
|
|
|
log_string
|
2005-01-07 06:05:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def start_httpserver(config={}, &block)
|
|
|
|
start_server(WEBrick::HTTPServer, config, &block)
|
|
|
|
end
|
|
|
|
|
|
|
|
def start_httpproxy(config={}, &block)
|
|
|
|
start_server(WEBrick::HTTPProxyServer, config, &block)
|
|
|
|
end
|
|
|
|
end
|