diff --git a/test/open-uri/test_open-uri.rb b/test/open-uri/test_open-uri.rb index 85f2880dba..8fb1f06a82 100644 --- a/test/open-uri/test_open-uri.rb +++ b/test/open-uri/test_open-uri.rb @@ -119,11 +119,11 @@ class TestOpenURI < Test::Unit::TestCase def test_proxy with_http {|srv, dr, url| prxy = WEBrick::HTTPProxyServer.new({ - :ServerType => Thread, - :Logger => WEBrick::Log.new(NullLog), - :AccessLog => [[NullLog, ""]], - :BindAddress => '127.0.0.1', - :Port => 0}) + :ServerType => Thread, + :Logger => WEBrick::Log.new(NullLog), + :AccessLog => [[sio=StringIO.new, WEBrick::AccessLog::COMMON_LOG_FORMAT]], + :BindAddress => '127.0.0.1', + :Port => 0}) _, p_port, _, p_host = prxy.listeners[0].addr begin th = prxy.start @@ -132,17 +132,23 @@ class TestOpenURI < Test::Unit::TestCase assert_equal("200", f.status[0]) assert_equal("proxy", f.read) } + assert_match(/#{Regexp.quote url}/, sio.string) + sio.truncate(0); sio.rewind open("#{url}/proxy", :proxy=>URI("http://#{p_host}:#{p_port}/")) {|f| assert_equal("200", f.status[0]) assert_equal("proxy", f.read) } + assert_match(/#{Regexp.quote url}/, sio.string) + sio.truncate(0); sio.rewind open("#{url}/proxy", :proxy=>nil) {|f| assert_equal("200", f.status[0]) assert_equal("proxy", f.read) } + assert_equal("", sio.string) assert_raise(ArgumentError) { open("#{url}/proxy", :proxy=>:invalid) {} } + assert_equal("", sio.string) ensure prxy.shutdown end @@ -154,7 +160,7 @@ class TestOpenURI < Test::Unit::TestCase prxy = WEBrick::HTTPProxyServer.new({ :ServerType => Thread, :Logger => WEBrick::Log.new(NullLog), - :AccessLog => [[NullLog, ""]], + :AccessLog => [[sio=StringIO.new, WEBrick::AccessLog::COMMON_LOG_FORMAT]], :ProxyAuthProc => lambda {|req, res| if req["Proxy-Authorization"] != "Basic #{['user:pass'].pack('m').chomp}" raise WEBrick::HTTPStatus::ProxyAuthenticationRequired @@ -169,15 +175,20 @@ class TestOpenURI < Test::Unit::TestCase open("#{dr}/proxy", "w") {|f| f << "proxy" } exc = assert_raise(OpenURI::HTTPError) { open("#{url}/proxy", :proxy=>p_url) {} } assert_equal("407", exc.io.status[0]) + assert_match(/#{Regexp.quote url}/, sio.string) + sio.truncate(0); sio.rewind open("#{url}/proxy", :proxy_http_basic_authentication=>[p_url, "user", "pass"]) {|f| assert_equal("200", f.status[0]) assert_equal("proxy", f.read) } + assert_match(/#{Regexp.quote url}/, sio.string) + sio.truncate(0); sio.rewind assert_raise(ArgumentError) { open("#{url}/proxy", :proxy_http_basic_authentication=>[true, "user", "pass"]) {} } + assert_equal("", sio.string) ensure prxy.shutdown end diff --git a/test/open-uri/test_ssl.rb b/test/open-uri/test_ssl.rb index 28c9ecf46f..29c794a598 100644 --- a/test/open-uri/test_ssl.rb +++ b/test/open-uri/test_ssl.rb @@ -1,8 +1,10 @@ require 'test/unit' require 'open-uri' require 'openssl' +require 'stringio' require 'webrick' require 'webrick/https' +require 'webrick/httpproxy' class TestOpenURISSL < Test::Unit::TestCase @@ -59,6 +61,31 @@ class TestOpenURISSL < Test::Unit::TestCase } end + def test_proxy + with_https {|srv, dr, url| + cacert_filename = "#{dr}/cacert.pem" + open(cacert_filename, "w") {|f| f << CA_CERT } + prxy = WEBrick::HTTPProxyServer.new({ + :ServerType => Thread, + :Logger => WEBrick::Log.new(NullLog), + :AccessLog => [[sio=StringIO.new, WEBrick::AccessLog::COMMON_LOG_FORMAT]], + :BindAddress => '127.0.0.1', + :Port => 0}) + _, p_port, _, p_host = prxy.listeners[0].addr + begin + th = prxy.start + open("#{dr}/proxy", "w") {|f| f << "proxy" } + open("#{url}/proxy", :proxy=>"http://#{p_host}:#{p_port}/", :ssl_ca_cert => cacert_filename) {|f| + assert_equal("200", f.status[0]) + assert_equal("proxy", f.read) + } + assert_match(%r[CONNECT #{url.sub(%r{\Ahttps://}, '')} ], sio.string) + ensure + prxy.shutdown + end + } + end + end # mkdir demoCA demoCA/private demoCA/newcerts