From 25c83ecbcc4ca2d9a17a69669f17a4d0825dbe73 Mon Sep 17 00:00:00 2001 From: Fernando Briano Date: Thu, 21 Sep 2023 10:53:40 +0100 Subject: [PATCH] Fixes instantiating Client in Manticore implementation. Fixes #66 --- .../transport/transport/http/manticore.rb | 11 +++++++--- spec/elastic/transport/http/manticore_spec.rb | 22 +++++++++++++++++-- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/lib/elastic/transport/transport/http/manticore.rb b/lib/elastic/transport/transport/http/manticore.rb index 26aafe38..499c2ec6 100644 --- a/lib/elastic/transport/transport/http/manticore.rb +++ b/lib/elastic/transport/transport/http/manticore.rb @@ -90,11 +90,10 @@ 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 +160,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].clone || options.dig(:transport_options, :headers).clone || {} headers[CONTENT_TYPE_STR] = find_value(headers, CONTENT_TYPE_REGEX) || DEFAULT_CONTENT_TYPE diff --git a/spec/elastic/transport/http/manticore_spec.rb b/spec/elastic/transport/http/manticore_spec.rb index 7fbb7897..b65684ba 100644 --- a/spec/elastic/transport/http/manticore_spec.rb +++ b/spec/elastic/transport/http/manticore_spec.rb @@ -53,7 +53,7 @@ expect(perform_request).to be_kind_of(Elastic::Transport::Transport::Response) end - it 'run body with preper params' do + it 'runs body with proper params' do expect( client.transport.connections.first.connection ).to receive(:post).with( @@ -121,7 +121,7 @@ gzip.close.string end - it 'run body with preper params' do + it 'runs body with proper params' do expect( client.transport.connections.first.connection ).to receive(:post).with(*request_params).and_return(response) @@ -141,6 +141,24 @@ end end end + + context 'headers' do + it 'sends custom headers' do + client = Elastic::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