mirror of
https://github.com/lostisland/faraday.git
synced 2025-10-04 00:02:03 -04:00
RequestOptions#fetch_timeout => Adapter#request_timeout
This allows adapter internals and specs to treat request options as a plain hash.
This commit is contained in:
parent
8dae875906
commit
b35f6a2480
@ -65,5 +65,27 @@ module Faraday
|
||||
env.response.finish(env) unless env.parallel?
|
||||
env.response
|
||||
end
|
||||
|
||||
# Fetches either a read, write, or open timeout setting. Defaults to the
|
||||
# :timeout value if a more specific one is not given.
|
||||
#
|
||||
# @param type [Symbol] Describes which timeout setting to get: :read,
|
||||
# :write, or :open.
|
||||
# @param options [Hash] Hash containing Symbol keys like :timeout,
|
||||
# :read_timeout, :write_timeout, :open_timeout, or
|
||||
# :timeout
|
||||
#
|
||||
# @return [Integer, nil] Timeout duration in seconds, or nil if no timeout
|
||||
# has been set.
|
||||
def request_timeout(type, options)
|
||||
unless TIMEOUT_TYPES.include?(type)
|
||||
msg = "Expected :read, :write, :open. Got #{type.inspect} :("
|
||||
raise ArgumentError, msg
|
||||
end
|
||||
|
||||
options["#{type}_timeout".to_sym] || options[:timeout]
|
||||
end
|
||||
|
||||
TIMEOUT_TYPES = Set.new(%i[read write open])
|
||||
end
|
||||
end
|
||||
|
@ -71,8 +71,8 @@ module Faraday
|
||||
# Reads out timeout settings from env into options
|
||||
def configure_timeout(options, env)
|
||||
req = request_options(env)
|
||||
options[:inactivity_timeout] = req.fetch_timeout(:read)
|
||||
options[:connect_timeout] = req.fetch_timeout(:open)
|
||||
options[:inactivity_timeout] = request_timeout(:read, req)
|
||||
options[:connect_timeout] = request_timeout(:open, req)
|
||||
end
|
||||
|
||||
# Reads out compression header settings from env into options
|
||||
|
@ -90,15 +90,15 @@ module Faraday
|
||||
end
|
||||
|
||||
def amend_opts_with_timeouts!(opts, req)
|
||||
if (sec = req.fetch_timeout(:read))
|
||||
if (sec = request_timeout(:read, req))
|
||||
opts[:read_timeout] = sec
|
||||
end
|
||||
|
||||
if (sec = req.fetch_timeout(:write))
|
||||
if (sec = request_timeout(:write, req))
|
||||
opts[:write_timeout] = sec
|
||||
end
|
||||
|
||||
return unless (sec = req.fetch_timeout(:open))
|
||||
return unless (sec = request_timeout(:open, req))
|
||||
|
||||
opts[:connect_timeout] = sec
|
||||
end
|
||||
|
@ -165,14 +165,14 @@ module Faraday
|
||||
end
|
||||
|
||||
def configure_request(http, req)
|
||||
if (sec = req.fetch_timeout(:read))
|
||||
if (sec = request_timeout(:read, req))
|
||||
http.read_timeout = sec
|
||||
end
|
||||
if (sec = http.respond_to?(:write_timeout=) &&
|
||||
req.fetch_timeout(:write))
|
||||
request_timeout(:write, req))
|
||||
http.write_timeout = sec
|
||||
end
|
||||
if (sec = req.fetch_timeout(:open))
|
||||
if (sec = request_timeout(:open, req))
|
||||
http.open_timeout = sec
|
||||
end
|
||||
|
||||
|
@ -15,23 +15,6 @@ module Faraday
|
||||
end
|
||||
end
|
||||
|
||||
# Fetches either a read, write, or open timeout setting. Defaults to the
|
||||
# :timeout value if a more specific one is not given.
|
||||
#
|
||||
# @param type [Symbol] Describes which timeout setting to get: :read,
|
||||
# :write, or :open.
|
||||
#
|
||||
# @return [Integer, nil] Timeout duration in seconds, or nil if no timeout
|
||||
# has been set.
|
||||
def fetch_timeout(type)
|
||||
unless TIMEOUT_TYPES.include?(type)
|
||||
msg = "Expected :read, :write, :open. Got #{type.inspect} :("
|
||||
raise ArgumentError, msg
|
||||
end
|
||||
|
||||
self["#{type}_timeout".to_sym] || self[:timeout]
|
||||
end
|
||||
|
||||
def stream_response?
|
||||
on_data.is_a?(Proc)
|
||||
end
|
||||
|
@ -6,12 +6,10 @@ RSpec.describe Faraday::Adapter::EMHttp do
|
||||
|
||||
it_behaves_like 'an adapter'
|
||||
|
||||
let(:request) { Faraday::RequestOptions.new }
|
||||
|
||||
it 'allows to provide adapter specific configs' do
|
||||
url = URI('https://example.com:1234')
|
||||
adapter = described_class.new nil, inactivity_timeout: 20
|
||||
req = adapter.create_request(url: url, request: request)
|
||||
req = adapter.create_request(url: url, request: {})
|
||||
|
||||
expect(req.connopts.inactivity_timeout).to eq(20)
|
||||
end
|
||||
|
51
spec/faraday/adapter_spec.rb
Normal file
51
spec/faraday/adapter_spec.rb
Normal file
@ -0,0 +1,51 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
RSpec.describe Faraday::Adapter do
|
||||
let(:adapter) { Faraday::Adapter.new }
|
||||
let(:request) { {} }
|
||||
|
||||
context '#request_timeout' do
|
||||
it 'gets :read timeout' do
|
||||
expect(timeout(:read)).to eq(nil)
|
||||
|
||||
request[:timeout] = 5
|
||||
request[:write_timeout] = 1
|
||||
|
||||
expect(timeout(:read)).to eq(5)
|
||||
|
||||
request[:read_timeout] = 2
|
||||
|
||||
expect(timeout(:read)).to eq(2)
|
||||
end
|
||||
|
||||
it 'gets :open timeout' do
|
||||
expect(timeout(:open)).to eq(nil)
|
||||
|
||||
request[:timeout] = 5
|
||||
request[:write_timeout] = 1
|
||||
|
||||
expect(timeout(:open)).to eq(5)
|
||||
|
||||
request[:open_timeout] = 2
|
||||
|
||||
expect(timeout(:open)).to eq(2)
|
||||
end
|
||||
|
||||
it 'gets :write timeout' do
|
||||
expect(timeout(:write)).to eq(nil)
|
||||
|
||||
request[:timeout] = 5
|
||||
request[:read_timeout] = 1
|
||||
|
||||
expect(timeout(:write)).to eq(5)
|
||||
|
||||
request[:write_timeout] = 2
|
||||
|
||||
expect(timeout(:write)).to eq(2)
|
||||
end
|
||||
|
||||
def timeout(type)
|
||||
adapter.send(:request_timeout, type, request)
|
||||
end
|
||||
end
|
||||
end
|
@ -3,47 +3,6 @@
|
||||
RSpec.describe Faraday::RequestOptions do
|
||||
subject(:options) { Faraday::RequestOptions.new }
|
||||
|
||||
context '#fetch_timeout' do
|
||||
it 'gets :read timeout' do
|
||||
expect(options.fetch_timeout(:read)).to eq(nil)
|
||||
|
||||
options[:timeout] = 5
|
||||
options[:write_timeout] = 1
|
||||
|
||||
expect(options.fetch_timeout(:read)).to eq(5)
|
||||
|
||||
options[:read_timeout] = 2
|
||||
|
||||
expect(options.fetch_timeout(:read)).to eq(2)
|
||||
end
|
||||
|
||||
it 'gets :open timeout' do
|
||||
expect(options.fetch_timeout(:open)).to eq(nil)
|
||||
|
||||
options[:timeout] = 5
|
||||
options[:write_timeout] = 1
|
||||
|
||||
expect(options.fetch_timeout(:open)).to eq(5)
|
||||
|
||||
options[:open_timeout] = 2
|
||||
|
||||
expect(options.fetch_timeout(:open)).to eq(2)
|
||||
end
|
||||
|
||||
it 'gets :write timeout' do
|
||||
expect(options.fetch_timeout(:write)).to eq(nil)
|
||||
|
||||
options[:timeout] = 5
|
||||
options[:read_timeout] = 1
|
||||
|
||||
expect(options.fetch_timeout(:write)).to eq(5)
|
||||
|
||||
options[:write_timeout] = 2
|
||||
|
||||
expect(options.fetch_timeout(:write)).to eq(2)
|
||||
end
|
||||
end
|
||||
|
||||
it 'allows to set the request proxy' do
|
||||
expect(options.proxy).to be_nil
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user