diff options
author | yugui <yugui@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2008-08-25 15:13:14 +0000 |
---|---|---|
committer | yugui <yugui@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2008-08-25 15:13:14 +0000 |
commit | d0233291bc8a5068e52c69c210e5979e5324b5bc (patch) | |
tree | 7d9459449c33792c63eeb7baa071e76352e0baab /trunk/test/net/http | |
parent | 0dc342de848a642ecce8db697b8fecd83a63e117 (diff) | |
parent | 72eaacaa15256ab95c3b52ea386f88586fb9da40 (diff) |
re-adding tag v1_9_0_4 as an alias of trunk@18848v1_9_0_4
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/tags/v1_9_0_4@18849 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'trunk/test/net/http')
-rw-r--r-- | trunk/test/net/http/test_http.rb | 320 | ||||
-rw-r--r-- | trunk/test/net/http/test_httpheader.rb | 317 | ||||
-rw-r--r-- | trunk/test/net/http/test_https.rb | 97 | ||||
-rw-r--r-- | trunk/test/net/http/test_https_proxy.rb | 37 | ||||
-rw-r--r-- | trunk/test/net/http/utils.rb | 96 |
5 files changed, 0 insertions, 867 deletions
diff --git a/trunk/test/net/http/test_http.rb b/trunk/test/net/http/test_http.rb deleted file mode 100644 index 50d1fe2eae..0000000000 --- a/trunk/test/net/http/test_http.rb +++ /dev/null @@ -1,320 +0,0 @@ -# $Id$ - -require 'test/unit' -require 'net/http' -require 'stringio' -require File.expand_path("utils", File.dirname(__FILE__)) - -module TestNetHTTP_version_1_1_methods - - def test_s_get - assert_equal $test_net_http_data, - Net::HTTP.get(config('host'), '/', config('port')) - end - - def test_head - start {|http| - res = http.head('/') - assert_kind_of Net::HTTPResponse, res - assert_equal $test_net_http_data_type, res['Content-Type'] - assert_equal $test_net_http_data.size, res['Content-Length'].to_i - } - end - - def test_get - start {|http| - _test_get__get http - _test_get__iter http - _test_get__chunked http - } - end - - def _test_get__get(http) - res, body = http.get('/') - assert_kind_of Net::HTTPResponse, res - assert_kind_of String, res.body - assert_kind_of String, body - assert_not_nil res['content-length'] - assert_equal $test_net_http_data.size, res['content-length'].to_i - assert_equal $test_net_http_data_type, res['Content-Type'] - assert_equal $test_net_http_data.size, body.size - assert_equal $test_net_http_data, body - assert_equal $test_net_http_data.size, res.body.size - assert_equal $test_net_http_data, res.body - end - - def _test_get__iter(http) - buf = '' - res, body = http.get('/') {|s| buf << s } - assert_kind_of Net::HTTPResponse, res - # assert_kind_of String, res.body - # assert_kind_of String, body - assert_not_nil res['content-length'] - assert_equal $test_net_http_data.size, res['content-length'].to_i - assert_equal $test_net_http_data_type, res['Content-Type'] - assert_equal $test_net_http_data.size, buf.size - assert_equal $test_net_http_data, buf - # assert_equal $test_net_http_data.size, res.body.size - # assert_equal $test_net_http_data, res.body - end - - def _test_get__chunked(http) - buf = '' - res, body = http.get('/') {|s| buf << s } - assert_kind_of Net::HTTPResponse, res - # assert_kind_of String, res.body - # assert_kind_of String, body - assert_not_nil res['content-length'] - assert_equal $test_net_http_data.size, res['content-length'].to_i - assert_equal $test_net_http_data_type, res['Content-Type'] - assert_equal $test_net_http_data.size, buf.size - assert_equal $test_net_http_data, buf - # assert_equal $test_net_http_data.size, res.body.size - # assert_equal $test_net_http_data, res.body - end - - def test_get__break - i = 0 - start {|http| - http.get('/') do |str| - i += 1 - break - end - } - assert_equal 1, i - end - - def test_get__implicit_start - res, body = new().get('/') - assert_kind_of Net::HTTPResponse, res - assert_kind_of String, body - assert_kind_of String, res.body - assert_not_nil res['content-length'] - assert_equal $test_net_http_data_type, res['Content-Type'] - assert_equal $test_net_http_data.size, res.body.size - assert_equal $test_net_http_data, res.body - end - - def test_get2 - start {|http| - http.get2('/') {|res| - assert_kind_of Net::HTTPResponse, res - assert_kind_of Net::HTTPResponse, res.header - assert_not_nil res['content-length'] - assert_equal $test_net_http_data_type, res['Content-Type'] - assert_kind_of String, res.body - assert_kind_of String, res.entity - assert_equal $test_net_http_data.size, res.body.size - assert_equal $test_net_http_data, res.body - assert_equal $test_net_http_data, res.entity - } - } - end - - def test_post - start {|http| - _test_post__base http - _test_post__file http - } - end - - def _test_post__base(http) - uheader = {} - uheader['Accept'] = 'application/octet-stream' - data = 'post data' - res, body = http.post('/', data) - assert_kind_of Net::HTTPResponse, res - assert_kind_of String, body - assert_kind_of String, res.body - assert_equal data, body - assert_equal data, res.body - assert_equal data, res.entity - end - - def _test_post__file(http) - data = 'post data' - f = StringIO.new - http.post('/', data, nil, f) - assert_equal data, f.string - end - - def test_s_post_form - res = Net::HTTP.post_form( - URI.parse("http://#{config('host')}:#{config('port')}/"), - "a" => "x") - assert_equal ["a=x"], res.body.split(/[;&]/).sort - - res = Net::HTTP.post_form( - URI.parse("http://#{config('host')}:#{config('port')}/"), - "a" => "x", - "b" => "y") - assert_equal ["a=x", "b=y"], res.body.split(/[;&]/).sort - - res = Net::HTTP.post_form( - URI.parse("http://#{config('host')}:#{config('port')}/"), - "a" => ["x1", "x2"], - "b" => "y") - assert_equal ["a=x1", "a=x2", "b=y"], res.body.split(/[;&]/).sort - end - -end - - -module TestNetHTTP_version_1_2_methods - - def test_request - start {|http| - _test_request__GET http - _test_request__file http - # _test_request__range http # WEBrick does not support Range: header. - _test_request__HEAD http - _test_request__POST http - _test_request__stream_body http - } - end - - def _test_request__GET(http) - req = Net::HTTP::Get.new('/') - http.request(req) {|res| - assert_kind_of Net::HTTPResponse, res - assert_kind_of String, res.body - assert_not_nil res['content-length'] - assert_equal $test_net_http_data.size, res['content-length'].to_i - assert_equal $test_net_http_data.size, res.body.size - assert_equal $test_net_http_data, res.body - } - end - - def _test_request__file(http) - req = Net::HTTP::Get.new('/') - http.request(req) {|res| - assert_kind_of Net::HTTPResponse, res - assert_not_nil res['content-length'] - assert_equal $test_net_http_data.size, res['content-length'].to_i - f = StringIO.new("".force_encoding("ASCII-8BIT")) - res.read_body f - assert_equal $test_net_http_data.bytesize, f.string.bytesize - assert_equal $test_net_http_data.encoding, f.string.encoding - assert_equal $test_net_http_data, f.string - } - end - - def _test_request__range(http) - req = Net::HTTP::Get.new('/') - req['range'] = 'bytes=0-5' - assert_equal $test_net_http_data[0,6], http.request(req).body - end - - def _test_request__HEAD(http) - req = Net::HTTP::Head.new('/') - http.request(req) {|res| - assert_kind_of Net::HTTPResponse, res - assert_not_nil res['content-length'] - assert_equal $test_net_http_data.size, res['content-length'].to_i - assert_nil res.body - } - end - - def _test_request__POST(http) - data = 'post data' - req = Net::HTTP::Post.new('/') - req['Accept'] = $test_net_http_data_type - http.request(req, data) {|res| - assert_kind_of Net::HTTPResponse, res - assert_equal data.size, res['content-length'].to_i - assert_kind_of String, res.body - assert_equal data, res.body - } - end - - def _test_request__stream_body(http) - req = Net::HTTP::Post.new('/') - data = $test_net_http_data - req.content_length = data.size - req.body_stream = StringIO.new(data) - res = http.request(req) - assert_kind_of Net::HTTPResponse, res - assert_kind_of String, res.body - assert_equal data.size, res.body.size - assert_equal data, res.body - end - - def test_send_request - start {|http| - _test_send_request__GET http - _test_send_request__POST http - } - end - - def _test_send_request__GET(http) - res = http.send_request('GET', '/') - assert_kind_of Net::HTTPResponse, res - assert_equal $test_net_http_data.size, res['content-length'].to_i - assert_kind_of String, res.body - assert_equal $test_net_http_data, res.body - end - - def _test_send_request__POST(http) - data = 'aaabbb cc ddddddddddd lkjoiu4j3qlkuoa' - res = http.send_request('POST', '/', data) - assert_kind_of Net::HTTPResponse, res - assert_kind_of String, res.body - assert_equal data.size, res.body.size - assert_equal data, res.body - end -end - -class TestNetHTTP_version_1_1 < Test::Unit::TestCase - CONFIG = { - 'host' => '127.0.0.1', - 'port' => 10081, - 'proxy_host' => nil, - 'proxy_port' => nil, - } - - include TestNetHTTPUtils - include TestNetHTTP_version_1_1_methods - - def new - Net::HTTP.version_1_1 - super - end -end - -class TestNetHTTP_v1_2 < Test::Unit::TestCase - CONFIG = { - 'host' => '127.0.0.1', - 'port' => 10081, - 'proxy_host' => nil, - 'proxy_port' => nil, - } - - include TestNetHTTPUtils - include TestNetHTTP_version_1_1_methods - include TestNetHTTP_version_1_2_methods - - def new - Net::HTTP.version_1_2 - super - end -end - -=begin -class TestNetHTTP_proxy < Test::Unit::TestCase - CONFIG = { - 'host' => '127.0.0.1', - 'port' => 10081, - 'proxy_host' => '127.0.0.1', - 'proxy_port' => 10082, - } - - include TestNetHTTPUtils - include TestNetHTTP_version_1_1_methods - include TestNetHTTP_version_1_2_methods - - def new - Net::HTTP.version_1_2 - super - end -end -=end diff --git a/trunk/test/net/http/test_httpheader.rb b/trunk/test/net/http/test_httpheader.rb deleted file mode 100644 index 3ba1b217c0..0000000000 --- a/trunk/test/net/http/test_httpheader.rb +++ /dev/null @@ -1,317 +0,0 @@ -require 'net/http' -require 'test/unit' - -class HTTPHeaderTest < Test::Unit::TestCase - - class C - include Net::HTTPHeader - def initialize - initialize_http_header({}) - end - attr_accessor :body - end - - def setup - @c = C.new - end - - def test_size - assert_equal 0, @c.size - @c['a'] = 'a' - assert_equal 1, @c.size - @c['b'] = 'b' - assert_equal 2, @c.size - @c['b'] = 'b' - assert_equal 2, @c.size - @c['c'] = 'c' - assert_equal 3, @c.size - end - - def test_ASET - @c['My-Header'] = 'test string' - @c['my-Header'] = 'test string' - @c['My-header'] = 'test string' - @c['my-header'] = 'test string' - @c['MY-HEADER'] = 'test string' - assert_equal 1, @c.size - - @c['AaA'] = 'aaa' - @c['aaA'] = 'aaa' - @c['AAa'] = 'aaa' - assert_equal 2, @c.length - end - - def test_AREF - @c['My-Header'] = 'test string' - assert_equal 'test string', @c['my-header'] - assert_equal 'test string', @c['MY-header'] - assert_equal 'test string', @c['my-HEADER'] - - @c['Next-Header'] = 'next string' - assert_equal 'next string', @c['next-header'] - end - - def test_add_field - @c.add_field 'My-Header', 'a' - assert_equal 'a', @c['My-Header'] - assert_equal ['a'], @c.get_fields('My-Header') - @c.add_field 'My-Header', 'b' - assert_equal 'a, b', @c['My-Header'] - assert_equal ['a', 'b'], @c.get_fields('My-Header') - @c.add_field 'My-Header', 'c' - assert_equal 'a, b, c', @c['My-Header'] - assert_equal ['a', 'b', 'c'], @c.get_fields('My-Header') - @c.add_field 'My-Header', 'd, d' - assert_equal 'a, b, c, d, d', @c['My-Header'] - assert_equal ['a', 'b', 'c', 'd, d'], @c.get_fields('My-Header') - end - - def test_get_fields - @c['My-Header'] = 'test string' - assert_equal ['test string'], @c.get_fields('my-header') - assert_equal ['test string'], @c.get_fields('My-header') - assert_equal ['test string'], @c.get_fields('my-Header') - - assert_nil @c.get_fields('not-found') - assert_nil @c.get_fields('Not-Found') - - @c.get_fields('my-header').push 'junk' - assert_equal ['test string'], @c.get_fields('my-header') - @c.get_fields('my-header').clear - assert_equal ['test string'], @c.get_fields('my-header') - end - - def test_delete - @c['My-Header'] = 'test' - assert_equal 'test', @c['My-Header'] - assert_nil @c['not-found'] - @c.delete 'My-Header' - assert_nil @c['My-Header'] - assert_nil @c['not-found'] - @c.delete 'My-Header' - @c.delete 'My-Header' - assert_nil @c['My-Header'] - assert_nil @c['not-found'] - end - - def test_each - @c['My-Header'] = 'test' - @c.each do |k, v| - assert_equal 'my-header', k - assert_equal 'test', v - end - @c.each do |k, v| - assert_equal 'my-header', k - assert_equal 'test', v - end - end - - def test_each_key - @c['My-Header'] = 'test' - @c.each_key do |k| - assert_equal 'my-header', k - end - @c.each_key do |k| - assert_equal 'my-header', k - end - end - - def test_each_value - @c['My-Header'] = 'test' - @c.each_value do |v| - assert_equal 'test', v - end - @c.each_value do |v| - assert_equal 'test', v - end - end - - def test_canonical_each - @c['my-header'] = ['a', 'b'] - @c.canonical_each do |k,v| - assert_equal 'My-Header', k - assert_equal 'a, b', v - end - end - - def test_each_capitalized - @c['my-header'] = ['a', 'b'] - @c.each_capitalized do |k,v| - assert_equal 'My-Header', k - assert_equal 'a, b', v - end - end - - def test_key? - @c['My-Header'] = 'test' - assert_equal true, @c.key?('My-Header') - assert_equal true, @c.key?('my-header') - assert_equal false, @c.key?('Not-Found') - assert_equal false, @c.key?('not-found') - assert_equal false, @c.key?('') - assert_equal false, @c.key?('x' * 1024) - end - - def test_to_hash - end - - def test_range - try_range(1..5, '1-5') - try_range(234..567, '234-567') - try_range(-5..-1, '-5') - try_range(1..-1, '1-') - end - - def try_range(r, s) - @c['range'] = "bytes=#{s}" - assert_equal r, Array(@c.range)[0] - end - - def test_range= - @c.range = 0..499 - assert_equal 'bytes=0-499', @c['range'] - @c.range = 0...500 - assert_equal 'bytes=0-499', @c['range'] - @c.range = 300 - assert_equal 'bytes=0-299', @c['range'] - @c.range = -400 - assert_equal 'bytes=-400', @c['range'] - @c.set_range 0, 500 - assert_equal 'bytes=0-499', @c['range'] - end - - def test_content_range - end - - def test_range_length - @c['Content-Range'] = "bytes 0-499/1000" - assert_equal 500, @c.range_length - @c['Content-Range'] = "bytes 1-500/1000" - assert_equal 500, @c.range_length - @c['Content-Range'] = "bytes 1-1/1000" - assert_equal 1, @c.range_length - end - - def test_chunked? - try_chunked true, 'chunked' - try_chunked true, ' chunked ' - try_chunked true, '(OK)chunked' - - try_chunked false, 'not-chunked' - try_chunked false, 'chunked-but-not-chunked' - end - - def try_chunked(bool, str) - @c['transfer-encoding'] = str - assert_equal bool, @c.chunked? - end - - def test_content_length - @c.delete('content-length') - assert_nil @c['content-length'] - - try_content_length 500, '500' - try_content_length 10000_0000_0000, '1000000000000' - try_content_length 123, ' 123' - try_content_length 1, '1 23' - try_content_length 500, '(OK)500' - assert_raises(Net::HTTPHeaderSyntaxError, 'here is no digit, but') { - @c['content-length'] = 'no digit' - @c.content_length - } - end - - def try_content_length(len, str) - @c['content-length'] = str - assert_equal len, @c.content_length - end - - def test_content_length= - @c.content_length = 0 - assert_equal 0, @c.content_length - @c.content_length = 1 - assert_equal 1, @c.content_length - @c.content_length = 999 - assert_equal 999, @c.content_length - @c.content_length = 10000000000000 - assert_equal 10000000000000, @c.content_length - end - - def test_content_type - assert_nil @c.content_type - @c.content_type = 'text/html' - assert_equal 'text/html', @c.content_type - @c.content_type = 'application/pdf' - assert_equal 'application/pdf', @c.content_type - @c.set_content_type 'text/html', {'charset' => 'iso-2022-jp'} - assert_equal 'text/html', @c.content_type - @c.content_type = 'text' - assert_equal 'text', @c.content_type - end - - def test_main_type - assert_nil @c.main_type - @c.content_type = 'text/html' - assert_equal 'text', @c.main_type - @c.content_type = 'application/pdf' - assert_equal 'application', @c.main_type - @c.set_content_type 'text/html', {'charset' => 'iso-2022-jp'} - assert_equal 'text', @c.main_type - @c.content_type = 'text' - assert_equal 'text', @c.main_type - end - - def test_sub_type - assert_nil @c.sub_type - @c.content_type = 'text/html' - assert_equal 'html', @c.sub_type - @c.content_type = 'application/pdf' - assert_equal 'pdf', @c.sub_type - @c.set_content_type 'text/html', {'charset' => 'iso-2022-jp'} - assert_equal 'html', @c.sub_type - @c.content_type = 'text' - assert_nil @c.sub_type - end - - def test_type_params - assert_equal({}, @c.type_params) - @c.content_type = 'text/html' - assert_equal({}, @c.type_params) - @c.content_type = 'application/pdf' - assert_equal({}, @c.type_params) - @c.set_content_type 'text/html', {'charset' => 'iso-2022-jp'} - assert_equal({'charset' => 'iso-2022-jp'}, @c.type_params) - @c.content_type = 'text' - assert_equal({}, @c.type_params) - end - - def test_set_content_type - end - - def test_form_data= - @c.form_data = {"cmd"=>"search", "q"=>"ruby", "max"=>"50"} - assert_equal 'application/x-www-form-urlencoded', @c.content_type - assert_equal %w( cmd=search max=50 q=ruby ), @c.body.split('&').sort - end - - def test_set_form_data - @c.set_form_data "cmd"=>"search", "q"=>"ruby", "max"=>"50" - assert_equal 'application/x-www-form-urlencoded', @c.content_type - assert_equal %w( cmd=search max=50 q=ruby ), @c.body.split('&').sort - - @c.set_form_data "cmd"=>"search", "q"=>"ruby", "max"=>50 - assert_equal 'application/x-www-form-urlencoded', @c.content_type - assert_equal %w( cmd=search max=50 q=ruby ), @c.body.split('&').sort - - @c.set_form_data({"cmd"=>"search", "q"=>"ruby", "max"=>"50"}, ';') - assert_equal 'application/x-www-form-urlencoded', @c.content_type - assert_equal %w( cmd=search max=50 q=ruby ), @c.body.split(';').sort - end - - def test_basic_auth - end - - def test_proxy_basic_auth - end - -end diff --git a/trunk/test/net/http/test_https.rb b/trunk/test/net/http/test_https.rb deleted file mode 100644 index adb50c58fb..0000000000 --- a/trunk/test/net/http/test_https.rb +++ /dev/null @@ -1,97 +0,0 @@ -require "test/unit" -begin - require 'net/https' - require 'stringio' - require File.expand_path("../../openssl/utils", File.dirname(__FILE__)) - require File.expand_path("utils", File.dirname(__FILE__)) -rescue LoadError - # should skip this test -end - -class TestNetHTTPS < Test::Unit::TestCase - include TestNetHTTPUtils - - subject = OpenSSL::X509::Name.parse("/DC=org/DC=ruby-lang/CN=localhost") - exts = [ - ["keyUsage", "keyEncipherment,digitalSignature", true], - ] - key = OpenSSL::TestUtils::TEST_KEY_RSA1024 - cert = OpenSSL::TestUtils.issue_cert( - subject, key, 1, Time.now, Time.now + 3600, exts, - nil, nil, OpenSSL::Digest::SHA1.new - ) - - CONFIG = { - 'host' => '127.0.0.1', - 'port' => 10081, - 'proxy_host' => nil, - 'proxy_port' => nil, - 'ssl_enable' => true, - 'ssl_certificate' => cert, - 'ssl_private_key' => key, - } - - def test_get - http = Net::HTTP.new("localhost", config("port")) - http.use_ssl = true - http.verify_callback = Proc.new do |preverify_ok, store_ctx| - store_ctx.current_cert.to_der == config('ssl_certificate').to_der - end - http.request_get("/") {|res| - assert_equal($test_net_http_data, res.body) - } - end - - def test_post - http = Net::HTTP.new("localhost", config("port")) - http.use_ssl = true - http.verify_callback = Proc.new do |preverify_ok, store_ctx| - store_ctx.current_cert.to_der == config('ssl_certificate').to_der - end - data = config('ssl_private_key').to_der - http.request_post("/", data) {|res| - assert_equal(data, res.body) - } - end - - if ENV["RUBY_OPENSSL_TEST_ALL"] - def test_verify - http = Net::HTTP.new("ssl.netlab.jp", 443) - http.use_ssl = true - assert( - (http.request_head("/"){|res| } rescue false), - "The system may not have default CA certificate store." - ) - end - end - - def test_verify_none - http = Net::HTTP.new("localhost", config("port")) - http.use_ssl = true - http.verify_mode = OpenSSL::SSL::VERIFY_NONE - http.request_get("/") {|res| - assert_equal($test_net_http_data, res.body) - } - end - - def test_certificate_verify_failure - http = Net::HTTP.new("localhost", config("port")) - http.use_ssl = true - ex = assert_raise(OpenSSL::SSL::SSLError){ - http.request_get("/") {|res| } - } - assert_match(/certificate verify failed/, ex.message) - end - - def test_identity_verify_failure - http = Net::HTTP.new("127.0.0.1", config("port")) - http.use_ssl = true - http.verify_callback = Proc.new do |preverify_ok, store_ctx| - store_ctx.current_cert.to_der == config('ssl_certificate').to_der - end - ex = assert_raise(OpenSSL::SSL::SSLError){ - http.request_get("/") {|res| } - } - assert_match(/hostname was not match/, ex.message) - end -end if defined?(OpenSSL) diff --git a/trunk/test/net/http/test_https_proxy.rb b/trunk/test/net/http/test_https_proxy.rb deleted file mode 100644 index 4f26342339..0000000000 --- a/trunk/test/net/http/test_https_proxy.rb +++ /dev/null @@ -1,37 +0,0 @@ -begin - require 'net/https' -rescue LoadError -end -require 'test/unit' - -class HTTPSProxyTest < Test::Unit::TestCase - def test_https_proxy_authentication - t = nil - TCPServer.open("127.0.0.1", 0) {|serv| - _, port, _, _ = serv.addr - t = Thread.new { - proxy = Net::HTTP.Proxy("127.0.0.1", port, 'user', 'password') - http = proxy.new("foo.example.org", 8000) - http.use_ssl = true - http.verify_mode = OpenSSL::SSL::VERIFY_NONE - begin - http.start - rescue EOFError - end - } - sock = serv.accept - proxy_request = sock.gets("\r\n\r\n") - assert_equal( - "CONNECT foo.example.org:8000 HTTP/1.1\r\n" + - "Host: foo.example.org:8000\r\n" + - "Proxy-Authorization: Basic dXNlcjpwYXNzd29yZA==\r\n" + - "\r\n", - proxy_request, - "[ruby-dev:25673]") - sock.close - } - ensure - t.join if t - end -end if defined?(OpenSSL) - diff --git a/trunk/test/net/http/utils.rb b/trunk/test/net/http/utils.rb deleted file mode 100644 index e685a8ff61..0000000000 --- a/trunk/test/net/http/utils.rb +++ /dev/null @@ -1,96 +0,0 @@ -require 'webrick' -begin - require "webrick/https" -rescue LoadError - # SSL features cannot be tested -end -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_config = { - :BindAddress => config('host'), - :Port => config('port'), - :Logger => WEBrick::Log.new(NullWriter.new), - :AccessLog => [], - :ShutdownSocketWithoutClose => true, - :ServerType => Thread, - } - if defined?(OpenSSL) and config('ssl_enable') - server_config.update({ - :SSLEnable => true, - :SSLCertificate => config('ssl_certificate'), - :SSLPrivateKey => config('ssl_private_key'), - }) - end - @server = WEBrick::HTTPServer.new(server_config) - @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.force_encoding("ASCII-8BIT") - $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 |