From acd6bc2983db9045756ef1c2fe5ba31745ab8eca Mon Sep 17 00:00:00 2001 From: Joshua Nichols Date: Sat, 8 Aug 2009 19:13:55 -0400 Subject: [PATCH] Set open_timeout when doing Net::HTTP calls. - Avoids timing out if the socket doesn't open fast enough - Had to refactor Connection#http to call a new method, build_http, because active_resource/http_mock overrides this behavior. This seems to be the only way to test how the Net::HTTP is built. --- activeresource/lib/active_resource/connection.rb | 11 +++++++- activeresource/lib/active_resource/http_mock.rb | 1 + activeresource/test/connection_test.rb | 27 ++++++++++++++++++++++ 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/activeresource/lib/active_resource/connection.rb b/activeresource/lib/active_resource/connection.rb index 943c9ef..1d65b74 100644 --- a/activeresource/lib/active_resource/connection.rb +++ b/activeresource/lib/active_resource/connection.rb @@ -186,11 +186,18 @@ module ActiveResource # Creates new Net::HTTP instance for communication with # remote service and resources. def http + build_http + end + + def build_http http = Net::HTTP.new(@site.host, @site.port) 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. - http + # If timeout is not set, the default Net::HTTP timeout (60s) is used. + if @timeout + http.read_timeout = @timeout + http.open_timeout = @timeout + end end def default_header diff --git a/activeresource/lib/active_resource/http_mock.rb b/activeresource/lib/active_resource/http_mock.rb index 7d7e378..791e00d 100644 --- a/activeresource/lib/active_resource/http_mock.rb +++ b/activeresource/lib/active_resource/http_mock.rb @@ -48,6 +48,7 @@ module ActiveResource # end # class HttpMock + class Responder #:nodoc: def initialize(responses) @responses = responses diff --git a/activeresource/test/connection_test.rb b/activeresource/test/connection_test.rb index 831fbc4..2aaff47 100644 --- a/activeresource/test/connection_test.rb +++ b/activeresource/test/connection_test.rb @@ -175,6 +175,26 @@ class ConnectionTest < Test::Unit::TestCase assert_raise(ActiveResource::TimeoutError) { @conn.get('/people_timeout.xml') } end + def test_build_http_read_timeout + @http = build_stub_http + Net::HTTP.stubs(:new).returns(@http) + + @conn.timeout = 20 + @http.expects(:read_timeout=).with(20) + + @conn.send(:build_http) + end + + def test_build_http_open_timeout + @http = build_stub_http + Net::HTTP.stubs(:new).returns(@http) + + @conn.timeout = 20 + @http.expects(:open_timeout=).with(20) + + @conn.send(:build_http) + end + def test_accept_http_header @http = mock('new Net::HTTP') @conn.expects(:http).returns(@http) @@ -193,4 +213,11 @@ class ConnectionTest < Test::Unit::TestCase def handle_response(response) @conn.__send__(:handle_response, response) end + + def build_stub_http + stub('new Net::HTTP', :use_ssl= => nil, :use_ssl? => nil, + :verify_mode= => nil, :read_timeout= => nil, + :open_timeout= => nil) + + end end -- 1.6.4