diff --git a/elasticsearch-api/lib/elasticsearch/api/actions/bulk.rb b/elasticsearch-api/lib/elasticsearch/api/actions/bulk.rb index 1ec0d57a68..4c3132e577 100644 --- a/elasticsearch-api/lib/elasticsearch/api/actions/bulk.rb +++ b/elasticsearch-api/lib/elasticsearch/api/actions/bulk.rb @@ -187,9 +187,7 @@ def bulk(arguments = {}) body end - headers.merge!({ - 'Content-Type' => 'application/vnd.elasticsearch+x-ndjson; compatible-with=9' - }) + Utils.update_ndjson_headers!(headers, transport.options.dig(:transport_options, :headers)) Elasticsearch::API::Response.new( perform_request(method, path, params, payload, headers, request_opts) ) diff --git a/elasticsearch-api/lib/elasticsearch/api/actions/fleet/msearch.rb b/elasticsearch-api/lib/elasticsearch/api/actions/fleet/msearch.rb index d2c6048031..bd722107a6 100644 --- a/elasticsearch-api/lib/elasticsearch/api/actions/fleet/msearch.rb +++ b/elasticsearch-api/lib/elasticsearch/api/actions/fleet/msearch.rb @@ -108,9 +108,7 @@ def msearch(arguments = {}) payload = body end - headers.merge!({ - 'Content-Type' => 'application/vnd.elasticsearch+x-ndjson; compatible-with=9' - }) + Utils.update_ndjson_headers!(headers, client.transport.options.dig(:transport_options, :headers)) Elasticsearch::API::Response.new( perform_request(method, path, params, payload, headers, request_opts) ) diff --git a/elasticsearch-api/lib/elasticsearch/api/actions/msearch.rb b/elasticsearch-api/lib/elasticsearch/api/actions/msearch.rb index 79046ffc59..2407f2fbd6 100644 --- a/elasticsearch-api/lib/elasticsearch/api/actions/msearch.rb +++ b/elasticsearch-api/lib/elasticsearch/api/actions/msearch.rb @@ -120,9 +120,7 @@ def msearch(arguments = {}) payload = body end - headers.merge!({ - 'Content-Type' => 'application/vnd.elasticsearch+x-ndjson; compatible-with=9' - }) + Utils.update_ndjson_headers!(headers, transport.options.dig(:transport_options, :headers)) Elasticsearch::API::Response.new( perform_request(method, path, params, payload, headers, request_opts) ) diff --git a/elasticsearch-api/lib/elasticsearch/api/actions/msearch_template.rb b/elasticsearch-api/lib/elasticsearch/api/actions/msearch_template.rb index 49d19c067c..1ff11061cd 100644 --- a/elasticsearch-api/lib/elasticsearch/api/actions/msearch_template.rb +++ b/elasticsearch-api/lib/elasticsearch/api/actions/msearch_template.rb @@ -93,9 +93,7 @@ def msearch_template(arguments = {}) payload = body end - headers.merge!({ - 'Content-Type' => 'application/vnd.elasticsearch+x-ndjson; compatible-with=9' - }) + Utils.update_ndjson_headers!(headers, transport.options.dig(:transport_options, :headers)) Elasticsearch::API::Response.new( perform_request(method, path, params, payload, headers, request_opts) ) diff --git a/elasticsearch-api/lib/elasticsearch/api/actions/text_structure/find_structure.rb b/elasticsearch-api/lib/elasticsearch/api/actions/text_structure/find_structure.rb index 489ce1d202..23c4a20d1e 100644 --- a/elasticsearch-api/lib/elasticsearch/api/actions/text_structure/find_structure.rb +++ b/elasticsearch-api/lib/elasticsearch/api/actions/text_structure/find_structure.rb @@ -136,9 +136,7 @@ def find_structure(arguments = {}) body end - headers.merge!({ - 'Content-Type' => 'application/vnd.elasticsearch+x-ndjson; compatible-with=9' - }) + Utils.update_ndjson_headers!(headers, client.transport.options.dig(:transport_options, :headers)) Elasticsearch::API::Response.new( perform_request(method, path, params, payload, headers, request_opts) ) diff --git a/elasticsearch-api/lib/elasticsearch/api/utils.rb b/elasticsearch-api/lib/elasticsearch/api/utils.rb index 128281a611..8414c82ec7 100644 --- a/elasticsearch-api/lib/elasticsearch/api/utils.rb +++ b/elasticsearch-api/lib/elasticsearch/api/utils.rb @@ -177,6 +177,21 @@ def rescue_from_not_found(&block) end end + # Updates ndjson headers for msearch, bulk, and others + # + def update_ndjson_headers!(headers, client_headers) + current_content = client_headers.keys.find { |c| c.match?(/content-?_?type/i) } || 'content-type' + current_accept = client_headers.keys.find { |c| c.match?(/accept/i) } || 'accept' + version = client_headers[current_content].match(/compatible-with=([0-9]+)/)[1] || 9 + + headers.merge!( + { + current_content => "application/vnd.elasticsearch+x-ndjson; compatible-with=#{version}", + current_accept => "application/vnd.elasticsearch+x-ndjson; compatible-with=#{version}" + } + ) + end + extend self end end diff --git a/elasticsearch-api/spec/spec_helper.rb b/elasticsearch-api/spec/spec_helper.rb index 420a4aecd3..426e70af6e 100644 --- a/elasticsearch-api/spec/spec_helper.rb +++ b/elasticsearch-api/spec/spec_helper.rb @@ -59,6 +59,13 @@ def self.included(context) end end +Transport ||= Struct.new('Transport', :options) + +def dummy_ndjson_headers + allow(client_double).to receive(:transport).and_return Transport.new({ transport_options: { headers: {} } }) + allow(Elasticsearch::API::Utils).to receive(:update_ndjson_headers!).and_return({}) +end + RSpec.configure do |config| config.include(HelperModule) config.filter_run_excluding skip: true diff --git a/elasticsearch-api/spec/unit/actions/bulk_spec.rb b/elasticsearch-api/spec/unit/actions/bulk_spec.rb index 25906f11c7..4709c48e66 100644 --- a/elasticsearch-api/spec/unit/actions/bulk_spec.rb +++ b/elasticsearch-api/spec/unit/actions/bulk_spec.rb @@ -29,11 +29,12 @@ ] end - let(:headers) { - { - 'Content-Type' => 'application/vnd.elasticsearch+x-ndjson; compatible-with=9' - } - } + let(:headers) { {} } + + # This test only cares about the body, there's another test for the ndjson headers. + before do + dummy_ndjson_headers + end let(:params) { {} } let(:url) { '_bulk' } @@ -53,12 +54,16 @@ end it 'performs the request' do - expect(client_double.bulk(:body => [ - { :index => { :_index => 'myindexA', :_id => '1', :data => { :title => 'Test' } } }, - { :update => { :_index => 'myindexB', :_id => '2', :data => { :doc => { :title => 'Update' } } } }, - { :delete => { :_index => 'myindexC', :_id => '3' } }, - { :index => { :_index => 'myindexD', :_id => '1', :data => { :data => 'MYDATA' } } }, - ])).to be_a Elasticsearch::API::Response + expect( + client_double.bulk( + body: [ + { index: { _index: 'myindexA', _id: '1', data: { title: 'Test' } } }, + { update: { _index: 'myindexB', _id: '2', data: { doc: { title: 'Update' } } } }, + { delete: { _index: 'myindexC', _id: '3' } }, + { index: { _index: 'myindexD', _id: '1', data: { data: 'MYDATA' } } } + ] + ) + ).to be_a Elasticsearch::API::Response end end @@ -72,7 +77,7 @@ params, body, headers, - { defined_params: { index: 'myindex' }, :endpoint=>"bulk"} + { defined_params: { index: 'myindex' }, endpoint: 'bulk' } ] end @@ -90,8 +95,14 @@ end it 'performs the request' do - expect(client_double.bulk(body:[ { :update => { :_index => 'myindex', :_id => '1' } }, - { :doc => { :data => { :title => 'Update' } } } ])).to be_a Elasticsearch::API::Response + expect( + client_double.bulk( + body: [ + { update: { _index: 'myindex', _id: '1' } }, + { doc: { data: { title: 'Update' } } } + ] + ) + ).to be_a Elasticsearch::API::Response end end @@ -137,7 +148,7 @@ params, body, headers, - { defined_params: { index: 'foo^bar' }, :endpoint=>"bulk"} + { defined_params: { index: 'foo^bar' }, endpoint: 'bulk'} ] end diff --git a/elasticsearch-api/spec/unit/actions/fleet/msearch_spec.rb b/elasticsearch-api/spec/unit/actions/fleet/msearch_spec.rb index 40ef5f6584..5f786d969d 100644 --- a/elasticsearch-api/spec/unit/actions/fleet/msearch_spec.rb +++ b/elasticsearch-api/spec/unit/actions/fleet/msearch_spec.rb @@ -24,28 +24,17 @@ '_fleet/_fleet_msearch', {}, {}, - headers, + {}, { endpoint: 'fleet.msearch' } ] end - let(:headers) { - { - 'Content-Type' => 'application/vnd.elasticsearch+x-ndjson; compatible-with=9' - } - } + # This test only cares about the body, there's another test for the ndjson headers. + before do + dummy_ndjson_headers + end it 'performs the request' do expect(client_double.fleet.msearch(body: {})).to be_a Elasticsearch::API::Response end - - let(:client) do - Class.new { include Elasticsearch::API }.new - end - - it 'requires the :body argument' do - expect { - client.fleet.msearch - }.to raise_exception(ArgumentError) - end end diff --git a/elasticsearch-api/spec/unit/actions/msearch_spec.rb b/elasticsearch-api/spec/unit/actions/msearch_spec.rb index a7b576e02c..2be0a06272 100644 --- a/elasticsearch-api/spec/unit/actions/msearch_spec.rb +++ b/elasticsearch-api/spec/unit/actions/msearch_spec.rb @@ -41,24 +41,16 @@ {} end - let(:headers) { - { - 'Content-Type' => 'application/vnd.elasticsearch+x-ndjson; compatible-with=9' - } - } - - let(:client) do - Class.new { include Elasticsearch::API }.new + let(:headers) do + {} end - it 'requires the :body argument' do - expect { - client.msearch - }.to raise_exception(ArgumentError) + # This test only cares about the body, there's another test for the ndjson headers. + before do + dummy_ndjson_headers end context 'when the body is an object' do - let(:body) do <<-PAYLOAD.gsub(/^\s+/, '') {"index":"foo"} @@ -72,7 +64,7 @@ it 'performs the request' do expect(client_double.msearch body: [ - { index: 'foo', search: { query: { match_all: {} } } }, + { index: 'foo', search: { query: { match_all: {} } } }, { index: 'bar', search: { query: { match: { foo: 'bar' } } } }, { search_type: 'count', search: { facets: { tags: {} } } } ]) @@ -80,7 +72,6 @@ end context 'when the body is a string' do - let(:body) do %Q|{"foo":"bar"}\n{"moo":"lam"}| end @@ -141,7 +132,6 @@ end context 'when the request needs to be URL-escaped' do - let(:url) do 'foo%5Ebar/_msearch' end @@ -167,7 +157,6 @@ end context 'when the URL params need to be URL-encoded' do - let(:url) do '_msearch' end diff --git a/elasticsearch-api/spec/unit/actions/msearch_template_spec.rb b/elasticsearch-api/spec/unit/actions/msearch_template_spec.rb index 8b30d7d189..b62af31b2e 100644 --- a/elasticsearch-api/spec/unit/actions/msearch_template_spec.rb +++ b/elasticsearch-api/spec/unit/actions/msearch_template_spec.rb @@ -37,11 +37,12 @@ {} end - let(:headers) { - { - 'Content-Type' => 'application/vnd.elasticsearch+x-ndjson; compatible-with=9' - } - } + let(:headers) {{}} + + # This test only cares about the body, there's another test for the ndjson headers. + before do + dummy_ndjson_headers + end let(:url) do '_msearch/template' diff --git a/elasticsearch-api/spec/unit/actions/text_structure/find_structure_spec.rb b/elasticsearch-api/spec/unit/actions/text_structure/find_structure_spec.rb index cd94e76348..3798068f35 100644 --- a/elasticsearch-api/spec/unit/actions/text_structure/find_structure_spec.rb +++ b/elasticsearch-api/spec/unit/actions/text_structure/find_structure_spec.rb @@ -17,23 +17,23 @@ require 'spec_helper' -describe 'client.text_structure#test_grok_pattern' do +describe 'client.text_structure#find_structure' do let(:expected_args) do [ 'POST', '_text_structure/find_structure', {}, {}, - headers, + {}, { endpoint: 'text_structure.find_structure' } ] end - let(:headers) { - { - 'Content-Type' => 'application/vnd.elasticsearch+x-ndjson; compatible-with=9' - } - } + # This test only cares about the body, there's another test for the ndjson headers. + before do + dummy_ndjson_headers + allow(client_double.text_structure.client).to receive(:transport).and_return Transport.new({ transport_options: { headers: {} } }) + end it 'performs the request' do expect(client_double.text_structure.find_structure(body: {})).to be_a Elasticsearch::API::Response diff --git a/elasticsearch-api/spec/unit/ndjson_endpoints/bulk_spec.rb b/elasticsearch-api/spec/unit/ndjson_endpoints/bulk_spec.rb new file mode 100644 index 0000000000..d775c4e12c --- /dev/null +++ b/elasticsearch-api/spec/unit/ndjson_endpoints/bulk_spec.rb @@ -0,0 +1,124 @@ +# Licensed to Elasticsearch B.V. under one or more contributor +# license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright +# ownership. Elasticsearch B.V. licenses this file to you under +# the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +require 'spec_helper' + +describe 'bulk headers test' do + context 'when not setting headers' do + let(:client) do + Elasticsearch::Client.new + end + + let(:expected_headers) do + { + 'accept' => 'application/vnd.elasticsearch+x-ndjson; compatible-with=9', + 'content-type' => 'application/vnd.elasticsearch+x-ndjson; compatible-with=9' + } + end + + it 'does not override headers' do + allow(client) + .to receive(:perform_request) + .with( + Elasticsearch::API::HTTP_POST, + '_bulk', + {}, + {}, + expected_headers, + { endpoint: 'bulk' } + ) + expect(client.bulk(body: {})).to be_a Elasticsearch::API::Response + end + end + + context 'when using compatibility headers for version 8' do + let(:client) do + Elasticsearch::Client.new( + transport_options: { + headers: custom_headers + } + ) + end + + let(:custom_headers) do + { + accept: 'application/vnd.elasticsearch+json; compatible-with=8', + content_type: 'application/vnd.elasticsearch+json; compatible-with=8' + } + end + + let(:expected_headers) do + { + accept: 'application/vnd.elasticsearch+x-ndjson; compatible-with=8', + content_type: 'application/vnd.elasticsearch+x-ndjson; compatible-with=8' + } + end + + it 'does not override version in headers' do + allow(client) + .to receive(:perform_request) + .with( + Elasticsearch::API::HTTP_POST, + '_bulk', + {}, + {}, + expected_headers, + { endpoint: 'bulk' } + ) + expect(client.bulk(body: {})).to be_a Elasticsearch::API::Response + end + end + + context 'when using custom headers in request' do + let(:client) do + Elasticsearch::Client.new( + transport_options: { + headers: custom_headers + } + ) + end + + let(:custom_headers) do + { + accept: 'application/vnd.elasticsearch+json; compatible-with=8', + content_type: 'application/vnd.elasticsearch+json; compatible-with=8' + } + end + + let(:expected_headers) do + { + accept: 'application/vnd.elasticsearch+x-ndjson; compatible-with=8', + content_type: 'application/vnd.elasticsearch+x-ndjson; compatible-with=8', + x_custom: 'Custom header' + } + end + + it 'does not override version in headers' do + allow(client) + .to receive(:perform_request) + .with( + Elasticsearch::API::HTTP_POST, + '_bulk', + {}, + {}, + expected_headers, + { endpoint: 'bulk' } + ) + expect(client.bulk(body: {}, headers: { x_custom: 'Custom header' })).to be_a Elasticsearch::API::Response + end + end +end diff --git a/elasticsearch-api/spec/unit/ndjson_endpoints/find_structure_spec.rb b/elasticsearch-api/spec/unit/ndjson_endpoints/find_structure_spec.rb new file mode 100644 index 0000000000..3b49f059a2 --- /dev/null +++ b/elasticsearch-api/spec/unit/ndjson_endpoints/find_structure_spec.rb @@ -0,0 +1,124 @@ +# Licensed to Elasticsearch B.V. under one or more contributor +# license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright +# ownership. Elasticsearch B.V. licenses this file to you under +# the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +require 'spec_helper' + +describe 'text_structure.find_structure headers test' do + context 'when not setting headers' do + let(:client) do + Elasticsearch::Client.new + end + + let(:expected_headers) do + { + 'accept' => 'application/vnd.elasticsearch+x-ndjson; compatible-with=9', + 'content-type' => 'application/vnd.elasticsearch+x-ndjson; compatible-with=9' + } + end + + it 'does not override headers' do + allow(client) + .to receive(:perform_request) + .with( + Elasticsearch::API::HTTP_POST, + '_text_structure/find_structure', + {}, + {}, + expected_headers, + { endpoint: 'text_structure.find_structure' } + ) + expect(client.text_structure.find_structure(body: {})).to be_a Elasticsearch::API::Response + end + end + + context 'when using compatibility headers for version 8' do + let(:client) do + Elasticsearch::Client.new( + transport_options: { + headers: custom_headers + } + ) + end + + let(:custom_headers) do + { + accept: 'application/vnd.elasticsearch+json; compatible-with=8', + content_type: 'application/vnd.elasticsearch+json; compatible-with=8' + } + end + + let(:expected_headers) do + { + accept: 'application/vnd.elasticsearch+x-ndjson; compatible-with=8', + content_type: 'application/vnd.elasticsearch+x-ndjson; compatible-with=8' + } + end + + it 'does not override version in headers' do + allow(client) + .to receive(:perform_request) + .with( + Elasticsearch::API::HTTP_POST, + '_text_structure/find_structure', + {}, + {}, + expected_headers, + { endpoint: 'text_structure.find_structure' } + ) + expect(client.text_structure.find_structure(body: {})).to be_a Elasticsearch::API::Response + end + end + + context 'when using custom headers in request' do + let(:client) do + Elasticsearch::Client.new( + transport_options: { + headers: custom_headers + } + ) + end + + let(:custom_headers) do + { + accept: 'application/vnd.elasticsearch+json; compatible-with=8', + content_type: 'application/vnd.elasticsearch+json; compatible-with=8' + } + end + + let(:expected_headers) do + { + accept: 'application/vnd.elasticsearch+x-ndjson; compatible-with=8', + content_type: 'application/vnd.elasticsearch+x-ndjson; compatible-with=8', + x_custom: 'Custom header' + } + end + + it 'does not override version in headers' do + allow(client) + .to receive(:perform_request) + .with( + Elasticsearch::API::HTTP_POST, + '_text_structure/find_structure', + {}, + {}, + expected_headers, + { endpoint: 'text_structure.find_structure' } + ) + expect(client.text_structure.find_structure(body: {}, headers: { x_custom: 'Custom header' })).to be_a Elasticsearch::API::Response + end + end +end diff --git a/elasticsearch-api/spec/unit/ndjson_endpoints/fleet_msearch_spec.rb b/elasticsearch-api/spec/unit/ndjson_endpoints/fleet_msearch_spec.rb new file mode 100644 index 0000000000..ddd416a526 --- /dev/null +++ b/elasticsearch-api/spec/unit/ndjson_endpoints/fleet_msearch_spec.rb @@ -0,0 +1,136 @@ +# Licensed to Elasticsearch B.V. under one or more contributor +# license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright +# ownership. Elasticsearch B.V. licenses this file to you under +# the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +require 'spec_helper' + +describe 'fleet.msearch exception' do + let(:client) do + Class.new { include Elasticsearch::API }.new + end + + it 'requires the :body argument' do + expect { + client.fleet.msearch + }.to raise_exception(ArgumentError) + end +end + +describe 'fleet.msearch headers test' do + context 'when not setting headers' do + let(:client) do + Elasticsearch::Client.new + end + + let(:expected_headers) do + { + 'accept' => 'application/vnd.elasticsearch+x-ndjson; compatible-with=9', + 'content-type' => 'application/vnd.elasticsearch+x-ndjson; compatible-with=9' + } + end + + it 'does not override headers' do + allow(client) + .to receive(:perform_request) + .with( + Elasticsearch::API::HTTP_POST, + '_fleet/_fleet_msearch', + {}, + {}, + expected_headers, + { endpoint: 'fleet.msearch' } + ) + expect(client.fleet.msearch(body: {})).to be_a Elasticsearch::API::Response + end + end + + context 'when using compatibility headers for version 8' do + let(:client) do + Elasticsearch::Client.new( + transport_options: { + headers: custom_headers + } + ) + end + + let(:custom_headers) do + { + accept: 'application/vnd.elasticsearch+json; compatible-with=8', + content_type: 'application/vnd.elasticsearch+json; compatible-with=8' + } + end + + let(:expected_headers) do + { + accept: 'application/vnd.elasticsearch+x-ndjson; compatible-with=8', + content_type: 'application/vnd.elasticsearch+x-ndjson; compatible-with=8' + } + end + + it 'does not override version in headers' do + allow(client) + .to receive(:perform_request) + .with( + Elasticsearch::API::HTTP_POST, + '_fleet/_fleet_msearch', + {}, + {}, + expected_headers, + { endpoint: 'fleet.msearch' } + ) + expect(client.fleet.msearch(body: {})).to be_a Elasticsearch::API::Response + end + end + + context 'when using custom headers in request' do + let(:client) do + Elasticsearch::Client.new( + transport_options: { + headers: custom_headers + } + ) + end + + let(:custom_headers) do + { + accept: 'application/vnd.elasticsearch+json; compatible-with=8', + content_type: 'application/vnd.elasticsearch+json; compatible-with=8' + } + end + + let(:expected_headers) do + { + accept: 'application/vnd.elasticsearch+x-ndjson; compatible-with=8', + content_type: 'application/vnd.elasticsearch+x-ndjson; compatible-with=8', + x_custom: 'Custom header' + } + end + + it 'does not override version in headers' do + allow(client) + .to receive(:perform_request) + .with( + Elasticsearch::API::HTTP_POST, + '_fleet/_fleet_msearch', + {}, + {}, + expected_headers, + { endpoint: 'fleet.msearch' } + ) + expect(client.fleet.msearch(body: {}, headers: { x_custom: 'Custom header' })).to be_a Elasticsearch::API::Response + end + end +end diff --git a/elasticsearch-api/spec/unit/ndjson_endpoints/msearch_spec.rb b/elasticsearch-api/spec/unit/ndjson_endpoints/msearch_spec.rb new file mode 100644 index 0000000000..d1d63c586c --- /dev/null +++ b/elasticsearch-api/spec/unit/ndjson_endpoints/msearch_spec.rb @@ -0,0 +1,132 @@ +# Licensed to Elasticsearch B.V. under one or more contributor +# license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright +# ownership. Elasticsearch B.V. licenses this file to you under +# the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +require 'spec_helper' + +describe 'msearch custom' do + context 'raise exception' do + it 'requires the :body argument' do + expect do + Elasticsearch::Client.new.msearch + end.to raise_exception(ArgumentError) + end + end + + context 'when not setting headers' do + let(:client) do + Elasticsearch::Client.new + end + + let(:expected_headers) do + { + 'accept' => 'application/vnd.elasticsearch+x-ndjson; compatible-with=9', + 'content-type' => 'application/vnd.elasticsearch+x-ndjson; compatible-with=9' + } + end + + it 'does not override headers' do + allow(client) + .to receive(:perform_request) + .with( + Elasticsearch::API::HTTP_POST, + '_msearch', + {}, + {}, + expected_headers, + { endpoint: 'msearch' } + ) + expect(client.msearch(body: {})).to be_a Elasticsearch::API::Response + end + end + + context 'when using compatibility headers for version 8' do + let(:client) do + Elasticsearch::Client.new( + transport_options: { + headers: custom_headers + } + ) + end + + let(:custom_headers) do + { + accept: 'application/vnd.elasticsearch+json; compatible-with=8', + content_type: 'application/vnd.elasticsearch+json; compatible-with=8' + } + end + + let(:expected_headers) do + { + accept: 'application/vnd.elasticsearch+x-ndjson; compatible-with=8', + content_type: 'application/vnd.elasticsearch+x-ndjson; compatible-with=8' + } + end + + it 'does not override version in headers' do + allow(client) + .to receive(:perform_request) + .with( + Elasticsearch::API::HTTP_POST, + '_msearch', + {}, + {}, + expected_headers, + { endpoint: 'msearch' } + ) + expect(client.msearch(body: {})).to be_a Elasticsearch::API::Response + end + end + + context 'when using custom headers in request' do + let(:client) do + Elasticsearch::Client.new( + transport_options: { + headers: custom_headers + } + ) + end + + let(:custom_headers) do + { + accept: 'application/vnd.elasticsearch+json; compatible-with=8', + content_type: 'application/vnd.elasticsearch+json; compatible-with=8' + } + end + + let(:expected_headers) do + { + accept: 'application/vnd.elasticsearch+x-ndjson; compatible-with=8', + content_type: 'application/vnd.elasticsearch+x-ndjson; compatible-with=8', + x_custom: 'Custom header' + } + end + + it 'does not override version in headers' do + allow(client) + .to receive(:perform_request) + .with( + Elasticsearch::API::HTTP_POST, + '_msearch', + {}, + {}, + expected_headers, + { endpoint: 'msearch' } + ) + expect(client.msearch(body: {}, headers: { x_custom: 'Custom header' })).to be_a Elasticsearch::API::Response + end + end +end diff --git a/elasticsearch-api/spec/unit/ndjson_endpoints/msearch_template_spec.rb b/elasticsearch-api/spec/unit/ndjson_endpoints/msearch_template_spec.rb new file mode 100644 index 0000000000..915d906de7 --- /dev/null +++ b/elasticsearch-api/spec/unit/ndjson_endpoints/msearch_template_spec.rb @@ -0,0 +1,124 @@ +# Licensed to Elasticsearch B.V. under one or more contributor +# license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright +# ownership. Elasticsearch B.V. licenses this file to you under +# the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +require 'spec_helper' + +describe 'msearrch_template headers test' do + context 'when not setting headers' do + let(:client) do + Elasticsearch::Client.new + end + + let(:expected_headers) do + { + 'accept' => 'application/vnd.elasticsearch+x-ndjson; compatible-with=9', + 'content-type' => 'application/vnd.elasticsearch+x-ndjson; compatible-with=9' + } + end + + it 'does not override headers' do + allow(client) + .to receive(:perform_request) + .with( + Elasticsearch::API::HTTP_POST, + '_msearch/template', + {}, + {}, + expected_headers, + { endpoint: 'msearch_template' } + ) + expect(client.msearch_template(body: {})).to be_a Elasticsearch::API::Response + end + end + + context 'when using compatibility headers for version 8' do + let(:client) do + Elasticsearch::Client.new( + transport_options: { + headers: custom_headers + } + ) + end + + let(:custom_headers) do + { + accept: 'application/vnd.elasticsearch+json; compatible-with=8', + content_type: 'application/vnd.elasticsearch+json; compatible-with=8' + } + end + + let(:expected_headers) do + { + accept: 'application/vnd.elasticsearch+x-ndjson; compatible-with=8', + content_type: 'application/vnd.elasticsearch+x-ndjson; compatible-with=8' + } + end + + it 'does not override version in headers' do + allow(client) + .to receive(:perform_request) + .with( + Elasticsearch::API::HTTP_POST, + '_msearch/template', + {}, + {}, + expected_headers, + { endpoint: 'msearch_template' } + ) + expect(client.msearch_template(body: {})).to be_a Elasticsearch::API::Response + end + end + + context 'when using custom headers in request' do + let(:client) do + Elasticsearch::Client.new( + transport_options: { + headers: custom_headers + } + ) + end + + let(:custom_headers) do + { + accept: 'application/vnd.elasticsearch+json; compatible-with=8', + content_type: 'application/vnd.elasticsearch+json; compatible-with=8' + } + end + + let(:expected_headers) do + { + accept: 'application/vnd.elasticsearch+x-ndjson; compatible-with=8', + content_type: 'application/vnd.elasticsearch+x-ndjson; compatible-with=8', + x_custom: 'Custom header' + } + end + + it 'does not override version in headers' do + allow(client) + .to receive(:perform_request) + .with( + Elasticsearch::API::HTTP_POST, + '_msearch/template', + {}, + {}, + expected_headers, + { endpoint: 'msearch_template' } + ) + expect(client.msearch_template(body: {}, headers: { x_custom: 'Custom header' })).to be_a Elasticsearch::API::Response + end + end +end diff --git a/elasticsearch-api/spec/unit/perform_request_spec.rb b/elasticsearch-api/spec/unit/perform_request_spec.rb index a94e74ff14..a43fb198ab 100644 --- a/elasticsearch-api/spec/unit/perform_request_spec.rb +++ b/elasticsearch-api/spec/unit/perform_request_spec.rb @@ -57,6 +57,16 @@ double('response', status: 200, body: {}, headers: {}) end + let(:transport_double) do + Transport ||= Struct.new('Transport', :options) + Transport.new({ transport_options: { headers: {} } }) + end + + before do + allow(client_double).to receive(:transport).and_return transport_double + allow(Elasticsearch::API::Utils).to receive(:update_ndjson_headers!).and_return({}) + end + context("'#{spec.endpoint_name}'") do # The expected hash passed to perform_request contains the endpoint name and any defined path parts let(:expected_perform_request_params) do