Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add default adapter options #1382

Merged
merged 6 commits into from
Jan 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/faraday.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ class << self
# @return [Symbol] the new default_adapter.
attr_reader :default_adapter

# Option for the default_adapter
# @return [Hash] default_adapter options
attr_accessor :default_adapter_options

# Documented below, see default_connection
attr_writer :default_connection

Expand Down Expand Up @@ -149,4 +153,5 @@ def method_missing(name, *args, &block)
self.root_path = File.expand_path __dir__
self.lib_path = File.expand_path 'faraday', __dir__
self.default_adapter = :net_http
self.default_adapter_options = {}
end
2 changes: 1 addition & 1 deletion lib/faraday/rack_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def initialize_dup(original)
def build
raise_if_locked
block_given? ? yield(self) : request(:url_encoded)
adapter(Faraday.default_adapter) unless @adapter
adapter(Faraday.default_adapter, **Faraday.default_adapter_options) unless @adapter
end

def [](idx)
Expand Down
27 changes: 27 additions & 0 deletions spec/faraday/rack_builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,33 @@ class Banana < Handler
end
end

context 'when adapter is added with named options' do
after { Faraday.default_adapter_options = {} }
let(:conn) { Faraday::Connection.new {} }

let(:cat_adapter) do
Class.new(Faraday::Adapter) do
attr_accessor :name

def initialize(app, name:)
super(app)
@name = name
end
end
end

let(:cat) { subject.adapter.build }

it 'adds a handler to construct adapter with named options' do
Faraday.default_adapter = cat_adapter
Faraday.default_adapter_options = { name: 'Chloe' }
expect { cat }.to_not output(
/warning: Using the last argument as keyword parameters is deprecated/
).to_stderr
expect(cat.name).to eq 'Chloe'
end
end

context 'when middleware is added with named arguments' do
let(:conn) { Faraday::Connection.new {} }

Expand Down