Skip to content

Commit 3c04cf9

Browse files
committed
Fixes instantiating Client in Manticore implementation.
Backport from elastic-transport elastic/elastic-transport-ruby#69
1 parent 6fde526 commit 3c04cf9

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,13 @@ def build_client(options = {})
8787
# @return [Response]
8888
# @see Transport::Base#perform_request
8989
#
90-
def perform_request(method, path, params={}, body=nil, headers=nil, opts={})
90+
def perform_request(method, path, params = {}, body = nil, headers = nil, opts = {})
9191
super do |connection, url|
9292
body = body ? __convert_to_json(body) : nil
93-
body, headers = compress_request(body, @request_options[:headers])
94-
93+
body, headers = compress_request(body, parse_headers(headers))
9594
params[:body] = body if body
9695
params[:headers] = headers if headers
97-
params = params.merge(@request_options)
96+
9897
case method
9998
when 'GET'
10099
resp = connection.connection.get(url, params)
@@ -161,8 +160,14 @@ def host_unreachable_exceptions
161160

162161
private
163162

163+
def parse_headers(headers)
164+
request_headers = @request_options.fetch(:headers, {})
165+
headers = request_headers.merge(headers || {})
166+
headers.empty? ? nil : headers
167+
end
168+
164169
def apply_headers(options)
165-
headers = options[:headers] || options.dig(:transport_options, :headers) || {}
170+
headers = options[:headers].clone || options.dig(:transport_options, :headers).clone || {}
166171
headers[CONTENT_TYPE_STR] = find_value(headers, CONTENT_TYPE_REGEX) || DEFAULT_CONTENT_TYPE
167172
headers[USER_AGENT_STR] = find_value(headers, USER_AGENT_REGEX) || find_value(@request_options[:headers], USER_AGENT_REGEX) || user_agent_header
168173
headers[ACCEPT_ENCODING] = GZIP if use_compression?

elasticsearch-transport/spec/elasticsearch/transport/http/manticore_spec.rb

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
expect(perform_request).to be_kind_of(Elasticsearch::Transport::Transport::Response)
5454
end
5555

56-
it 'run body with preper params' do
56+
it 'run body with proper params' do
5757
expect(
5858
client.transport.connections.first.connection
5959
).to receive(:post).with('http://localhost:9200/', { body: body, headers: expected_headers }).and_return(response)
@@ -138,6 +138,24 @@
138138
end
139139
end
140140
end
141+
142+
context 'headers' do
143+
it 'sends custom headers' do
144+
client = Elasticsearch::Transport::Client.new(
145+
transport_class: described_class,
146+
transport_options: { headers: { 'Elastic-Api-Version'=>'2023-10-31' } }
147+
)
148+
expect(
149+
client.transport.connections.first.connection
150+
).to receive(:get).with(
151+
'http://localhost:9200/',
152+
{
153+
headers: expected_headers.merge({ 'Elastic-Api-Version'=>'2023-10-31' })
154+
}
155+
).and_return(response)
156+
client.perform_request('GET', '/', {}, nil, headers)
157+
end
158+
end
141159
end
142160
end
143161
end

elasticsearch-transport/test/unit/transport_manticore_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ def common_headers
111111
{
112112
body: '{"foo":"bar"}',
113113
headers: {
114-
'Content-Type' => 'application/json',
114+
'Content-Type' => 'application/x-ndjson',
115115
'User-Agent' => @transport.send(:user_agent_header)
116116
}
117117
}

0 commit comments

Comments
 (0)