Skip to content

Commit 5256354

Browse files
committed
Avoid duplicated content-type headers
Adds tests for Manticore, depends on changes in transport: elastic/elastic-transport-ruby#115 Addresses #2961
1 parent 6828e76 commit 5256354

2 files changed

Lines changed: 84 additions & 9 deletions

File tree

elasticsearch/lib/elasticsearch.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,10 @@ def set_user_agent!(arguments)
189189
def set_content_type!(arguments)
190190
headers = {}
191191
user_headers = arguments&.[](:transport_options)&.[](:headers)
192-
unless user_headers&.keys&.detect { |h| h =~ /content-?_?type/ }
192+
unless user_headers&.keys&.detect { |h| h =~ /content-?_?type/i }
193193
headers['content-type'] = 'application/vnd.elasticsearch+json; compatible-with=9'
194194
end
195-
unless user_headers&.keys&.detect { |h| h =~ /accept/ }
195+
unless user_headers&.keys&.detect { |h| h =~ /accept/i }
196196
headers['accept'] = 'application/vnd.elasticsearch+json; compatible-with=9'
197197
end
198198
set_header(headers, arguments) unless headers.empty?

elasticsearch/spec/unit/headers_spec.rb

Lines changed: 82 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,20 +73,18 @@
7373

7474
expect_any_instance_of(Faraday::Connection)
7575
.to receive(:run_request)
76-
.with(:get, 'http://localhost:9200/_search', nil, connection_headers) { OpenStruct.new(body: '') }
76+
.with(:get, 'http://localhost:9200/_search', nil, connection_headers) { OpenStruct.new(body: '') }
7777
client.search
7878
end
7979
end
8080

8181
context 'when content-type header is changed' do
8282
let!(:client) do
83-
described_class.new(
84-
host: 'http://localhost:9200',
85-
transport_options: { headers: instance_headers }
86-
).tap do |client|
83+
described_class.new(transport_options: { headers: instance_headers }).tap do |client|
8784
client.instance_variable_set('@verified', true)
8885
end
8986
end
87+
9088
let(:instance_headers) do
9189
{ content_type: 'application/json' }
9290
end
@@ -98,11 +96,88 @@
9896

9997
expect_any_instance_of(Faraday::Connection)
10098
.to receive(:run_request)
101-
.with(:get, 'http://localhost:9200/_search', nil, connection_headers) { OpenStruct.new(body: '') }
99+
.with(:get, 'http://localhost:9200/_search', nil, connection_headers) { OpenStruct.new(body: '') }
100+
client.search
101+
end
102+
end
103+
104+
context 'when Content-Type header is used' do
105+
let(:client) do
106+
described_class.new(transport_options: { headers: instance_headers }).tap do |client|
107+
client.instance_variable_set('@verified', true)
108+
end
109+
end
110+
111+
let(:instance_headers) do
112+
{ 'Content-Type' => 'application/text' }
113+
end
114+
115+
it 'performs the request with the header' do
116+
connection_headers = client.transport.connections.connections.first.connection.headers
117+
expect(connection_headers['Accept']).to eq 'application/vnd.elasticsearch+json; compatible-with=9'
118+
expect(connection_headers['Content-Type']).to eq 'application/text'
119+
120+
expect_any_instance_of(Faraday::Connection)
121+
.to receive(:run_request)
122+
.with(:get, 'http://localhost:9200/_search', nil, connection_headers) { OpenStruct.new(body: '') }
102123
client.search
103124
end
104125
end
105126

127+
if defined?(JRUBY_VERSION)
128+
context 'Using Manticore with JRuby' do
129+
let(:client) do
130+
require 'elastic/transport/transport/http/manticore'
131+
described_class.new(
132+
transport_class: Elastic::Transport::Transport::HTTP::Manticore,
133+
transport_options: { headers: instance_headers }
134+
).tap do |client|
135+
client.instance_variable_set('@verified', true)
136+
end
137+
end
138+
139+
context 'when Content-Type header is used' do
140+
let(:instance_headers) do
141+
{ 'Content-Type' => 'application/text' }
142+
end
143+
144+
it 'does not duplicate content-type the header' do
145+
connection_headers = client.transport.connections.connections.first.connection.instance_variable_get('@options')[:headers]
146+
expect(connection_headers['accept']).to eq 'application/vnd.elasticsearch+json; compatible-with=9'
147+
expect(connection_headers['content-type']).to be nil
148+
expect(connection_headers.fetch('Content-Type')).to eq 'application/text'
149+
150+
expect_any_instance_of(Manticore::Client)
151+
.to receive(:get).with(
152+
'http://localhost:9200/_search',
153+
{ headers: connection_headers }
154+
) { OpenStruct.new(body: '') }
155+
client.search
156+
end
157+
158+
context 'when content-type header is used' do
159+
let(:instance_headers) do
160+
{ 'content-type' => 'application/text' }
161+
end
162+
163+
it 'does not duplicate Content-Type the header' do
164+
connection_headers = client.transport.connections.connections.first.connection.instance_variable_get('@options')[:headers]
165+
expect(connection_headers['accept']).to eq 'application/vnd.elasticsearch+json; compatible-with=9'
166+
expect(connection_headers['content-type']).to be nil
167+
expect(connection_headers.fetch('Content-Type')).to eq 'application/text'
168+
169+
expect_any_instance_of(Manticore::Client)
170+
.to receive(:get).with(
171+
'http://localhost:9200/_search',
172+
{ headers: connection_headers }
173+
) { OpenStruct.new(body: '') }
174+
client.search
175+
end
176+
end
177+
end
178+
end
179+
end
180+
106181
context 'when no header is set, uses v9 content-type and accept' do
107182
let!(:client) do
108183
described_class.new(host: 'http://localhost:9200').tap do |client|
@@ -117,7 +192,7 @@
117192

118193
expect_any_instance_of(Faraday::Connection)
119194
.to receive(:run_request)
120-
.with(:get, 'http://localhost:9200/_search', nil, expected_headers) { OpenStruct.new(body: '') }
195+
.with(:get, 'http://localhost:9200/_search', nil, expected_headers) { OpenStruct.new(body: '') }
121196
client.search
122197
end
123198
end

0 commit comments

Comments
 (0)