1
0
Fork 0
mirror of https://github.com/jnunemaker/httparty synced 2023-03-27 23:23:07 -04:00

Merge pull request #278 from theasci/master

Added read_timeout and open_timeout options
This commit is contained in:
John Nunemaker 2014-03-13 09:59:44 -04:00
commit b3aa2d9181
5 changed files with 109 additions and 0 deletions

View file

@ -2,6 +2,14 @@ When /^I set my HTTParty timeout option to (\d+)$/ do |timeout|
@request_options[:timeout] = timeout.to_i @request_options[:timeout] = timeout.to_i
end 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| When /I call HTTParty#get with '(.*)'$/ do |url|
begin begin
@response_from_httparty = HTTParty.get("http://#{@host_and_port}#{url}", @request_options) @response_from_httparty = HTTParty.get("http://#{@host_and_port}#{url}", @request_options)

View file

@ -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

View file

@ -176,6 +176,28 @@ module HTTParty
default_options[:timeout] = t default_options[:timeout] = t
end 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. # Set an output stream for debugging, defaults to $stderr.
# The output stream is passed on to Net::HTTP#set_debug_output. # The output stream is passed on to Net::HTTP#set_debug_output.
# #

View file

@ -40,6 +40,8 @@ module HTTParty
# HTTParty::ConnectionAdapter#connection for examples of how they are used. # HTTParty::ConnectionAdapter#connection for examples of how they are used.
# Something are probably interesting are as follows: # Something are probably interesting are as follows:
# * :+timeout+: timeout in seconds # * :+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. # * :+debug_output+: see HTTParty::ClassMethods.debug_output.
# * :+pem+: contains pem data. see HTTParty::ClassMethods.pem. # * :+pem+: contains pem data. see HTTParty::ClassMethods.pem.
# * :+ssl_ca_file+: see HTTParty::ClassMethods.ssl_ca_file. # * :+ssl_ca_file+: see HTTParty::ClassMethods.ssl_ca_file.
@ -81,6 +83,14 @@ module HTTParty
http.read_timeout = options[:timeout] http.read_timeout = options[:timeout]
end 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] if options[:debug_output]
http.set_debug_output(options[:debug_output]) http.set_debug_output(options[:debug_output])
end end

View file

@ -153,6 +153,62 @@ describe HTTParty::ConnectionAdapter do
end end
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 context "when debug_output" do
let(:http) { Net::HTTP.new(uri) } let(:http) { Net::HTTP.new(uri) }
before do before do