aboutsummaryrefslogtreecommitdiffstats
path: root/test/net/http/utils.rb
diff options
context:
space:
mode:
Diffstat (limited to 'test/net/http/utils.rb')
-rw-r--r--test/net/http/utils.rb82
1 files changed, 82 insertions, 0 deletions
diff --git a/test/net/http/utils.rb b/test/net/http/utils.rb
new file mode 100644
index 0000000000..aec1967a74
--- /dev/null
+++ b/test/net/http/utils.rb
@@ -0,0 +1,82 @@
+require 'webrick'
+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)
+ self.class::CONFIG[key]
+ end
+
+ def logfile
+ $DEBUG ? $stderr : NullWriter.new
+ end
+
+ def setup
+ spawn_server
+ end
+
+ def teardown
+ @server.shutdown
+ until @server.status == :Stop
+ sleep 0.1
+ end
+ # resume global state
+ Net::HTTP.version_1_2
+ end
+
+ def spawn_server
+ @server = WEBrick::HTTPServer.new(
+ :BindAddress => config('host'),
+ :Port => config('port'),
+ :Logger => WEBrick::Log.new(NullWriter.new),
+ :AccessLog => [],
+ :ShutdownSocketWithoutClose => true,
+ :ServerType => Thread
+ )
+ @server.mount('/', Servlet)
+ @server.start
+ n_try_max = 5
+ begin
+ TCPSocket.open(config('host'), config('port')).close
+ rescue Errno::ECONNREFUSED
+ sleep 0.2
+ n_try_max -= 1
+ raise 'cannot spawn server; give up' if n_try_max < 0
+ retry
+ end
+ end
+
+ $test_net_http = nil
+ $test_net_http_data = (0...256).to_a.map {|i| i.chr }.join('') * 64
+ $test_net_http_data_type = 'application/octet-stream'
+
+ class Servlet < WEBrick::HTTPServlet::AbstractServlet
+ def do_GET(req, res)
+ res['Content-Type'] = $test_net_http_data_type
+ res.body = $test_net_http_data
+ end
+
+ # echo server
+ def do_POST(req, res)
+ res['Content-Type'] = req['Content-Type']
+ res.body = req.body
+ end
+ end
+
+ class NullWriter
+ def <<(s) end
+ def puts(*args) end
+ def print(*args) end
+ def printf(*args) end
+ end
+end