Skip to content

Commit 661bbf9

Browse files
committed
[Gem] Updates setting content-type, accept to not overwrite user set headers
1 parent 4899c52 commit 661bbf9

File tree

2 files changed

+78
-5
lines changed

2 files changed

+78
-5
lines changed

elasticsearch/lib/elasticsearch.rb

+9-5
Original file line numberDiff line numberDiff line change
@@ -180,11 +180,15 @@ def set_user_agent!(arguments)
180180
end
181181

182182
def set_content_type!(arguments)
183-
headers = {
184-
'content-type' => 'application/vnd.elasticsearch+json; compatible-with=9',
185-
'accept' => 'application/vnd.elasticsearch+json; compatible-with=9'
186-
}
187-
set_header(headers, arguments)
183+
headers = {}
184+
user_headers = arguments&.[](:transport_options)&.[](:headers)
185+
unless user_headers&.keys&.detect { |h| h =~ /content-?_?type/ }
186+
headers['content-type'] = 'application/vnd.elasticsearch+json; compatible-with=9'
187+
end
188+
unless user_headers&.keys&.detect { |h| h =~ /accept/ }
189+
headers['accept'] = 'application/vnd.elasticsearch+json; compatible-with=9'
190+
end
191+
set_header(headers, arguments) unless headers.empty?
188192
end
189193

190194
def set_header(header, arguments)

elasticsearch/spec/unit/headers_spec.rb

+69
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,73 @@
5252
client.search(headers: param_headers)
5353
end
5454
end
55+
56+
context 'when accept header is changed' do
57+
let!(:client) do
58+
described_class.new(
59+
host: 'http://localhost:9200',
60+
transport_options: { headers: instance_headers }
61+
).tap do |client|
62+
client.instance_variable_set('@verified', true)
63+
end
64+
end
65+
let(:instance_headers) do
66+
{ accept: 'application/json' }
67+
end
68+
69+
it 'performs the request with the header' do
70+
connection_headers = client.transport.connections.connections.first.connection.headers
71+
expect(connection_headers['Accept']).to eq 'application/json'
72+
expect(connection_headers['Content-Type']).to eq 'application/vnd.elasticsearch+json; compatible-with=9'
73+
74+
expect_any_instance_of(Faraday::Connection)
75+
.to receive(:run_request)
76+
.with(:get, 'http://localhost:9200/_search', nil, connection_headers) { OpenStruct.new(body: '') }
77+
client.search
78+
end
79+
end
80+
81+
context 'when content-type header is changed' do
82+
let!(:client) do
83+
described_class.new(
84+
host: 'http://localhost:9200',
85+
transport_options: { headers: instance_headers }
86+
).tap do |client|
87+
client.instance_variable_set('@verified', true)
88+
end
89+
end
90+
let(:instance_headers) do
91+
{ content_type: 'application/json' }
92+
end
93+
94+
it 'performs the request with the header' do
95+
connection_headers = client.transport.connections.connections.first.connection.headers
96+
expect(connection_headers['Accept']).to eq 'application/vnd.elasticsearch+json; compatible-with=9'
97+
expect(connection_headers['Content-Type']).to eq 'application/json'
98+
99+
expect_any_instance_of(Faraday::Connection)
100+
.to receive(:run_request)
101+
.with(:get, 'http://localhost:9200/_search', nil, connection_headers) { OpenStruct.new(body: '') }
102+
client.search
103+
end
104+
end
105+
106+
context 'when no header is set, uses v9 content-type and accept' do
107+
let!(:client) do
108+
described_class.new(host: 'http://localhost:9200').tap do |client|
109+
client.instance_variable_set('@verified', true)
110+
end
111+
end
112+
113+
it 'performs the request with the header' do
114+
expected_headers = client.transport.connections.connections.first.connection.headers
115+
expect(expected_headers['Accept']).to eq 'application/vnd.elasticsearch+json; compatible-with=9'
116+
expect(expected_headers['Content-Type']).to eq 'application/vnd.elasticsearch+json; compatible-with=9'
117+
118+
expect_any_instance_of(Faraday::Connection)
119+
.to receive(:run_request)
120+
.with(:get, 'http://localhost:9200/_search', nil, expected_headers) { OpenStruct.new(body: '') }
121+
client.search
122+
end
123+
end
55124
end

0 commit comments

Comments
 (0)