Skip to content

Commit 3732dd4

Browse files
committed
[CLIENT] Addresses custom headers on initialization
Related: #1428, #1426
1 parent cf589c3 commit 3732dd4

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ class Manticore
6363
include Base
6464

6565
def initialize(arguments={}, &block)
66+
@request_options = { headers: (arguments.dig(:transport_options, :headers) || {}) }
6667
@manticore = build_client(arguments[:options] || {})
6768
super(arguments, &block)
6869
end
@@ -109,7 +110,6 @@ def perform_request(method, path, params={}, body=nil, headers=nil, opts={})
109110
# @return [Connections::Collection]
110111
#
111112
def __build_connections
112-
@request_options = {}
113113
apply_headers(@request_options, options[:transport_options])
114114
apply_headers(@request_options, options)
115115

@@ -155,11 +155,11 @@ def host_unreachable_exceptions
155155
private
156156

157157
def apply_headers(request_options, options)
158-
headers = (options && options[:headers]) || {}
158+
headers = options&.[](:headers) || {}
159159
headers[CONTENT_TYPE_STR] = find_value(headers, CONTENT_TYPE_REGEX) || DEFAULT_CONTENT_TYPE
160160
headers[USER_AGENT_STR] = find_value(headers, USER_AGENT_REGEX) || user_agent_header
161161
headers[ACCEPT_ENCODING] = GZIP if use_compression?
162-
request_options.merge!(headers: headers)
162+
request_options[:headers].merge!(headers)
163163
end
164164

165165
def user_agent_header

elasticsearch-transport/test/unit/transport_manticore_test.rb

+24-2
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ def common_headers
184184

185185
should 'allow to set options for Manticore' do
186186
options = { headers: { 'User-Agent' => 'myapp-0.0' } }
187-
transport = Manticore.new hosts: [{ host: 'foobar', port: 1234 }], options: options
187+
transport = Manticore.new(hosts: [{ host: 'foobar', port: 1234 }], options: options)
188188
transport.connections.first.connection
189189
.expects(:get)
190190
.with do |_host, _options|
@@ -209,13 +209,35 @@ def common_headers
209209
transport = Manticore.new hosts: [{ host: 'foobar', port: 1234 }], options: options
210210
end
211211

212+
should 'allow custom headers' do
213+
transport_options = { headers: { 'Authorization' => 'Basic token' } }
214+
transport = Manticore.new(
215+
hosts: [{ host: 'foobar', port: 1234 }],
216+
transport_options: transport_options
217+
)
218+
219+
assert_equal(
220+
transport.instance_variable_get(:@request_options)[:headers]['Authorization'],
221+
'Basic token'
222+
)
223+
transport.connections.first.connection
224+
.expects(:get)
225+
.with do |_host, _options|
226+
assert_equal('Basic token', _options[:headers]['Authorization'])
227+
true
228+
end
229+
.returns(stub_everything)
230+
231+
transport.perform_request('GET', '/', {})
232+
end
233+
212234
should 'pass :transport_options to Manticore::Client' do
213235
options = {
214236
transport_options: { potatoes: 1 }
215237
}
216238

217239
::Manticore::Client.expects(:new).with(potatoes: 1, ssl: {})
218-
transport = Manticore.new hosts: [{ host: 'foobar', port: 1234 }], options: options
240+
transport = Manticore.new(hosts: [{ host: 'foobar', port: 1234 }], options: options)
219241
end
220242
end
221243
end

0 commit comments

Comments
 (0)