Skip to content

Commit

Permalink
Merge pull request #278 from theasci/master
Browse files Browse the repository at this point in the history
Added read_timeout and open_timeout options
  • Loading branch information
greatuserongithub committed Mar 13, 2014
2 parents 2ea1499 + c0dc069 commit b3aa2d9
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 0 deletions.
8 changes: 8 additions & 0 deletions features/steps/httparty_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@
@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)
Expand Down
13 changes: 13 additions & 0 deletions features/supports_read_timeout_option.feature
Original file line number Diff line number Diff line change
@@ -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 '<h1>Some HTML</h1>'
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
22 changes: 22 additions & 0 deletions lib/httparty.rb
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,28 @@ def default_timeout(t)
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.
#
Expand Down
10 changes: 10 additions & 0 deletions lib/httparty/connection_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -81,6 +83,14 @@ def connection
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
Expand Down
56 changes: 56 additions & 0 deletions spec/httparty/connection_adapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,62 @@
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
Expand Down

0 comments on commit b3aa2d9

Please sign in to comment.