From bd10a6b27716b4f95057d95e644595cc68751fbf Mon Sep 17 00:00:00 2001 From: Fernando Briano Date: Tue, 26 Sep 2023 08:57:35 +0100 Subject: [PATCH] Fixes instantiating Client in Manticore implementation. Backport from elastic-transport https://github.com/elastic/elastic-transport-ruby/pull/69 --- .../transport/transport/http/manticore.rb | 10 ++++++++-- .../transport/http/manticore_spec.rb | 20 ++++++++++++++++++- .../test/unit/transport_manticore_test.rb | 2 +- 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/elasticsearch-transport/lib/elasticsearch/transport/transport/http/manticore.rb b/elasticsearch-transport/lib/elasticsearch/transport/transport/http/manticore.rb index e0ff0b73c2..165168e35d 100644 --- a/elasticsearch-transport/lib/elasticsearch/transport/transport/http/manticore.rb +++ b/elasticsearch-transport/lib/elasticsearch/transport/transport/http/manticore.rb @@ -90,11 +90,11 @@ def build_client(options = {}) def perform_request(method, path, params={}, body=nil, headers=nil, opts={}) super do |connection, url| body = body ? __convert_to_json(body) : nil - body, headers = compress_request(body, @request_options[:headers]) + body, headers = compress_request(body, parse_headers(headers)) params[:body] = body if body params[:headers] = headers if headers - params = params.merge(@request_options) + case method when 'GET' resp = connection.connection.get(url, params) @@ -161,6 +161,12 @@ def host_unreachable_exceptions private + def parse_headers(headers) + request_headers = @request_options.fetch(:headers, {}) + headers = request_headers.merge(headers || {}) + headers.empty? ? nil : headers + end + def apply_headers(options) headers = options[:headers] || options.dig(:transport_options, :headers) || {} headers[CONTENT_TYPE_STR] = find_value(headers, CONTENT_TYPE_REGEX) || DEFAULT_CONTENT_TYPE diff --git a/elasticsearch-transport/spec/elasticsearch/transport/http/manticore_spec.rb b/elasticsearch-transport/spec/elasticsearch/transport/http/manticore_spec.rb index 374a4d6727..0776321455 100644 --- a/elasticsearch-transport/spec/elasticsearch/transport/http/manticore_spec.rb +++ b/elasticsearch-transport/spec/elasticsearch/transport/http/manticore_spec.rb @@ -53,7 +53,7 @@ expect(perform_request).to be_kind_of(Elasticsearch::Transport::Transport::Response) end - it 'run body with preper params' do + it 'run body with proper params' do expect( client.transport.connections.first.connection ).to receive(:post).with('http://localhost:9200/', { body: body, headers: expected_headers }).and_return(response) @@ -137,6 +137,24 @@ client.perform_request('POST', '/', {}, nil, headers) end end + + context 'headers' do + it 'sends custom headers' do + client = Elasticsearch::Transport::Client.new( + transport_class: described_class, + transport_options: { headers: { 'Elastic-Api-Version'=>'2023-10-31' } } + ) + expect( + client.transport.connections.first.connection + ).to receive(:get).with( + 'http://localhost:9200/', + { + headers: expected_headers.merge({ 'Elastic-Api-Version'=>'2023-10-31' }) + } + ).and_return(response) + client.perform_request('GET', '/', {}, nil, headers) + end + end end end end diff --git a/elasticsearch-transport/test/unit/transport_manticore_test.rb b/elasticsearch-transport/test/unit/transport_manticore_test.rb index 5e4522a2f1..c254c1b1ad 100644 --- a/elasticsearch-transport/test/unit/transport_manticore_test.rb +++ b/elasticsearch-transport/test/unit/transport_manticore_test.rb @@ -111,7 +111,7 @@ def common_headers { body: '{"foo":"bar"}', headers: { - 'Content-Type' => 'application/json', + 'Content-Type' => 'application/x-ndjson', 'User-Agent' => @transport.send(:user_agent_header) } }