Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes instantiating Client in Manticore implementation. #69

Merged
merged 2 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions lib/elastic/transport/transport/http/manticore.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
22 changes: 20 additions & 2 deletions spec/elastic/transport/http/manticore_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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)
Expand All @@ -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
2 changes: 1 addition & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,4 @@ def is_faraday_v2?
Gem::Version.new(Faraday::VERSION) >= Gem::Version.new(2)
end

Minitest::Reporters.use! FixedMinitestSpecReporter.new
Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new(print_failure_summary: true)
2 changes: 1 addition & 1 deletion test/unit/transport_manticore_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down