Skip to content

Commit 56aaf3e

Browse files
committed
[Client] Fixes bug with headers in Faraday default class
This bug was present when using the default Transport class (Transport::HTTP::Faraday). If a user passed in headers when calling a request on the client, the implementation would use this header parameter and override the previously set headers in the connection. With this commit, we merge the headers.
1 parent e69c446 commit 56aaf3e

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

elasticsearch-transport/lib/elasticsearch/transport/transport/http/faraday.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,17 @@ class Faraday
3333
# @return [Response]
3434
# @see Transport::Base#perform_request
3535
#
36-
def perform_request(method, path, params={}, body=nil, headers=nil, opts={})
36+
def perform_request(method, path, params = {}, body = nil, headers = nil, opts = {})
3737
super do |connection, url|
38-
headers = headers || connection.connection.headers
38+
headers = if connection.connection.headers
39+
if !headers.nil?
40+
connection.connection.headers.merge(headers)
41+
else
42+
connection.connection.headers
43+
end
44+
else
45+
headers
46+
end
3947

4048
response = connection.connection.run_request(method.downcase.to_sym,
4149
url,

elasticsearch-transport/spec/elasticsearch/transport/client_spec.rb

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1423,7 +1423,7 @@
14231423
allow(client).to receive(:perform_request) { OpenStruct.new(body: '') }
14241424
expect { client.search(opaque_id: 'opaque_id') }.not_to raise_error
14251425
expect(client).to have_received(:perform_request)
1426-
.with('GET', '_search', { opaque_id: 'opaque_id' }, nil, {})
1426+
.with('GET', '_search', { opaque_id: 'opaque_id' }, nil, {})
14271427
end
14281428
end
14291429
end
@@ -1462,7 +1462,28 @@
14621462
allow(client).to receive(:perform_request) { OpenStruct.new(body: '') }
14631463
expect { client.search(headers: headers) }.not_to raise_error
14641464
expect(client).to have_received(:perform_request)
1465-
.with('GET', '_search', {}, nil, headers)
1465+
.with('GET', '_search', {}, nil, headers)
1466+
end
1467+
end
1468+
1469+
context 'when a header is set on an endpoint request and on initialization' do
1470+
let!(:client) do
1471+
described_class.new(
1472+
host: hosts,
1473+
transport_options: { headers: instance_headers }
1474+
)
1475+
end
1476+
let(:instance_headers) { { set_in_instantiation: 'header value' } }
1477+
let(:param_headers) {{'user-agent' => 'My Ruby Tests', 'set-on-method-call' => 'header value'}}
1478+
1479+
it 'performs the request with the header' do
1480+
expected_headers = client.transport.connections.connections.first.connection.headers.merge(param_headers)
1481+
1482+
expect_any_instance_of(Faraday::Connection)
1483+
.to receive(:run_request)
1484+
.with(:get, "http://#{hosts[0]}/_search", nil, expected_headers) { OpenStruct.new(body: '')}
1485+
1486+
client.search(headers: param_headers)
14661487
end
14671488
end
14681489
end

0 commit comments

Comments
 (0)