2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2007-12-18 09:46:52 -05:00
|
|
|
require 'webrick'
|
2007-12-20 11:21:22 -05:00
|
|
|
begin
|
|
|
|
require "webrick/https"
|
|
|
|
rescue LoadError
|
|
|
|
# SSL features cannot be tested
|
|
|
|
end
|
2007-12-18 09:46:52 -05:00
|
|
|
require 'webrick/httpservlet/abstract'
|
|
|
|
|
|
|
|
module TestNetHTTPUtils
|
|
|
|
def start(&block)
|
|
|
|
new().start(&block)
|
|
|
|
end
|
|
|
|
|
|
|
|
def new
|
|
|
|
klass = Net::HTTP::Proxy(config('proxy_host'), config('proxy_port'))
|
|
|
|
http = klass.new(config('host'), config('port'))
|
|
|
|
http.set_debug_output logfile()
|
|
|
|
http
|
|
|
|
end
|
|
|
|
|
|
|
|
def config(key)
|
2012-07-21 15:07:28 -04:00
|
|
|
@config ||= self.class::CONFIG
|
|
|
|
@config[key]
|
2007-12-18 09:46:52 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def logfile
|
|
|
|
$DEBUG ? $stderr : NullWriter.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def setup
|
|
|
|
spawn_server
|
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
2011-05-28 13:17:36 -04:00
|
|
|
if @server
|
|
|
|
@server.shutdown
|
2014-05-31 10:28:19 -04:00
|
|
|
@server_thread.join
|
2016-01-05 01:09:17 -05:00
|
|
|
WEBrick::Utils::TimeoutHandler.terminate
|
2007-12-18 09:46:52 -05:00
|
|
|
end
|
2014-11-09 06:51:06 -05:00
|
|
|
@log_tester.call(@log) if @log_tester
|
2007-12-18 09:46:52 -05:00
|
|
|
# resume global state
|
|
|
|
Net::HTTP.version_1_2
|
|
|
|
end
|
|
|
|
|
|
|
|
def spawn_server
|
2014-11-09 06:51:06 -05:00
|
|
|
@log = []
|
|
|
|
@log_tester = lambda {|log| assert_equal([], log ) }
|
2012-07-21 15:07:28 -04:00
|
|
|
@config = self.class::CONFIG
|
2007-12-20 11:21:22 -05:00
|
|
|
server_config = {
|
2007-12-18 09:46:52 -05:00
|
|
|
:BindAddress => config('host'),
|
2013-11-19 07:29:46 -05:00
|
|
|
:Port => 0,
|
2014-11-08 21:49:13 -05:00
|
|
|
:Logger => WEBrick::Log.new(@log, WEBrick::BasicLog::WARN),
|
2007-12-18 09:46:52 -05:00
|
|
|
:AccessLog => [],
|
2007-12-20 11:21:22 -05:00
|
|
|
:ServerType => Thread,
|
|
|
|
}
|
2010-05-03 19:42:26 -04:00
|
|
|
server_config[:OutputBufferSize] = 4 if config('chunked')
|
2012-01-20 00:56:35 -05:00
|
|
|
server_config[:RequestTimeout] = config('RequestTimeout') if config('RequestTimeout')
|
2007-12-20 11:21:22 -05:00
|
|
|
if defined?(OpenSSL) and config('ssl_enable')
|
|
|
|
server_config.update({
|
|
|
|
:SSLEnable => true,
|
|
|
|
:SSLCertificate => config('ssl_certificate'),
|
|
|
|
:SSLPrivateKey => config('ssl_private_key'),
|
2016-11-29 10:48:45 -05:00
|
|
|
:SSLTmpDhCallback => config('ssl_tmp_dh_callback'),
|
2007-12-20 11:21:22 -05:00
|
|
|
})
|
|
|
|
end
|
|
|
|
@server = WEBrick::HTTPServer.new(server_config)
|
2010-05-03 19:42:26 -04:00
|
|
|
@server.mount('/', Servlet, config('chunked'))
|
2014-05-31 10:28:19 -04:00
|
|
|
@server_thread = @server.start
|
2013-11-19 07:29:46 -05:00
|
|
|
@config['port'] = @server[:Port]
|
2007-12-18 09:46:52 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
$test_net_http = nil
|
|
|
|
$test_net_http_data = (0...256).to_a.map {|i| i.chr }.join('') * 64
|
2008-06-29 14:02:24 -04:00
|
|
|
$test_net_http_data.force_encoding("ASCII-8BIT")
|
2007-12-18 09:46:52 -05:00
|
|
|
$test_net_http_data_type = 'application/octet-stream'
|
|
|
|
|
|
|
|
class Servlet < WEBrick::HTTPServlet::AbstractServlet
|
2010-05-03 19:42:26 -04:00
|
|
|
def initialize(this, chunked = false)
|
|
|
|
@chunked = chunked
|
|
|
|
end
|
|
|
|
|
2007-12-18 09:46:52 -05:00
|
|
|
def do_GET(req, res)
|
2020-03-11 01:26:22 -04:00
|
|
|
if req['Accept'] != '*/*'
|
|
|
|
res['Content-Type'] = req['Accept']
|
|
|
|
else
|
|
|
|
res['Content-Type'] = $test_net_http_data_type
|
|
|
|
end
|
2007-12-18 09:46:52 -05:00
|
|
|
res.body = $test_net_http_data
|
2010-05-03 19:42:26 -04:00
|
|
|
res.chunked = @chunked
|
2007-12-18 09:46:52 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# echo server
|
|
|
|
def do_POST(req, res)
|
|
|
|
res['Content-Type'] = req['Content-Type']
|
2011-06-21 13:44:44 -04:00
|
|
|
res['X-request-uri'] = req.request_uri.to_s
|
2007-12-18 09:46:52 -05:00
|
|
|
res.body = req.body
|
2010-05-03 19:42:26 -04:00
|
|
|
res.chunked = @chunked
|
2007-12-18 09:46:52 -05:00
|
|
|
end
|
2010-06-01 08:30:52 -04:00
|
|
|
|
|
|
|
def do_PATCH(req, res)
|
|
|
|
res['Content-Type'] = req['Content-Type']
|
|
|
|
res.body = req.body
|
|
|
|
res.chunked = @chunked
|
|
|
|
end
|
2007-12-18 09:46:52 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
class NullWriter
|
|
|
|
def <<(s) end
|
|
|
|
def puts(*args) end
|
|
|
|
def print(*args) end
|
|
|
|
def printf(*args) end
|
|
|
|
end
|
2019-12-09 19:41:33 -05:00
|
|
|
|
|
|
|
def self.clean_http_proxy_env
|
|
|
|
orig = {
|
|
|
|
'http_proxy' => ENV['http_proxy'],
|
|
|
|
'http_proxy_user' => ENV['http_proxy_user'],
|
|
|
|
'http_proxy_pass' => ENV['http_proxy_pass'],
|
|
|
|
'no_proxy' => ENV['no_proxy'],
|
|
|
|
}
|
|
|
|
|
|
|
|
orig.each_key do |key|
|
|
|
|
ENV.delete key
|
|
|
|
end
|
|
|
|
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
orig.each do |key, value|
|
|
|
|
ENV[key] = value
|
|
|
|
end
|
|
|
|
end
|
2007-12-18 09:46:52 -05:00
|
|
|
end
|