diff --git a/elasticsearch/lib/elasticsearch.rb b/elasticsearch/lib/elasticsearch.rb index 3e55285ad9..fc54bae1c8 100644 --- a/elasticsearch/lib/elasticsearch.rb +++ b/elasticsearch/lib/elasticsearch.rb @@ -180,11 +180,15 @@ def set_user_agent!(arguments) end def set_content_type!(arguments) - headers = { - 'content-type' => 'application/vnd.elasticsearch+json; compatible-with=9', - 'accept' => 'application/vnd.elasticsearch+json; compatible-with=9' - } - set_header(headers, arguments) + headers = {} + user_headers = arguments&.[](:transport_options)&.[](:headers) + unless user_headers&.keys&.detect { |h| h =~ /content-?_?type/ } + headers['content-type'] = 'application/vnd.elasticsearch+json; compatible-with=9' + end + unless user_headers&.keys&.detect { |h| h =~ /accept/ } + headers['accept'] = 'application/vnd.elasticsearch+json; compatible-with=9' + end + set_header(headers, arguments) unless headers.empty? end def set_header(header, arguments) diff --git a/elasticsearch/spec/unit/headers_spec.rb b/elasticsearch/spec/unit/headers_spec.rb index 48a072b2fc..3751c74488 100644 --- a/elasticsearch/spec/unit/headers_spec.rb +++ b/elasticsearch/spec/unit/headers_spec.rb @@ -52,4 +52,73 @@ client.search(headers: param_headers) end end + + context 'when accept header is changed' do + let!(:client) do + described_class.new( + host: 'http://localhost:9200', + transport_options: { headers: instance_headers } + ).tap do |client| + client.instance_variable_set('@verified', true) + end + end + let(:instance_headers) do + { accept: 'application/json' } + end + + it 'performs the request with the header' do + connection_headers = client.transport.connections.connections.first.connection.headers + expect(connection_headers['Accept']).to eq 'application/json' + expect(connection_headers['Content-Type']).to eq 'application/vnd.elasticsearch+json; compatible-with=9' + + expect_any_instance_of(Faraday::Connection) + .to receive(:run_request) + .with(:get, 'http://localhost:9200/_search', nil, connection_headers) { OpenStruct.new(body: '') } + client.search + end + end + + context 'when content-type header is changed' do + let!(:client) do + described_class.new( + host: 'http://localhost:9200', + transport_options: { headers: instance_headers } + ).tap do |client| + client.instance_variable_set('@verified', true) + end + end + let(:instance_headers) do + { content_type: 'application/json' } + end + + it 'performs the request with the header' do + connection_headers = client.transport.connections.connections.first.connection.headers + expect(connection_headers['Accept']).to eq 'application/vnd.elasticsearch+json; compatible-with=9' + expect(connection_headers['Content-Type']).to eq 'application/json' + + expect_any_instance_of(Faraday::Connection) + .to receive(:run_request) + .with(:get, 'http://localhost:9200/_search', nil, connection_headers) { OpenStruct.new(body: '') } + client.search + end + end + + context 'when no header is set, uses v9 content-type and accept' do + let!(:client) do + described_class.new(host: 'http://localhost:9200').tap do |client| + client.instance_variable_set('@verified', true) + end + end + + it 'performs the request with the header' do + expected_headers = client.transport.connections.connections.first.connection.headers + expect(expected_headers['Accept']).to eq 'application/vnd.elasticsearch+json; compatible-with=9' + expect(expected_headers['Content-Type']).to eq 'application/vnd.elasticsearch+json; compatible-with=9' + + expect_any_instance_of(Faraday::Connection) + .to receive(:run_request) + .with(:get, 'http://localhost:9200/_search', nil, expected_headers) { OpenStruct.new(body: '') } + client.search + end + end end