Skip to content

Commit

Permalink
[CLIENT] Addresses custom headers on initialization
Browse files Browse the repository at this point in the history
Related: #1428, #1426
  • Loading branch information
picandocodigo committed Aug 17, 2021
1 parent cf589c3 commit 3732dd4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class Manticore
include Base

def initialize(arguments={}, &block)
@request_options = { headers: (arguments.dig(:transport_options, :headers) || {}) }
@manticore = build_client(arguments[:options] || {})
super(arguments, &block)
end
Expand Down Expand Up @@ -109,7 +110,6 @@ def perform_request(method, path, params={}, body=nil, headers=nil, opts={})
# @return [Connections::Collection]
#
def __build_connections
@request_options = {}
apply_headers(@request_options, options[:transport_options])
apply_headers(@request_options, options)

Expand Down Expand Up @@ -155,11 +155,11 @@ def host_unreachable_exceptions
private

def apply_headers(request_options, options)
headers = (options && options[:headers]) || {}
headers = options&.[](:headers) || {}
headers[CONTENT_TYPE_STR] = find_value(headers, CONTENT_TYPE_REGEX) || DEFAULT_CONTENT_TYPE
headers[USER_AGENT_STR] = find_value(headers, USER_AGENT_REGEX) || user_agent_header
headers[ACCEPT_ENCODING] = GZIP if use_compression?
request_options.merge!(headers: headers)
request_options[:headers].merge!(headers)
end

def user_agent_header
Expand Down
26 changes: 24 additions & 2 deletions elasticsearch-transport/test/unit/transport_manticore_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ def common_headers

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

should 'allow custom headers' do
transport_options = { headers: { 'Authorization' => 'Basic token' } }
transport = Manticore.new(
hosts: [{ host: 'foobar', port: 1234 }],
transport_options: transport_options
)

assert_equal(
transport.instance_variable_get(:@request_options)[:headers]['Authorization'],
'Basic token'
)
transport.connections.first.connection
.expects(:get)
.with do |_host, _options|
assert_equal('Basic token', _options[:headers]['Authorization'])
true
end
.returns(stub_everything)

transport.perform_request('GET', '/', {})
end

should 'pass :transport_options to Manticore::Client' do
options = {
transport_options: { potatoes: 1 }
}

::Manticore::Client.expects(:new).with(potatoes: 1, ssl: {})
transport = Manticore.new hosts: [{ host: 'foobar', port: 1234 }], options: options
transport = Manticore.new(hosts: [{ host: 'foobar', port: 1234 }], options: options)
end
end
end
Expand Down

0 comments on commit 3732dd4

Please sign in to comment.