Skip to content
34 changes: 24 additions & 10 deletions test/net/http/test_http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,11 @@ def test_get2
def test_post
start {|http|
_test_post__base http
}
start {|http|
_test_post__file http
}
start {|http|
_test_post__no_data http
}
end
Expand Down Expand Up @@ -629,10 +633,12 @@ def test_request
# _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
}
start {|http|
_test_request__stream_body http
}
end

def _test_request__GET(http)
Expand Down Expand Up @@ -843,7 +849,13 @@ def test_set_form
__EOM__
start {|http|
_test_set_form_urlencoded(http, data.reject{|k,v|!v.is_a?(String)})
}
start {|http|
@server.mount('/', lambda {|req, res| res.body = req.body })
_test_set_form_multipart(http, false, data, expected)
}
start {|http|
@server.mount('/', lambda {|req, res| res.body = req.body })
_test_set_form_multipart(http, true, data, expected)
}
}
Expand Down Expand Up @@ -887,6 +899,7 @@ def test_set_form_with_file
expected.sub!(/<filename>/, filename)
expected.sub!(/<data>/, $test_net_http_data)
start {|http|
@server.mount('/', lambda {|req, res| res.body = req.body })
data.each{|k,v|v.rewind rescue nil}
req = Net::HTTP::Post.new('/')
req.set_form(data, 'multipart/form-data')
Expand All @@ -902,10 +915,11 @@ def test_set_form_with_file
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)
# TODO: test with chunked
# data.each{|k,v|v.rewind rescue nil}
# req['Transfer-Encoding'] = 'chunked'
# res = http.request req
# assert_equal(expected, res.body)
}
}
end
Expand Down Expand Up @@ -984,7 +998,7 @@ def logfile
end

def mount_proc(&block)
@server.mount('/continue', WEBrick::HTTPServlet::ProcHandler.new(block.to_proc))
@server.mount('/continue', block.to_proc)
end

def test_expect_continue
Expand Down Expand Up @@ -1039,7 +1053,7 @@ def test_expect_continue_error
def test_expect_continue_error_before_body
@log_tester = nil
mount_proc {|req, res|
raise WEBrick::HTTPStatus::Forbidden
raise TestNetHTTPUtils::Forbidden
}
start {|http|
uheader = {'content-type' => 'application/x-www-form-urlencoded', 'content-length' => '5', 'expect' => '100-continue'}
Expand Down Expand Up @@ -1084,7 +1098,7 @@ def logfile
end

def mount_proc(&block)
@server.mount('/continue', WEBrick::HTTPServlet::ProcHandler.new(block.to_proc))
@server.mount('/continue', block.to_proc)
end

def test_info
Expand Down Expand Up @@ -1159,11 +1173,11 @@ def test_keep_alive_get_auto_retry
end

def test_keep_alive_reset_on_new_connection
# Using WEBrick's debug log output on accepting connection:
# Using debug log output on accepting connection:
#
# "[2021-04-29 20:36:46] DEBUG accept: 127.0.0.1:50674\n"
@log_tester = nil
@server.logger.level = WEBrick::BasicLog::DEBUG
@logger_level = :debug

start {|http|
res = http.get('/')
Expand Down
62 changes: 40 additions & 22 deletions test/net/http/test_https.rb
Original file line number Diff line number Diff line change
Expand Up @@ -240,27 +240,6 @@ def test_certificate_verify_failure
http.request_get("/") {|res| }
}
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
# the certificate's subject has CN=localhost
http = Net::HTTP.new(HOST_IP, config("port"))
http.use_ssl = true
http.cert_store = TEST_STORE
@log_tester = lambda {|_| }
ex = assert_raise(OpenSSL::SSL::SSLError){
http.request_get("/") {|res| }
}
re_msg = /certificate verify failed|hostname \"#{HOST_IP}\" does not match/
assert_match(re_msg, ex.message)
end

def test_timeout_during_SSL_handshake
Expand Down Expand Up @@ -295,7 +274,7 @@ def test_min_version
end

def test_max_version
http = Net::HTTP.new(HOST_IP, config("port"))
http = Net::HTTP.new(HOST, config("port"))
http.use_ssl = true
http.max_version = :SSL2
http.verify_callback = Proc.new do |preverify_ok, store_ctx|
Expand All @@ -310,3 +289,42 @@ def test_max_version
end

end if defined?(OpenSSL::SSL)

class TestNetHTTPSIdentityVerifyFailure < Test::Unit::TestCase
include TestNetHTTPUtils

def self.read_fixture(key)
File.read(File.expand_path("../fixtures/#{key}", __dir__))
end

HOST = 'localhost'
HOST_IP = '127.0.0.1'
CA_CERT = OpenSSL::X509::Certificate.new(read_fixture("cacert.pem"))
SERVER_KEY = OpenSSL::PKey.read(read_fixture("server.key"))
SERVER_CERT = OpenSSL::X509::Certificate.new(read_fixture("server.crt"))
DHPARAMS = OpenSSL::PKey::DH.new(read_fixture("dhparams.pem"))
TEST_STORE = OpenSSL::X509::Store.new.tap {|s| s.add_cert(CA_CERT) }

CONFIG = {
'host' => HOST_IP,
'proxy_host' => nil,
'proxy_port' => nil,
'ssl_enable' => true,
'ssl_certificate' => SERVER_CERT,
'ssl_private_key' => SERVER_KEY,
'ssl_tmp_dh_callback' => proc { DHPARAMS },
}

def test_identity_verify_failure
# the certificate's subject has CN=localhost
http = Net::HTTP.new(HOST_IP, config("port"))
http.use_ssl = true
http.cert_store = TEST_STORE
@log_tester = lambda {|_| }
ex = assert_raise(OpenSSL::SSL::SSLError){
http.request_get("/") {|res| }
}
re_msg = /certificate verify failed|hostname \"#{HOST_IP}\" does not match/
assert_match(re_msg, ex.message)
end
end if defined?(OpenSSL::SSL)
Loading