Skip to content

Commit

Permalink
Merge pull request #5 from cyx/add-read-timeout
Browse files Browse the repository at this point in the history
Add read_timeout / open_timeout pass through
  • Loading branch information
cyx authored Mar 31, 2017
2 parents 33649d9 + e4bfbee commit 36eec0e
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
11 changes: 7 additions & 4 deletions lib/requests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ def self.request(method, url,
data: nil,
params: nil,
auth: nil,
proxy: nil)
proxy: nil,
options: {})

uri = URI.parse(url)
uri.query = encode_www_form(params) if params
Expand All @@ -32,7 +33,7 @@ def self.request(method, url,
basic_auth(headers, *auth) if auth

proxy = proxy.to_h.values_at(:host, :port, :user, :password)
response = Net::HTTP.start(uri.host, uri.port, *proxy, opts(uri)) do |http|
response = Net::HTTP.start(uri.host, uri.port, *proxy, opts(uri, options)) do |http|
http.send_request(method, uri, body, headers)
end

Expand All @@ -48,11 +49,13 @@ def self.encode_www_form(params)
URI.encode_www_form(params)
end

def self.opts(uri)
def self.opts(uri, options)
if uri.scheme == 'https'
{ use_ssl: true,
verify_mode: OpenSSL::SSL::VERIFY_PEER,
ca_file: CA_FILE
ca_file: CA_FILE,
read_timeout: options[:read_timeout],
open_timeout: options[:open_timeout],
}
end
end
Expand Down
2 changes: 1 addition & 1 deletion tests/proxy_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
assert_equal ['application/json'], r.headers['content-type']
assert(r.json['args'] && r.json['args']['foo'] == 'bar')

assert_equal ["1.1 0.0.0.0:#{port}"], r.headers['via']
assert_equal ["1.1 vegur, 1.1 0.0.0.0:#{port}"], r.headers['via']

proxy.shutdown
end
34 changes: 34 additions & 0 deletions tests/requests_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,37 @@
assert_equal Net::HTTPNotFound, e.response.class
end
end

test 'read timeout' do
begin
Requests.get('http://httpbin.org:10000', options: { read_timeout: 1 })
rescue => err
assert err.kind_of?(Errno::ECONNREFUSED)
end
end

test 'read timeout not failing' do
begin
Requests.get('http://httpbin.org/get', options: { read_timeout: 30 })
rescue => err
flunk(err)
end
end

test 'open timeout' do
begin
Requests.get('http://httpbin.org:10000', options: { open_timeout: 1 })
rescue => err
assert err.kind_of?(Errno::ECONNREFUSED)
else
flunk("expected exception")
end
end

test 'open timeout not failing' do
begin
Requests.get('http://httpbin.org/get', options: { open_timeout: 30 })
rescue => err
flunk(err)
end
end

2 comments on commit 36eec0e

@jvillarejo
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great! Thank you very much for this!

@cyx
Copy link
Owner Author

@cyx cyx commented on 36eec0e Apr 6, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Please sign in to comment.