diff --git a/lib/httparty/request.rb b/lib/httparty/request.rb index 745016f..9c7a051 100644 --- a/lib/httparty/request.rb +++ b/lib/httparty/request.rb @@ -41,12 +41,24 @@ module HTTParty end private + # def http + # http = Net::HTTP.new(uri.host, uri.port, options[:http_proxyaddr], options[:http_proxyport]) + # http.use_ssl = (uri.port == 443) + # http.verify_mode = OpenSSL::SSL::VERIFY_NONE + # http + # end + def http http = Net::HTTP.new(uri.host, uri.port, options[:http_proxyaddr], options[:http_proxyport]) http.use_ssl = (uri.port == 443) http.verify_mode = OpenSSL::SSL::VERIFY_NONE + if options[:timeout] && options[:timeout].is_a?(Integer) + http.open_timeout = options[:timeout] + http.read_timeout = options[:timeout] + end http end + def body options[:body].is_a?(Hash) ? options[:body].to_params : options[:body] diff --git a/spec/httparty/request_spec.rb b/spec/httparty/request_spec.rb index 2961010..237e9a1 100644 --- a/spec/httparty/request_spec.rb +++ b/spec/httparty/request_spec.rb @@ -46,6 +46,23 @@ describe HTTParty::Request do @request.send(:setup_raw_request) @request.instance_variable_get(:@raw_request)['authorization'].should_not be_nil end + + it "should change timeout when configured (if an Integer)" do + @request.send(:http).open_timeout.should be_nil + @request.send(:http).read_timeout.should == 60 + + invalid_timeout = "invalid" + invalid_timeout.is_a?(Integer).should be_false + @request.options[:timeout] = invalid_timeout + @request.send(:http).open_timeout.should be_nil + @request.send(:http).read_timeout.should == 60 + + timeout = 30 + timeout.is_a?(Integer).should be_true + @request.options[:timeout] = timeout + @request.send(:http).open_timeout.should == timeout + @request.send(:http).read_timeout.should == timeout + end end describe '#format_from_mimetype' do