Skip to content

Commit 5952bd9

Browse files
authored
Merge pull request #180 from ruby/remove-webrick
Remove WEBrick from dependency
2 parents d39f1e3 + 6376592 commit 5952bd9

File tree

3 files changed

+361
-98
lines changed

3 files changed

+361
-98
lines changed

test/net/http/test_http.rb

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,11 @@ def test_get2
442442
def test_post
443443
start {|http|
444444
_test_post__base http
445+
}
446+
start {|http|
445447
_test_post__file http
448+
}
449+
start {|http|
446450
_test_post__no_data http
447451
}
448452
end
@@ -629,10 +633,12 @@ def test_request
629633
# _test_request__range http # WEBrick does not support Range: header.
630634
_test_request__HEAD http
631635
_test_request__POST http
632-
_test_request__stream_body http
633636
_test_request__uri http
634637
_test_request__uri_host http
635638
}
639+
start {|http|
640+
_test_request__stream_body http
641+
}
636642
end
637643

638644
def _test_request__GET(http)
@@ -843,7 +849,13 @@ def test_set_form
843849
__EOM__
844850
start {|http|
845851
_test_set_form_urlencoded(http, data.reject{|k,v|!v.is_a?(String)})
852+
}
853+
start {|http|
854+
@server.mount('/', lambda {|req, res| res.body = req.body })
846855
_test_set_form_multipart(http, false, data, expected)
856+
}
857+
start {|http|
858+
@server.mount('/', lambda {|req, res| res.body = req.body })
847859
_test_set_form_multipart(http, true, data, expected)
848860
}
849861
}
@@ -887,6 +899,7 @@ def test_set_form_with_file
887899
expected.sub!(/<filename>/, filename)
888900
expected.sub!(/<data>/, $test_net_http_data)
889901
start {|http|
902+
@server.mount('/', lambda {|req, res| res.body = req.body })
890903
data.each{|k,v|v.rewind rescue nil}
891904
req = Net::HTTP::Post.new('/')
892905
req.set_form(data, 'multipart/form-data')
@@ -902,10 +915,11 @@ def test_set_form_with_file
902915
header)
903916
assert_equal(expected, body)
904917

905-
data.each{|k,v|v.rewind rescue nil}
906-
req['Transfer-Encoding'] = 'chunked'
907-
res = http.request req
908-
#assert_equal(expected, res.body)
918+
# TODO: test with chunked
919+
# data.each{|k,v|v.rewind rescue nil}
920+
# req['Transfer-Encoding'] = 'chunked'
921+
# res = http.request req
922+
# assert_equal(expected, res.body)
909923
}
910924
}
911925
end
@@ -984,7 +998,7 @@ def logfile
984998
end
985999

9861000
def mount_proc(&block)
987-
@server.mount('/continue', WEBrick::HTTPServlet::ProcHandler.new(block.to_proc))
1001+
@server.mount('/continue', block.to_proc)
9881002
end
9891003

9901004
def test_expect_continue
@@ -1039,7 +1053,7 @@ def test_expect_continue_error
10391053
def test_expect_continue_error_before_body
10401054
@log_tester = nil
10411055
mount_proc {|req, res|
1042-
raise WEBrick::HTTPStatus::Forbidden
1056+
raise TestNetHTTPUtils::Forbidden
10431057
}
10441058
start {|http|
10451059
uheader = {'content-type' => 'application/x-www-form-urlencoded', 'content-length' => '5', 'expect' => '100-continue'}
@@ -1084,7 +1098,7 @@ def logfile
10841098
end
10851099

10861100
def mount_proc(&block)
1087-
@server.mount('/continue', WEBrick::HTTPServlet::ProcHandler.new(block.to_proc))
1101+
@server.mount('/continue', block.to_proc)
10881102
end
10891103

10901104
def test_info
@@ -1159,11 +1173,11 @@ def test_keep_alive_get_auto_retry
11591173
end
11601174

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

11681182
start {|http|
11691183
res = http.get('/')

test/net/http/test_https.rb

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -240,27 +240,6 @@ def test_certificate_verify_failure
240240
http.request_get("/") {|res| }
241241
}
242242
assert_match(/certificate verify failed/, ex.message)
243-
unless /mswin|mingw/ =~ RUBY_PLATFORM
244-
# on Windows, Errno::ECONNRESET will be raised, and it'll be eaten by
245-
# WEBrick
246-
@log_tester = lambda {|log|
247-
assert_equal(1, log.length)
248-
assert_match(/ERROR OpenSSL::SSL::SSLError:/, log[0])
249-
}
250-
end
251-
end
252-
253-
def test_identity_verify_failure
254-
# the certificate's subject has CN=localhost
255-
http = Net::HTTP.new(HOST_IP, config("port"))
256-
http.use_ssl = true
257-
http.cert_store = TEST_STORE
258-
@log_tester = lambda {|_| }
259-
ex = assert_raise(OpenSSL::SSL::SSLError){
260-
http.request_get("/") {|res| }
261-
}
262-
re_msg = /certificate verify failed|hostname \"#{HOST_IP}\" does not match/
263-
assert_match(re_msg, ex.message)
264243
end
265244

266245
def test_timeout_during_SSL_handshake
@@ -295,7 +274,7 @@ def test_min_version
295274
end
296275

297276
def test_max_version
298-
http = Net::HTTP.new(HOST_IP, config("port"))
277+
http = Net::HTTP.new(HOST, config("port"))
299278
http.use_ssl = true
300279
http.max_version = :SSL2
301280
http.verify_callback = Proc.new do |preverify_ok, store_ctx|
@@ -310,3 +289,42 @@ def test_max_version
310289
end
311290

312291
end if defined?(OpenSSL::SSL)
292+
293+
class TestNetHTTPSIdentityVerifyFailure < Test::Unit::TestCase
294+
include TestNetHTTPUtils
295+
296+
def self.read_fixture(key)
297+
File.read(File.expand_path("../fixtures/#{key}", __dir__))
298+
end
299+
300+
HOST = 'localhost'
301+
HOST_IP = '127.0.0.1'
302+
CA_CERT = OpenSSL::X509::Certificate.new(read_fixture("cacert.pem"))
303+
SERVER_KEY = OpenSSL::PKey.read(read_fixture("server.key"))
304+
SERVER_CERT = OpenSSL::X509::Certificate.new(read_fixture("server.crt"))
305+
DHPARAMS = OpenSSL::PKey::DH.new(read_fixture("dhparams.pem"))
306+
TEST_STORE = OpenSSL::X509::Store.new.tap {|s| s.add_cert(CA_CERT) }
307+
308+
CONFIG = {
309+
'host' => HOST_IP,
310+
'proxy_host' => nil,
311+
'proxy_port' => nil,
312+
'ssl_enable' => true,
313+
'ssl_certificate' => SERVER_CERT,
314+
'ssl_private_key' => SERVER_KEY,
315+
'ssl_tmp_dh_callback' => proc { DHPARAMS },
316+
}
317+
318+
def test_identity_verify_failure
319+
# the certificate's subject has CN=localhost
320+
http = Net::HTTP.new(HOST_IP, config("port"))
321+
http.use_ssl = true
322+
http.cert_store = TEST_STORE
323+
@log_tester = lambda {|_| }
324+
ex = assert_raise(OpenSSL::SSL::SSLError){
325+
http.request_get("/") {|res| }
326+
}
327+
re_msg = /certificate verify failed|hostname \"#{HOST_IP}\" does not match/
328+
assert_match(re_msg, ex.message)
329+
end
330+
end if defined?(OpenSSL::SSL)

0 commit comments

Comments
 (0)