2009-09-12 21:00:51 +04:00
|
|
|
require_relative '../ruby/envutil'
|
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
|
|
|
|
|
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}\""
|
|
|
|
|
2005-01-07 14:05:22 +03:00
|
|
|
module_function
|
|
|
|
|
|
|
|
def start_server(klass, config={}, &block)
|
2008-10-29 14:48:35 +03: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 14:05:22 +03:00
|
|
|
server = klass.new({
|
|
|
|
:BindAddress => "127.0.0.1", :Port => 0,
|
2007-12-31 17:17:41 +03:00
|
|
|
:ShutdownSocketWithoutClose =>true,
|
|
|
|
:ServerType => Thread,
|
2008-10-29 14:48:35 +03:00
|
|
|
:Logger => WEBrick::Log.new(logger),
|
|
|
|
:AccessLog => [[logger, ""]]
|
2005-01-07 14:05:22 +03:00
|
|
|
}.update(config))
|
|
|
|
begin
|
2012-04-12 00:28:11 +04:00
|
|
|
server_thread = server.start
|
2005-01-07 14:05:22 +03:00
|
|
|
addr = server.listeners[0].addr
|
2008-10-29 14:48:35 +03:00
|
|
|
block.yield([server, addr[3], addr[1], log])
|
2005-01-07 14:05:22 +03:00
|
|
|
ensure
|
2007-12-31 17:17:41 +03:00
|
|
|
server.shutdown
|
2012-04-12 00:28:11 +04:00
|
|
|
|
|
|
|
server_thread.join
|
2005-01-07 14:05:22 +03:00
|
|
|
end
|
2008-10-29 14:48:35 +03:00
|
|
|
log_string
|
2005-01-07 14:05:22 +03: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
|