diff --git a/features/steps/httparty_steps.rb b/features/steps/httparty_steps.rb
index e0aea39..88a5956 100644
--- a/features/steps/httparty_steps.rb
+++ b/features/steps/httparty_steps.rb
@@ -2,6 +2,14 @@ When /^I set my HTTParty timeout option to (\d+)$/ do |timeout|
@request_options[:timeout] = timeout.to_i
end
+When /^I set my HTTParty open_timeout option to (\d+)$/ do |timeout|
+ @request_options[:open_timeout] = timeout.to_i
+end
+
+When /^I set my HTTParty read_timeout option to (\d+)$/ do |timeout|
+ @request_options[:read_timeout] = timeout.to_i
+end
+
When /I call HTTParty#get with '(.*)'$/ do |url|
begin
@response_from_httparty = HTTParty.get("http://#{@host_and_port}#{url}", @request_options)
diff --git a/features/supports_read_timeout_option.feature b/features/supports_read_timeout_option.feature
new file mode 100644
index 0000000..58b9d37
--- /dev/null
+++ b/features/supports_read_timeout_option.feature
@@ -0,0 +1,13 @@
+Feature: Supports the read timeout option
+ In order to handle inappropriately slow response times
+ As a developer
+ I want my request to raise an exception after my specified read_timeout as elapsed
+
+ Scenario: A long running response
+ Given a remote service that returns '
Some HTML
'
+ And that service is accessed at the path '/long_running_service.html'
+ And that service takes 2 seconds to generate a response
+ When I set my HTTParty read_timeout option to 1
+ And I call HTTParty#get with '/long_running_service.html'
+ Then it should raise a Timeout::Error exception
+ And I wait for the server to recover
diff --git a/lib/httparty.rb b/lib/httparty.rb
index 04344b7..b96789b 100644
--- a/lib/httparty.rb
+++ b/lib/httparty.rb
@@ -176,6 +176,28 @@ module HTTParty
default_options[:timeout] = t
end
+ # Allows setting a default open_timeout for all HTTP calls in seconds
+ #
+ # class Foo
+ # include HTTParty
+ # open_timeout 10
+ # end
+ def open_timeout(t)
+ raise ArgumentError, 'open_timeout must be an integer or float' unless t && (t.is_a?(Integer) || t.is_a?(Float))
+ default_options[:open_timeout] = t
+ end
+
+ # Allows setting a default read_timeout for all HTTP calls in seconds
+ #
+ # class Foo
+ # include HTTParty
+ # read_timeout 10
+ # end
+ def read_timeout(t)
+ raise ArgumentError, 'read_timeout must be an integer or float' unless t && (t.is_a?(Integer) || t.is_a?(Float))
+ default_options[:read_timeout] = t
+ end
+
# Set an output stream for debugging, defaults to $stderr.
# The output stream is passed on to Net::HTTP#set_debug_output.
#
diff --git a/lib/httparty/connection_adapter.rb b/lib/httparty/connection_adapter.rb
index 617d941..e13bb09 100644
--- a/lib/httparty/connection_adapter.rb
+++ b/lib/httparty/connection_adapter.rb
@@ -40,6 +40,8 @@ module HTTParty
# HTTParty::ConnectionAdapter#connection for examples of how they are used.
# Something are probably interesting are as follows:
# * :+timeout+: timeout in seconds
+ # * :+open_timeout+: http connection open_timeout in seconds, overrides timeout if set
+ # * :+read_timeout+: http connection read_timeout in seconds, overrides timeout if set
# * :+debug_output+: see HTTParty::ClassMethods.debug_output.
# * :+pem+: contains pem data. see HTTParty::ClassMethods.pem.
# * :+ssl_ca_file+: see HTTParty::ClassMethods.ssl_ca_file.
@@ -81,6 +83,14 @@ module HTTParty
http.read_timeout = options[:timeout]
end
+ if options[:read_timeout] && (options[:read_timeout].is_a?(Integer) || options[:read_timeout].is_a?(Float))
+ http.read_timeout = options[:read_timeout]
+ end
+
+ if options[:open_timeout] && (options[:open_timeout].is_a?(Integer) || options[:open_timeout].is_a?(Float))
+ http.open_timeout = options[:open_timeout]
+ end
+
if options[:debug_output]
http.set_debug_output(options[:debug_output])
end
diff --git a/spec/httparty/connection_adapter_spec.rb b/spec/httparty/connection_adapter_spec.rb
index 12650d3..12b147a 100644
--- a/spec/httparty/connection_adapter_spec.rb
+++ b/spec/httparty/connection_adapter_spec.rb
@@ -153,6 +153,62 @@ describe HTTParty::ConnectionAdapter do
end
end
+ context "when timeout is not set and read_timeout is set to 6 seconds" do
+ let(:options) { {:read_timeout => 6} }
+
+ its(:read_timeout) { should == 6 }
+
+ it "should not set the open_timeout" do
+ http = mock("http", :null_object => true)
+ http.should_not_receive(:open_timeout=)
+ Net::HTTP.stub(:new => http)
+ adapter.connection
+ end
+ end
+
+ context "when timeout is set and read_timeout is set to 6 seconds" do
+ let(:options) { {:timeout => 5, :read_timeout => 6} }
+
+ its(:open_timeout) { should == 5 }
+ its(:read_timeout) { should == 6 }
+
+ it "should override the timeout option" do
+ http = mock("http", :null_object => true)
+ http.should_receive(:open_timeout=)
+ http.should_receive(:read_timeout=).twice
+ Net::HTTP.stub(:new => http)
+ adapter.connection
+ end
+ end
+
+ context "when timeout is not set and open_timeout is set to 7 seconds" do
+ let(:options) { {:open_timeout => 7} }
+
+ its(:open_timeout) { should == 7 }
+
+ it "should not set the read_timeout" do
+ http = mock("http", :null_object => true)
+ http.should_not_receive(:read_timeout=)
+ Net::HTTP.stub(:new => http)
+ adapter.connection
+ end
+ end
+
+ context "when timeout is set and open_timeout is set to 7 seconds" do
+ let(:options) { {:timeout => 5, :open_timeout => 7} }
+
+ its(:open_timeout) { should == 7 }
+ its(:read_timeout) { should == 5 }
+
+ it "should override the timeout option" do
+ http = mock("http", :null_object => true)
+ http.should_receive(:open_timeout=).twice
+ http.should_receive(:read_timeout=)
+ Net::HTTP.stub(:new => http)
+ adapter.connection
+ end
+ end
+
context "when debug_output" do
let(:http) { Net::HTTP.new(uri) }
before do