Browse files

Setting connection timeout also affects Net::HTTP open_timeout.

[#2947 state:resolved]
  • Loading branch information...
1 parent ee8fe3a commit 83cc7de2a47df199348cc47d2794a3f30ad4f4fb @jeremy jeremy committed Aug 9, 2009
Showing with 30 additions and 7 deletions.
  1. +19 −7 activeresource/lib/active_resource/connection.rb
  2. +11 −0 activeresource/test/connection_test.rb
View
26 activeresource/lib/active_resource/connection.rb
@@ -191,15 +191,27 @@ def handle_response(response)
# Creates new Net::HTTP instance for communication with
# remote service and resources.
def http
- http =
- if @proxy
- Net::HTTP.new(@site.host, @site.port, @proxy.host, @proxy.port, @proxy.user, @proxy.password)
- else
- Net::HTTP.new(@site.host, @site.port)
- end
+ configure_http(new_http)
+ end
+
+ def new_http
+ if @proxy
+ Net::HTTP.new(@site.host, @site.port, @proxy.host, @proxy.port, @proxy.user, @proxy.password)
+ else
+ Net::HTTP.new(@site.host, @site.port)
+ end
+ end
+
+ def configure_http(http)
http.use_ssl = @site.is_a?(URI::HTTPS)
http.verify_mode = OpenSSL::SSL::VERIFY_NONE if http.use_ssl?
- http.read_timeout = @timeout if @timeout # If timeout is not set, the default Net::HTTP timeout (60s) is used.
+
+ # Net::HTTP timeouts default to 60 seconds.
+ if @timeout
+ http.open_timeout = @timeout
+ http.read_timeout = @timeout
+ end
+
http
end
View
11 activeresource/test/connection_test.rb
@@ -185,6 +185,17 @@ def test_timeout
assert_raise(ActiveResource::TimeoutError) { @conn.get('/people_timeout.xml') }
end
+ def test_setting_timeout
+ http = Net::HTTP.new('')
+
+ [10, 20].each do |timeout|
+ @conn.timeout = timeout
+ @conn.send(:configure_http, http)
+ assert_equal timeout, http.open_timeout
+ assert_equal timeout, http.read_timeout
+ end
+ end
+
def test_accept_http_header
@http = mock('new Net::HTTP')
@conn.expects(:http).returns(@http)

0 comments on commit 83cc7de

Please sign in to comment.