Skip to content

Replace Hash#merge with Utils#deep_merge for connection options #1343

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

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/faraday.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class << self
# params: { page: 1 }
# # => Faraday::Connection to http://faraday.com?page=1
def new(url = nil, options = {}, &block)
options = default_connection_options.merge(options)
options = Utils.deep_merge(default_connection_options, options)
Faraday::Connection.new(url, options, &block)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/faraday/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def initialize(url = nil, options = nil)
options = ConnectionOptions.from(options)

if url.is_a?(Hash) || url.is_a?(ConnectionOptions)
options = options.merge(url)
options = Utils.deep_merge(options, url)
url = options.url
end

Expand Down
2 changes: 1 addition & 1 deletion lib/faraday/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def normalize_path(url)
# Recursive hash update
def deep_merge!(target, hash)
hash.each do |key, value|
target[key] = if value.is_a?(Hash) && target[key].is_a?(Hash)
target[key] = if value.is_a?(Hash) && (target[key].is_a?(Hash) || target[key].is_a?(Options))
deep_merge(target[key], value)
else
value
Expand Down
18 changes: 18 additions & 0 deletions spec/faraday/connection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,24 @@ def decode(params)

it_behaves_like 'default connection options'
end

context 'preserving a user_agent assigned via default_conncetion_options' do
context 'when url is a Hash' do
let(:conn) { Faraday.new(url: 'http://example.co', headers: { 'CustomHeader' => 'CustomValue' }) }

before { Faraday.default_connection_options = { headers: { user_agent: 'My Agent 1.2' } } }
Copy link
Member

@iMacTia iMacTia Nov 30, 2021

Choose a reason for hiding this comment

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

I just realised there's no rollback of the original value after these test have run 😄.
I'll be fixing this on the main branch, so no worries at all, but please keep this in mind next time you write tests for a global configuration 👍


it { expect(conn.headers).to eq('CustomHeader' => 'CustomValue', 'User-Agent' => 'My Agent 1.2') }
end

context 'when url is a String' do
let(:conn) { Faraday.new('http://example.co', headers: { 'CustomHeader' => 'CustomValue' }) }

before { Faraday.default_connection_options = { headers: { user_agent: 'My Agent 1.2' } } }

it { expect(conn.headers).to eq('CustomHeader' => 'CustomValue', 'User-Agent' => 'My Agent 1.2') }
end
end
end

describe 'request params' do
Expand Down
61 changes: 61 additions & 0 deletions spec/faraday/utils_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,65 @@
expect(headers).not_to have_key('authorization')
end
end

describe '.deep_merge!' do
let(:connection_options) { Faraday::ConnectionOptions.new }
let(:url) do
{
url: 'http://example.com/abc',
headers: { 'Mime-Version' => '1.0' },
request: { oauth: { consumer_key: 'anonymous' } },
ssl: { version: '2' }
}
end

it 'recursively merges the headers' do
connection_options.headers = { user_agent: 'My Agent 1.0' }
deep_merge = Faraday::Utils.deep_merge!(connection_options, url)

expect(deep_merge.headers).to eq('Mime-Version' => '1.0', user_agent: 'My Agent 1.0')
end

context 'when a target hash has an Options Struct value' do
let(:request) do
{
params_encoder: nil,
proxy: nil,
bind: nil,
timeout: nil,
open_timeout: nil,
read_timeout: nil,
write_timeout: nil,
boundary: nil,
oauth: { consumer_key: 'anonymous' },
context: nil,
on_data: nil
}
end
let(:ssl) do
{
verify: nil,
ca_file: nil,
ca_path: nil,
verify_mode: nil,
cert_store: nil,
client_cert: nil,
client_key: nil,
certificate: nil,
private_key: nil,
verify_depth: nil,
version: '2',
min_version: nil,
max_version: nil
}
end

it 'does not overwrite an Options Struct value' do
deep_merge = Faraday::Utils.deep_merge!(connection_options, url)

expect(deep_merge.request.to_h).to eq(request)
expect(deep_merge.ssl.to_h).to eq(ssl)
end
end
end
end