Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed error messages when a proxy server is down. Fixes #30

  • Loading branch information...
commit 9fc2681aca6cf698549b7a7a0819d7da97bbb49b 1 parent 327e826
@drbrain authored
View
2  History.txt
@@ -16,6 +16,8 @@
* #shutdown now works even if no connections were made. Pull Request #24 by
James Tucker.
* Updated test RSA key size to 1024 bits. Bug #25 by Gunnar Wolf.
+ * The correct host:port are shown in the exception when a proxy connection
+ fails. Bug #30 by glebtv.
=== 2.7 / 2012-06-06
View
10 lib/net/http/persistent.rb
@@ -571,9 +571,15 @@ def connection_for uri
connection
rescue Errno::ECONNREFUSED
- raise Error, "connection refused: #{connection.address}:#{connection.port}"
+ address = connection.proxy_address || connection.address
+ port = connection.proxy_port || connection.port
+
+ raise Error, "connection refused: #{address}:#{port}"
rescue Errno::EHOSTDOWN
- raise Error, "host down: #{connection.address}:#{connection.port}"
+ address = connection.proxy_address || connection.address
+ port = connection.proxy_port || connection.port
+
+ raise Error, "host down: #{address}:#{port}"
end
##
View
97 test/test_net_http_persistent.rb
@@ -4,40 +4,19 @@
require 'openssl'
require 'stringio'
-class Net::HTTP
- alias orig_connect connect
-
- def test_connect
- unless use_ssl? then
- io = Object.new
- def io.setsockopt(*a) @setsockopts ||= []; @setsockopts << a end
-
- @socket = Net::BufferedIO.new io
+module Net::HTTP::Persistent::TestConnect
+ def self.included mod
+ mod.send :alias_method, :orig_connect, :connect
- return
+ def mod.use_connect which
+ self.send :remove_method, :connect
+ self.send :alias_method, :connect, which
end
-
- io = open '/dev/null'
- def io.setsockopt(*a) @setsockopts ||= []; @setsockopts << a end
-
- @ssl_context ||= OpenSSL::SSL::SSLContext.new
-
- @ssl_context.verify_mode = OpenSSL::SSL::VERIFY_PEER unless
- @ssl_context.verify_mode
-
- s = OpenSSL::SSL::SSLSocket.new io, @ssl_context
-
- @socket = Net::BufferedIO.new s
end
- def self.use_connect which
- self.send :remove_method, :connect
- self.send :alias_method, :connect, which
+ def host_down_connect
+ raise Errno::EHOSTDOWN
end
-end
-
-class Net::HTTP::Persistent::SSLReuse
- alias orig_connect connect
def test_connect
unless use_ssl? then
@@ -62,12 +41,19 @@ def io.setsockopt(*a) @setsockopts ||= []; @setsockopts << a end
@socket = Net::BufferedIO.new s
end
- def self.use_connect which
- self.send :remove_method, :connect
- self.send :alias_method, :connect, which
+ def refused_connect
+ raise Errno::ECONNREFUSED
end
end
+class Net::HTTP
+ include Net::HTTP::Persistent::TestConnect
+end
+
+class Net::HTTP::Persistent::SSLReuse
+ include Net::HTTP::Persistent::TestConnect
+end
+
class TestNetHttpPersistent < MiniTest::Unit::TestCase
def setup
@@ -364,6 +350,17 @@ def test_connection_for_cached_expired
'connection not reset due to timeout'
end
+ def test_connection_for_refused
+ Net::HTTP.use_connect :refused_connect
+ Net::HTTP::Persistent::SSLReuse.use_connect :refused_connect
+
+ e = assert_raises Net::HTTP::Persistent::Error do
+ @http.connection_for @uri
+ end
+
+ assert_equal 'connection refused: example.com:80', e.message
+ end
+
def test_connection_for_finished_ssl
uri = URI.parse 'https://example.com/path'
c = @http.connection_for uri
@@ -390,7 +387,7 @@ def cached.started?; false end
@http.connection_for @uri
end
- assert_match %r%host down%, e.message
+ assert_equal 'host down: example.com:80', e.message
end
def test_connection_for_http_class_with_fakeweb
@@ -460,6 +457,40 @@ def test_connection_for_proxy
assert_same c, conns[1]['example.com:80:proxy.example:80:johndoe:muffins']
end
+ def test_connection_for_proxy_host_down
+ Net::HTTP.use_connect :host_down_connect
+ Net::HTTP::Persistent::SSLReuse.use_connect :host_down_connect
+
+ uri = URI.parse 'http://proxy.example'
+ uri.user = 'johndoe'
+ uri.password = 'muffins'
+
+ http = Net::HTTP::Persistent.new nil, uri
+
+ e = assert_raises Net::HTTP::Persistent::Error do
+ http.connection_for @uri
+ end
+
+ assert_equal 'host down: proxy.example:80', e.message
+ end
+
+ def test_connection_for_proxy_refused
+ Net::HTTP.use_connect :refused_connect
+ Net::HTTP::Persistent::SSLReuse.use_connect :refused_connect
+
+ uri = URI.parse 'http://proxy.example'
+ uri.user = 'johndoe'
+ uri.password = 'muffins'
+
+ http = Net::HTTP::Persistent.new nil, uri
+
+ e = assert_raises Net::HTTP::Persistent::Error do
+ http.connection_for @uri
+ end
+
+ assert_equal 'connection refused: proxy.example:80', e.message
+ end
+
def test_connection_for_no_proxy
uri = URI.parse 'http://proxy.example'
uri.user = 'johndoe'
Please sign in to comment.
Something went wrong with that request. Please try again.