Skip to content

Commit 6b0742a

Browse files
committed
[CLIENT] Improve code handling headers
1 parent 1fdaff1 commit 6b0742a

File tree

2 files changed

+37
-10
lines changed
  • elasticsearch-transport

2 files changed

+37
-10
lines changed

elasticsearch-transport/lib/elasticsearch/transport/transport/base.rb

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -364,22 +364,26 @@ def host_unreachable_exceptions
364364
private
365365

366366
USER_AGENT_STR = 'User-Agent'.freeze
367+
USER_AGENT_REGEX = /user\-?\_?agent/
367368
CONTENT_TYPE_STR = 'Content-Type'.freeze
369+
CONTENT_TYPE_REGEX = /content\-?\_?type/
370+
DEFAULT_CONTENT_TYPE = 'application/json'.freeze
368371

369372
def apply_headers(client, options)
370-
headers = (options[:headers] || {}).inject({}) do |h, (k, v)|
371-
if k.to_s.downcase =~ /content\-?\_?type/
372-
h[CONTENT_TYPE_STR] = v
373-
else
374-
h[k] = v
375-
end
376-
h
377-
end
378-
headers[CONTENT_TYPE_STR] = 'application/json' unless headers[CONTENT_TYPE_STR]
379-
headers[USER_AGENT_STR] = user_agent_header(client) unless headers[USER_AGENT_STR]
373+
headers = options[:headers] || {}
374+
headers[CONTENT_TYPE_STR] = find_key_value(headers, CONTENT_TYPE_REGEX) || DEFAULT_CONTENT_TYPE
375+
headers[USER_AGENT_STR] = find_key_value(headers, USER_AGENT_REGEX) || user_agent_header(client)
380376
client.headers.merge!(headers)
381377
end
382378

379+
def find_key_value(hash, regex)
380+
key_value = hash.find { |k,v| k.to_s.downcase =~ regex }
381+
if key_value
382+
hash.delete(key_value[0])
383+
key_value[1]
384+
end
385+
end
386+
383387
def user_agent_header(client)
384388
@user_agent ||= begin
385389
meta = ["RUBY_VERSION: #{RUBY_VERSION}"]

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,17 @@
7272
end
7373
end
7474

75+
context 'when a user-agent header is specified as client option in lower-case' do
76+
77+
let(:client) do
78+
described_class.new(transport_options: { headers: { 'user-agent' => 'testing' } })
79+
end
80+
81+
it 'sets the specified User-Agent header' do
82+
expect(client.transport.connections.first.connection.headers['User-Agent']).to eq('testing')
83+
end
84+
end
85+
7586
context 'when a Content-Type header is specified as client option' do
7687

7788
let(:client) do
@@ -140,6 +151,18 @@
140151
end
141152
end
142153

154+
context 'when a user-agent header is specified as a client option as lower-case' do
155+
156+
let(:client) do
157+
described_class.new(transport_class: Elasticsearch::Transport::Transport::HTTP::Curb,
158+
transport_options: { headers: { 'user-agent' => 'testing' } })
159+
end
160+
161+
it 'sets the specified User-Agent header' do
162+
expect(client.transport.connections.first.connection.headers['User-Agent']).to eq('testing')
163+
end
164+
end
165+
143166
context 'when a Content-Type header is specified as client option' do
144167

145168
let(:client) do

0 commit comments

Comments
 (0)