From fcbf63e62c627deae76c1b8cb8c0876c536ed811 Mon Sep 17 00:00:00 2001 From: Jari Vetoniemi Date: Mon, 16 Mar 2020 18:49:26 +0900 Subject: Fresh start --- jni/ruby/test/net/http/test_buffered_io.rb | 17 + jni/ruby/test/net/http/test_http.rb | 933 +++++++++++++++++++++++++++ jni/ruby/test/net/http/test_http_request.rb | 79 +++ jni/ruby/test/net/http/test_httpheader.rb | 334 ++++++++++ jni/ruby/test/net/http/test_httpresponse.rb | 394 +++++++++++ jni/ruby/test/net/http/test_httpresponses.rb | 24 + jni/ruby/test/net/http/test_https.rb | 194 ++++++ jni/ruby/test/net/http/test_https_proxy.rb | 46 ++ jni/ruby/test/net/http/utils.rb | 108 ++++ 9 files changed, 2129 insertions(+) create mode 100644 jni/ruby/test/net/http/test_buffered_io.rb create mode 100644 jni/ruby/test/net/http/test_http.rb create mode 100644 jni/ruby/test/net/http/test_http_request.rb create mode 100644 jni/ruby/test/net/http/test_httpheader.rb create mode 100644 jni/ruby/test/net/http/test_httpresponse.rb create mode 100644 jni/ruby/test/net/http/test_httpresponses.rb create mode 100644 jni/ruby/test/net/http/test_https.rb create mode 100644 jni/ruby/test/net/http/test_https_proxy.rb create mode 100644 jni/ruby/test/net/http/utils.rb (limited to 'jni/ruby/test/net/http') diff --git a/jni/ruby/test/net/http/test_buffered_io.rb b/jni/ruby/test/net/http/test_buffered_io.rb new file mode 100644 index 0000000..e24e7c1 --- /dev/null +++ b/jni/ruby/test/net/http/test_buffered_io.rb @@ -0,0 +1,17 @@ +require 'test/unit' +require 'net/http' +require 'stringio' + +require_relative 'utils' + +module Net + class TestBufferedIO < Test::Unit::TestCase + def test_eof? + s = StringIO.new + assert s.eof? + bio = BufferedIO.new(s) + assert_equal s, bio.io + assert_equal s.eof?, bio.eof? + end + end +end diff --git a/jni/ruby/test/net/http/test_http.rb b/jni/ruby/test/net/http/test_http.rb new file mode 100644 index 0000000..9d5cf39 --- /dev/null +++ b/jni/ruby/test/net/http/test_http.rb @@ -0,0 +1,933 @@ +# coding: US-ASCII +require 'test/unit' +require 'net/http' +require 'stringio' +require_relative 'utils' + +class TestNetHTTP < Test::Unit::TestCase + + def test_class_Proxy + no_proxy_class = Net::HTTP.Proxy nil + + assert_equal Net::HTTP, no_proxy_class + + proxy_class = Net::HTTP.Proxy 'proxy.example', 8000, 'user', 'pass' + + refute_equal Net::HTTP, proxy_class + + assert_operator proxy_class, :<, Net::HTTP + + assert_equal 'proxy.example', proxy_class.proxy_address + assert_equal 8000, proxy_class.proxy_port + assert_equal 'user', proxy_class.proxy_user + assert_equal 'pass', proxy_class.proxy_pass + + http = proxy_class.new 'example' + + refute http.proxy_from_env? + + + proxy_class = Net::HTTP.Proxy 'proxy.example' + assert_equal 'proxy.example', proxy_class.proxy_address + assert_equal 80, proxy_class.proxy_port + end + + def test_class_Proxy_from_ENV + clean_http_proxy_env do + ENV['http_proxy'] = 'http://proxy.example:8000' + + # These are ignored on purpose. See Bug 4388 and Feature 6546 + ENV['http_proxy_user'] = 'user' + ENV['http_proxy_pass'] = 'pass' + + proxy_class = Net::HTTP.Proxy :ENV + + refute_equal Net::HTTP, proxy_class + + assert_operator proxy_class, :<, Net::HTTP + + assert_nil proxy_class.proxy_address + assert_nil proxy_class.proxy_user + assert_nil proxy_class.proxy_pass + + refute_equal 8000, proxy_class.proxy_port + + http = proxy_class.new 'example' + + assert http.proxy_from_env? + end + end + + def test_edit_path + http = Net::HTTP.new 'example', nil, nil + + edited = http.send :edit_path, '/path' + + assert_equal '/path', edited + + http.use_ssl = true + + edited = http.send :edit_path, '/path' + + assert_equal '/path', edited + end + + def test_edit_path_proxy + http = Net::HTTP.new 'example', nil, 'proxy.example' + + edited = http.send :edit_path, '/path' + + assert_equal 'http://example/path', edited + + http.use_ssl = true + + edited = http.send :edit_path, '/path' + + assert_equal '/path', edited + end + + def test_proxy_address + clean_http_proxy_env do + http = Net::HTTP.new 'example', nil, 'proxy.example' + assert_equal 'proxy.example', http.proxy_address + + http = Net::HTTP.new 'example', nil + assert_equal nil, http.proxy_address + end + end + + def test_proxy_address_ENV + clean_http_proxy_env do + ENV['http_proxy'] = 'http://proxy.example:8000' + + http = Net::HTTP.new 'example' + + assert_equal 'proxy.example', http.proxy_address + end + end + + def test_proxy_eh_no_proxy + clean_http_proxy_env do + assert_equal false, Net::HTTP.new('example', nil, nil).proxy? + end + end + + def test_proxy_eh_ENV + clean_http_proxy_env do + ENV['http_proxy'] = 'http://proxy.example:8000' + + http = Net::HTTP.new 'example' + + assert_equal true, http.proxy? + end + end + + def test_proxy_eh_ENV_none_set + clean_http_proxy_env do + assert_equal false, Net::HTTP.new('example').proxy? + end + end + + def test_proxy_eh_ENV_no_proxy + clean_http_proxy_env do + ENV['http_proxy'] = 'http://proxy.example:8000' + ENV['no_proxy'] = 'example' + + assert_equal false, Net::HTTP.new('example').proxy? + end + end + + def test_proxy_port + clean_http_proxy_env do + http = Net::HTTP.new 'exmaple', nil, 'proxy.example' + assert_equal 'proxy.example', http.proxy_address + assert_equal 80, http.proxy_port + http = Net::HTTP.new 'exmaple', nil, 'proxy.example', 8000 + assert_equal 8000, http.proxy_port + http = Net::HTTP.new 'exmaple', nil + assert_equal nil, http.proxy_port + end + end + + def test_proxy_port_ENV + clean_http_proxy_env do + ENV['http_proxy'] = 'http://proxy.example:8000' + + http = Net::HTTP.new 'example' + + assert_equal 8000, http.proxy_port + end + end + + def test_newobj + clean_http_proxy_env do + ENV['http_proxy'] = 'http://proxy.example:8000' + + http = Net::HTTP.newobj 'example' + + assert_equal false, http.proxy? + end + end + + def 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 + +end + +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'] + unless self.is_a?(TestNetHTTP_v1_2_chunked) + assert_equal $test_net_http_data.size, res['Content-Length'].to_i + end + } + 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 = http.get('/') + assert_kind_of Net::HTTPResponse, res + assert_kind_of String, res.body + unless self.is_a?(TestNetHTTP_v1_2_chunked) + assert_not_nil res['content-length'] + assert_equal $test_net_http_data.size, res['content-length'].to_i + end + 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 + + assert_nothing_raised { + http.get('/', { 'User-Agent' => 'test' }.freeze) + } + + assert res.decode_content, '[Bug #7924]' if Net::HTTP::HAVE_ZLIB + end + + def _test_get__iter(http) + buf = '' + res = http.get('/') {|s| buf << s } + assert_kind_of Net::HTTPResponse, res + # assert_kind_of String, res.body + unless self.is_a?(TestNetHTTP_v1_2_chunked) + assert_not_nil res['content-length'] + assert_equal $test_net_http_data.size, res['content-length'].to_i + end + 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 = http.get('/') {|s| buf << s } + assert_kind_of Net::HTTPResponse, res + # assert_kind_of String, res.body + unless self.is_a?(TestNetHTTP_v1_2_chunked) + assert_not_nil res['content-length'] + assert_equal $test_net_http_data.size, res['content-length'].to_i + end + 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 + @log_tester = nil # server may encount ECONNRESET + end + + def test_get__implicit_start + res = new().get('/') + assert_kind_of Net::HTTPResponse, res + assert_kind_of String, res.body + unless self.is_a?(TestNetHTTP_v1_2_chunked) + assert_not_nil res['content-length'] + end + 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| + EnvUtil.suppress_warning do + assert_kind_of Net::HTTPResponse, res + assert_kind_of Net::HTTPResponse, res.header + end + + unless self.is_a?(TestNetHTTP_v1_2_chunked) + assert_not_nil res['content-length'] + end + 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 + _test_post__no_data http + } + end + + def _test_post__base(http) + uheader = {} + uheader['Accept'] = 'application/octet-stream' + uheader['Content-Type'] = 'application/x-www-form-urlencoded' + data = 'post data' + res = http.post('/', data, uheader) + assert_kind_of Net::HTTPResponse, res + assert_kind_of String, res.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, {'content-type' => 'application/x-www-form-urlencoded'}, f) + assert_equal data, f.string + end + + def _test_post__no_data(http) + unless self.is_a?(TestNetHTTP_v1_2_chunked) + EnvUtil.suppress_warning do + data = nil + res = http.post('/', data) + assert_not_equal '411', res.code + end + end + end + + def test_s_post_form + url = "http://#{config('host')}:#{config('port')}/" + res = Net::HTTP.post_form( + URI.parse(url), + "a" => "x") + assert_equal ["a=x"], res.body.split(/[;&]/).sort + + res = Net::HTTP.post_form( + URI.parse(url), + "a" => "x", + "b" => "y") + assert_equal ["a=x", "b=y"], res.body.split(/[;&]/).sort + + res = Net::HTTP.post_form( + URI.parse(url), + "a" => ["x1", "x2"], + "b" => "y") + assert_equal url, res['X-request-uri'] + assert_equal ["a=x1", "a=x2", "b=y"], res.body.split(/[;&]/).sort + + res = Net::HTTP.post_form( + URI.parse(url + '?a=x'), + "b" => "y") + assert_equal url + '?a=x', res['X-request-uri'] + assert_equal ["b=y"], res.body.split(/[;&]/).sort + end + + def test_patch + start {|http| + _test_patch__base http + } + end + + def _test_patch__base(http) + uheader = {} + uheader['Accept'] = 'application/octet-stream' + uheader['Content-Type'] = 'application/x-www-form-urlencoded' + data = 'patch data' + res = http.patch('/', data, uheader) + assert_kind_of Net::HTTPResponse, res + assert_kind_of String, res.body + assert_equal data, res.body + assert_equal data, res.entity + end + + def test_timeout_during_HTTP_session + bug4246 = "expected the HTTP session to have timed out but have not. c.f. [ruby-core:34203]" + + th = nil + # listen for connections... but deliberately do not read + TCPServer.open('localhost', 0) {|server| + port = server.addr[1] + + conn = Net::HTTP.new('localhost', port) + conn.read_timeout = 0.01 + conn.open_timeout = 0.1 + + th = Thread.new do + assert_raise(Net::ReadTimeout) { + conn.get('/') + } + end + assert th.join(10), bug4246 + } + ensure + th.kill + th.join + end +end + + +module TestNetHTTP_version_1_2_methods + + def test_request + start {|http| + _test_request__GET http + _test_request__accept_encoding 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 + _test_request__uri http + _test_request__uri_host 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 + unless self.is_a?(TestNetHTTP_v1_2_chunked) + assert_not_nil res['content-length'] + assert_equal $test_net_http_data.size, res['content-length'].to_i + end + assert_equal $test_net_http_data.size, res.body.size + assert_equal $test_net_http_data, res.body + + assert res.decode_content, 'Bug #7831' if Net::HTTP::HAVE_ZLIB + } + end + + def _test_request__accept_encoding(http) + req = Net::HTTP::Get.new('/', 'accept-encoding' => 'deflate') + http.request(req) {|res| + assert_kind_of Net::HTTPResponse, res + assert_kind_of String, res.body + unless self.is_a?(TestNetHTTP_v1_2_chunked) + assert_not_nil res['content-length'] + assert_equal $test_net_http_data.size, res['content-length'].to_i + end + assert_equal $test_net_http_data.size, res.body.size + assert_equal $test_net_http_data, res.body + + refute res.decode_content, 'Bug #7831' if Net::HTTP::HAVE_ZLIB + } + end + + def _test_request__file(http) + req = Net::HTTP::Get.new('/') + http.request(req) {|res| + assert_kind_of Net::HTTPResponse, res + unless self.is_a?(TestNetHTTP_v1_2_chunked) + assert_not_nil res['content-length'] + assert_equal $test_net_http_data.size, res['content-length'].to_i + end + 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 + unless self.is_a?(TestNetHTTP_v1_2_chunked) + assert_not_nil res['content-length'] + assert_equal $test_net_http_data.size, res['content-length'].to_i + end + 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 + req['Content-Type'] = 'application/x-www-form-urlencoded' + http.request(req, data) {|res| + assert_kind_of Net::HTTPResponse, res + unless self.is_a?(TestNetHTTP_v1_2_chunked) + assert_equal data.size, res['content-length'].to_i + end + 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['Content-Type'] = 'application/x-www-form-urlencoded' + 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_request__path(http) + uri = URI 'https://example/' + req = Net::HTTP::Get.new('/') + + res = http.request(req) + + assert_kind_of URI::Generic, req.uri + + refute_equal uri, req.uri + + assert_equal uri, res.uri + + refute_same uri, req.uri + refute_same req.uri, res.uri + end + + def _test_request__uri(http) + uri = URI 'https://example/' + req = Net::HTTP::Get.new(uri) + + res = http.request(req) + + assert_kind_of URI::Generic, req.uri + + refute_equal uri, req.uri + + assert_equal req.uri, res.uri + + refute_same uri, req.uri + refute_same req.uri, res.uri + end + + def _test_request__uri_host(http) + uri = URI 'http://other.example/' + + req = Net::HTTP::Get.new(uri) + req['host'] = 'example' + + res = http.request(req) + + assert_kind_of URI::Generic, req.uri + + assert_equal URI("http://example:#{http.port}"), res.uri + end + + def test_send_request + start {|http| + _test_send_request__GET http + _test_send_request__HEAD http + _test_send_request__POST http + } + end + + def _test_send_request__GET(http) + res = http.send_request('GET', '/') + assert_kind_of Net::HTTPResponse, res + unless self.is_a?(TestNetHTTP_v1_2_chunked) + assert_equal $test_net_http_data.size, res['content-length'].to_i + end + assert_kind_of String, res.body + assert_equal $test_net_http_data, res.body + end + + def _test_send_request__HEAD(http) + res = http.send_request('HEAD', '/') + assert_kind_of Net::HTTPResponse, res + unless self.is_a?(TestNetHTTP_v1_2_chunked) + assert_not_nil res['content-length'] + assert_equal $test_net_http_data.size, res['content-length'].to_i + end + assert_nil res.body + end + + def _test_send_request__POST(http) + data = 'aaabbb cc ddddddddddd lkjoiu4j3qlkuoa' + res = http.send_request('POST', '/', data, 'content-type' => 'application/x-www-form-urlencoded') + 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_set_form + require 'tempfile' + Tempfile.create('ruby-test') {|file| + file << "\u{30c7}\u{30fc}\u{30bf}" + data = [ + ['name', 'Gonbei Nanashi'], + ['name', "\u{540d}\u{7121}\u{3057}\u{306e}\u{6a29}\u{5175}\u{885b}"], + ['s"i\o', StringIO.new("\u{3042 3044 4e9c 925b}")], + ["file", file, filename: "ruby-test"] + ] + expected = <<"__EOM__".gsub(/\n/, "\r\n") +-- +Content-Disposition: form-data; name="name" + +Gonbei Nanashi +-- +Content-Disposition: form-data; name="name" + +\xE5\x90\x8D\xE7\x84\xA1\xE3\x81\x97\xE3\x81\xAE\xE6\xA8\xA9\xE5\x85\xB5\xE8\xA1\x9B +-- +Content-Disposition: form-data; name="s\\"i\\\\o" + +\xE3\x81\x82\xE3\x81\x84\xE4\xBA\x9C\xE9\x89\x9B +-- +Content-Disposition: form-data; name="file"; filename="ruby-test" +Content-Type: application/octet-stream + +\xE3\x83\x87\xE3\x83\xBC\xE3\x82\xBF +---- +__EOM__ + start {|http| + _test_set_form_urlencoded(http, data.reject{|k,v|!v.is_a?(String)}) + _test_set_form_multipart(http, false, data, expected) + _test_set_form_multipart(http, true, data, expected) + } + } + end + + def _test_set_form_urlencoded(http, data) + req = Net::HTTP::Post.new('/') + req.set_form(data) + res = http.request req + assert_equal "name=Gonbei+Nanashi&name=%E5%90%8D%E7%84%A1%E3%81%97%E3%81%AE%E6%A8%A9%E5%85%B5%E8%A1%9B", res.body + end + + def _test_set_form_multipart(http, chunked_p, data, expected) + data.each{|k,v|v.rewind rescue nil} + req = Net::HTTP::Post.new('/') + req.set_form(data, 'multipart/form-data') + req['Transfer-Encoding'] = 'chunked' if chunked_p + res = http.request req + body = res.body + assert_match(/\A--(?\S+)/, body) + /\A--(?\S+)/ =~ body + expected = expected.gsub(//, boundary) + assert_equal(expected, body) + end + + def test_set_form_with_file + require 'tempfile' + Tempfile.create('ruby-test') {|file| + file.binmode + file << $test_net_http_data + filename = File.basename(file.to_path) + data = [['file', file]] + expected = <<"__EOM__".gsub(/\n/, "\r\n") +-- +Content-Disposition: form-data; name="file"; filename="" +Content-Type: application/octet-stream + + +---- +__EOM__ + expected.sub!(//, filename) + expected.sub!(//, $test_net_http_data) + start {|http| + data.each{|k,v|v.rewind rescue nil} + req = Net::HTTP::Post.new('/') + req.set_form(data, 'multipart/form-data') + res = http.request req + body = res.body + header, _ = body.split(/\r\n\r\n/, 2) + assert_match(/\A--(?\S+)/, body) + /\A--(?\S+)/ =~ body + expected = expected.gsub(//, boundary) + assert_match(/^--(?\S+)\r\n/, header) + assert_match( + /^Content-Disposition: form-data; name="file"; filename="#{filename}"\r\n/, + header) + assert_equal(expected, body) + + data.each{|k,v|v.rewind rescue nil} + req['Transfer-Encoding'] = 'chunked' + res = http.request req + #assert_equal(expected, res.body) + } + } + end +end + +class TestNetHTTP_v1_2 < Test::Unit::TestCase + CONFIG = { + 'host' => '127.0.0.1', + '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 + +class TestNetHTTP_v1_2_chunked < Test::Unit::TestCase + CONFIG = { + 'host' => '127.0.0.1', + 'proxy_host' => nil, + 'proxy_port' => nil, + 'chunked' => true, + } + + include TestNetHTTPUtils + include TestNetHTTP_version_1_1_methods + include TestNetHTTP_version_1_2_methods + + def new + Net::HTTP.version_1_2 + super + end + + def test_chunked_break + assert_nothing_raised("[ruby-core:29229]") { + start {|http| + http.request_get('/') {|res| + res.read_body {|chunk| + break + } + } + } + } + end +end + +class TestNetHTTPContinue < Test::Unit::TestCase + CONFIG = { + 'host' => '127.0.0.1', + 'proxy_host' => nil, + 'proxy_port' => nil, + 'chunked' => true, + } + + include TestNetHTTPUtils + + def logfile + @debug = StringIO.new('') + end + + def mount_proc(&block) + @server.mount('/continue', WEBrick::HTTPServlet::ProcHandler.new(block.to_proc)) + end + + def test_expect_continue + mount_proc {|req, res| + req.continue + res.body = req.query['body'] + } + start {|http| + uheader = {'content-type' => 'application/x-www-form-urlencoded', 'expect' => '100-continue'} + http.continue_timeout = 0.2 + http.request_post('/continue', 'body=BODY', uheader) {|res| + assert_equal('BODY', res.read_body) + } + } + assert_match(/Expect: 100-continue/, @debug.string) + assert_match(/HTTP\/1.1 100 continue/, @debug.string) + end + + def test_expect_continue_timeout + mount_proc {|req, res| + sleep 0.2 + req.continue # just ignored because it's '100' + res.body = req.query['body'] + } + start {|http| + uheader = {'content-type' => 'application/x-www-form-urlencoded', 'expect' => '100-continue'} + http.continue_timeout = 0 + http.request_post('/continue', 'body=BODY', uheader) {|res| + assert_equal('BODY', res.read_body) + } + } + assert_match(/Expect: 100-continue/, @debug.string) + assert_match(/HTTP\/1.1 100 continue/, @debug.string) + end + + def test_expect_continue_error + mount_proc {|req, res| + res.status = 501 + res.body = req.query['body'] + } + start {|http| + uheader = {'content-type' => 'application/x-www-form-urlencoded', 'expect' => '100-continue'} + http.continue_timeout = 0 + http.request_post('/continue', 'body=ERROR', uheader) {|res| + assert_equal('ERROR', res.read_body) + } + } + assert_match(/Expect: 100-continue/, @debug.string) + assert_not_match(/HTTP\/1.1 100 continue/, @debug.string) + end + + def test_expect_continue_error_while_waiting + mount_proc {|req, res| + res.status = 501 + res.body = req.query['body'] + } + start {|http| + uheader = {'content-type' => 'application/x-www-form-urlencoded', 'expect' => '100-continue'} + http.continue_timeout = 0.5 + http.request_post('/continue', 'body=ERROR', uheader) {|res| + assert_equal('ERROR', res.read_body) + } + } + assert_match(/Expect: 100-continue/, @debug.string) + assert_not_match(/HTTP\/1.1 100 continue/, @debug.string) + end +end + +class TestNetHTTPKeepAlive < Test::Unit::TestCase + CONFIG = { + 'host' => '127.0.0.1', + 'proxy_host' => nil, + 'proxy_port' => nil, + 'RequestTimeout' => 1, + } + + include TestNetHTTPUtils + + def test_keep_alive_get_auto_reconnect + start {|http| + res = http.get('/') + http.keep_alive_timeout = 1 + assert_kind_of Net::HTTPResponse, res + assert_kind_of String, res.body + sleep 1.5 + assert_nothing_raised { + res = http.get('/') + } + assert_kind_of Net::HTTPResponse, res + assert_kind_of String, res.body + } + end + + def test_keep_alive_get_auto_retry + start {|http| + res = http.get('/') + http.keep_alive_timeout = 5 + assert_kind_of Net::HTTPResponse, res + assert_kind_of String, res.body + sleep 1.5 + res = http.get('/') + assert_kind_of Net::HTTPResponse, res + assert_kind_of String, res.body + } + end + + def test_keep_alive_server_close + def @server.run(sock) + sock.close + end + + start {|http| + assert_raises(EOFError, Errno::ECONNRESET, IOError) { + http.get('/') + } + } + end +end + +class TestNetHTTPLocalBind < Test::Unit::TestCase + CONFIG = { + 'host' => 'localhost', + 'proxy_host' => nil, + 'proxy_port' => nil, + } + + include TestNetHTTPUtils + + def test_bind_to_local_host + @server.mount_proc('/show_ip') { |req, res| res.body = req.remote_ip } + + http = Net::HTTP.new(config('host'), config('port')) + http.local_host = Addrinfo.tcp(config('host'), config('port')).ip_address + assert_not_nil(http.local_host) + assert_nil(http.local_port) + + res = http.get('/show_ip') + assert_equal(http.local_host, res.body) + end + + def test_bind_to_local_port + @server.mount_proc('/show_port') { |req, res| res.body = req.peeraddr[1].to_s } + + http = Net::HTTP.new(config('host'), config('port')) + http.local_host = Addrinfo.tcp(config('host'), config('port')).ip_address + http.local_port = Addrinfo.tcp(config('host'), 0).bind {|s| + s.local_address.ip_port.to_s + } + assert_not_nil(http.local_host) + assert_not_nil(http.local_port) + + res = http.get('/show_port') + assert_equal(http.local_port, res.body) + end +end + diff --git a/jni/ruby/test/net/http/test_http_request.rb b/jni/ruby/test/net/http/test_http_request.rb new file mode 100644 index 0000000..1dcb847 --- /dev/null +++ b/jni/ruby/test/net/http/test_http_request.rb @@ -0,0 +1,79 @@ +require 'net/http' +require 'test/unit' +require 'stringio' + +class HTTPRequestTest < Test::Unit::TestCase + + def test_initialize_GET + req = Net::HTTP::Get.new '/' + + assert_equal 'GET', req.method + refute req.request_body_permitted? + assert req.response_body_permitted? + + expected = { + 'accept' => %w[*/*], + 'user-agent' => %w[Ruby], + } + + expected['accept-encoding'] = %w[gzip;q=1.0,deflate;q=0.6,identity;q=0.3] if + Net::HTTP::HAVE_ZLIB + + assert_equal expected, req.to_hash + end + + def test_initialize_GET_range + req = Net::HTTP::Get.new '/', 'Range' => 'bytes=0-9' + + assert_equal 'GET', req.method + refute req.request_body_permitted? + assert req.response_body_permitted? + + expected = { + 'accept' => %w[*/*], + 'user-agent' => %w[Ruby], + 'range' => %w[bytes=0-9], + } + + assert_equal expected, req.to_hash + end + + def test_initialize_HEAD + req = Net::HTTP::Head.new '/' + + assert_equal 'HEAD', req.method + refute req.request_body_permitted? + refute req.response_body_permitted? + + expected = { + 'accept' => %w[*/*], + 'user-agent' => %w[Ruby], + } + + assert_equal expected, req.to_hash + end + + def test_initialize_accept_encoding + req1 = Net::HTTP::Get.new '/' + + assert req1.decode_content, 'Bug #7831 - automatically decode content' + + req2 = Net::HTTP::Get.new '/', 'accept-encoding' => 'identity' + + refute req2.decode_content, + 'Bug #7381 - do not decode content if the user overrides' + end if Net::HTTP::HAVE_ZLIB + + def test_header_set + req = Net::HTTP::Get.new '/' + + assert req.decode_content, 'Bug #7831 - automatically decode content' + + req['accept-encoding'] = 'identity' + + refute req.decode_content, + 'Bug #7831 - do not decode content if the user overrides' + end if Net::HTTP::HAVE_ZLIB + +end + diff --git a/jni/ruby/test/net/http/test_httpheader.rb b/jni/ruby/test/net/http/test_httpheader.rb new file mode 100644 index 0000000..0623871 --- /dev/null +++ b/jni/ruby/test/net/http/test_httpheader.rb @@ -0,0 +1,334 @@ +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_invalid_range('5-1') + try_range([234..567], '234-567') + try_range([-5..-1], '-5') + try_invalid_range('-0') + try_range([1..-1], '1-') + try_range([0..0,-1..-1], '0-0,-1') + try_range([1..2, 3..4], '1-2,3-4') + try_range([1..2, 3..4], '1-2 , 3-4') + try_range([1..2, 1..4], '1-2,1-4') + + try_invalid_range('invalid') + try_invalid_range(' 12-') + try_invalid_range('12- ') + try_invalid_range('123-abc') + try_invalid_range('abc-123') + end + + def try_range(r, s) + @c['range'] = "bytes=#{s}" + assert_equal r, @c.range + end + + def try_invalid_range(s) + @c['range'] = "bytes=#{s}" + assert_raise(Net::HTTPHeaderSyntaxError, s){ @c.range } + 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_raise(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/jni/ruby/test/net/http/test_httpresponse.rb b/jni/ruby/test/net/http/test_httpresponse.rb new file mode 100644 index 0000000..404c7ae --- /dev/null +++ b/jni/ruby/test/net/http/test_httpresponse.rb @@ -0,0 +1,394 @@ +# coding: US-ASCII +require 'net/http' +require 'test/unit' +require 'stringio' + +class HTTPResponseTest < Test::Unit::TestCase + def test_singleline_header + io = dummy_io(< #{klass.name}" + end + assert(klass < group, "#{klass.name} (#{code}) must inherit from #{group.name}") + } + end +end diff --git a/jni/ruby/test/net/http/test_https.rb b/jni/ruby/test/net/http/test_https.rb new file mode 100644 index 0000000..8177d94 --- /dev/null +++ b/jni/ruby/test/net/http/test_https.rb @@ -0,0 +1,194 @@ +require "test/unit" +begin + require 'net/https' + require 'stringio' + require 'timeout' + 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', + '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) + } + rescue SystemCallError + skip $! + 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, {'content-type' => 'application/x-www-form-urlencoded'}) {|res| + assert_equal(data, res.body) + } + rescue SystemCallError + skip $! + end + + def test_session_reuse + 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.start + http.get("/") + http.finish + + http.start + http.get("/") + http.finish # three times due to possible bug in OpenSSL 0.9.8 + + sid = http.instance_variable_get(:@ssl_session).id + + http.start + http.get("/") + + socket = http.instance_variable_get(:@socket).io + + assert socket.session_reused? + + assert_equal sid, http.instance_variable_get(:@ssl_session).id + + http.finish + rescue SystemCallError + skip $! + end + + def test_session_reuse_but_expire + 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.ssl_timeout = -1 + http.start + http.get("/") + http.finish + + sid = http.instance_variable_get(:@ssl_session).id + + http.start + http.get("/") + + socket = http.instance_variable_get(:@socket).io + assert_equal false, socket.session_reused? + + assert_not_equal sid, http.instance_variable_get(:@ssl_session).id + + http.finish + rescue SystemCallError + skip $! + 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) + } + rescue SystemCallError + skip $! + end + + def test_certificate_verify_failure + http = Net::HTTP.new("localhost", config("port")) + http.use_ssl = true + ex = assert_raise(OpenSSL::SSL::SSLError){ + begin + http.request_get("/") {|res| } + rescue SystemCallError + skip $! + end + } + assert_match(/certificate verify failed/, ex.message) + unless /mswin|mingw/ =~ RUBY_PLATFORM + # on Windows, Errno::ECONNRESET will be raised, and it'll be eaten by + # WEBrick + @log_tester = lambda {|log| + assert_equal(1, log.length) + assert_match(/ERROR OpenSSL::SSL::SSLError:/, log[0]) + } + end + 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 \"127.0.0.1\" does not match/, ex.message) + end + + def test_timeout_during_SSL_handshake + bug4246 = "expected the SSL connection to have timed out but have not. [ruby-core:34203]" + + # listen for connections... but deliberately do not complete SSL handshake + TCPServer.open('localhost', 0) {|server| + port = server.addr[1] + + conn = Net::HTTP.new('localhost', port) + conn.use_ssl = true + conn.read_timeout = 0.01 + conn.open_timeout = 0.01 + + th = Thread.new do + assert_raise(Net::OpenTimeout) { + conn.get('/') + } + end + assert th.join(10), bug4246 + } + end +end if defined?(OpenSSL::TestUtils) diff --git a/jni/ruby/test/net/http/test_https_proxy.rb b/jni/ruby/test/net/http/test_https_proxy.rb new file mode 100644 index 0000000..1c8503b --- /dev/null +++ b/jni/ruby/test/net/http/test_https_proxy.rb @@ -0,0 +1,46 @@ +begin + require 'net/https' +rescue LoadError +end +require 'test/unit' + +class HTTPSProxyTest < Test::Unit::TestCase + def test_https_proxy_authentication + begin + OpenSSL + rescue LoadError + skip 'autoload problem. see [ruby-dev:45021][Bug #5786]' + end + + TCPServer.open("127.0.0.1", 0) {|serv| + _, port, _, _ = serv.addr + client_thread = 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 + } + server_thread = Thread.new { + sock = serv.accept + begin + 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]") + ensure + sock.close + end + } + assert_join_threads([client_thread, server_thread]) + } + end +end if defined?(OpenSSL) + diff --git a/jni/ruby/test/net/http/utils.rb b/jni/ruby/test/net/http/utils.rb new file mode 100644 index 0000000..dcd9469 --- /dev/null +++ b/jni/ruby/test/net/http/utils.rb @@ -0,0 +1,108 @@ +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) + @config ||= self.class::CONFIG + @config[key] + end + + def logfile + $DEBUG ? $stderr : NullWriter.new + end + + def setup + spawn_server + end + + def teardown + if @server + @server.shutdown + @server_thread.join + end + @log_tester.call(@log) if @log_tester + # resume global state + Net::HTTP.version_1_2 + end + + def spawn_server + @log = [] + @log_tester = lambda {|log| assert_equal([], log ) } + @config = self.class::CONFIG + server_config = { + :BindAddress => config('host'), + :Port => 0, + :Logger => WEBrick::Log.new(@log, WEBrick::BasicLog::WARN), + :AccessLog => [], + :ServerType => Thread, + } + server_config[:OutputBufferSize] = 4 if config('chunked') + server_config[:RequestTimeout] = config('RequestTimeout') if config('RequestTimeout') + if defined?(OpenSSL) and config('ssl_enable') + server_config.update({ + :SSLEnable => true, + :SSLCertificate => config('ssl_certificate'), + :SSLPrivateKey => config('ssl_private_key'), + :SSLTmpDhCallback => proc { OpenSSL::TestUtils::TEST_KEY_DH1024 }, + }) + end + @server = WEBrick::HTTPServer.new(server_config) + @server.mount('/', Servlet, config('chunked')) + @server_thread = @server.start + @config['port'] = @server[:Port] + 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 initialize(this, chunked = false) + @chunked = chunked + end + + def do_GET(req, res) + res['Content-Type'] = $test_net_http_data_type + res.body = $test_net_http_data + res.chunked = @chunked + end + + # echo server + def do_POST(req, res) + res['Content-Type'] = req['Content-Type'] + res['X-request-uri'] = req.request_uri.to_s + res.body = req.body + res.chunked = @chunked + end + + def do_PATCH(req, res) + res['Content-Type'] = req['Content-Type'] + res.body = req.body + res.chunked = @chunked + end + end + + class NullWriter + def <<(s) end + def puts(*args) end + def print(*args) end + def printf(*args) end + end +end -- cgit v1.2.3