Skip to content

Commit c88475a

Browse files
committed
[API] Updates msearch to not override compatiblity headers
1 parent 7ccb0a7 commit c88475a

File tree

4 files changed

+161
-20
lines changed

4 files changed

+161
-20
lines changed

elasticsearch-api/lib/elasticsearch/api/actions/msearch.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,7 @@ def msearch(arguments = {})
120120
payload = body
121121
end
122122

123-
headers.merge!({
124-
'Content-Type' => 'application/vnd.elasticsearch+x-ndjson; compatible-with=9'
125-
})
123+
Utils.update_ndjson_headers!(headers, transport.options[:transport_options][:headers])
126124
Elasticsearch::API::Response.new(
127125
perform_request(method, path, params, payload, headers, request_opts)
128126
)

elasticsearch-api/lib/elasticsearch/api/utils.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,22 @@ def rescue_from_not_found(&block)
177177
end
178178
end
179179

180+
# Updates ndjson headers for msearch, bulk, and others
181+
#
182+
def update_ndjson_headers!(headers, client_headers)
183+
current_content = client_headers.keys.find { |c| c.match?(/content-?_?type/i) }
184+
current_accept = client_headers.keys.find { |c| c.match?(/accept/i) }
185+
186+
version = client_headers[current_content].match(/compatible-with=([8-9]{1})/)[1]
187+
188+
headers.merge!(
189+
{
190+
current_content => "application/vnd.elasticsearch+x-ndjson; compatible-with=#{version}",
191+
current_accept => "application/vnd.elasticsearch+x-ndjson; compatible-with=#{version}"
192+
}
193+
)
194+
end
195+
180196
extend self
181197
end
182198
end

elasticsearch-api/spec/unit/actions/msearch_spec.rb

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,24 +41,21 @@
4141
{}
4242
end
4343

44-
let(:headers) {
45-
{
46-
'Content-Type' => 'application/vnd.elasticsearch+x-ndjson; compatible-with=9'
47-
}
48-
}
49-
50-
let(:client) do
51-
Class.new { include Elasticsearch::API }.new
44+
let(:headers) do
45+
{}
5246
end
5347

54-
it 'requires the :body argument' do
55-
expect {
56-
client.msearch
57-
}.to raise_exception(ArgumentError)
48+
let(:transport_double) do
49+
Transport ||= Struct.new('Transport', :options)
50+
Transport.new({ transport_options: { headers: {} } })
5851
end
5952

60-
context 'when the body is an object' do
53+
before do
54+
allow(client_double).to receive(:transport).and_return transport_double
55+
allow(Elasticsearch::API::Utils).to receive(:update_ndjson_headers!).and_return(headers)
56+
end
6157

58+
context 'when the body is an object' do
6259
let(:body) do
6360
<<-PAYLOAD.gsub(/^\s+/, '')
6461
{"index":"foo"}
@@ -72,15 +69,14 @@
7269

7370
it 'performs the request' do
7471
expect(client_double.msearch body: [
75-
{ index: 'foo', search: { query: { match_all: {} } } },
72+
{ index: 'foo', search: { query: { match_all: {} } } },
7673
{ index: 'bar', search: { query: { match: { foo: 'bar' } } } },
7774
{ search_type: 'count', search: { facets: { tags: {} } } }
7875
])
7976
end
8077
end
8178

8279
context 'when the body is a string' do
83-
8480
let(:body) do
8581
%Q|{"foo":"bar"}\n{"moo":"lam"}|
8682
end
@@ -141,7 +137,6 @@
141137
end
142138

143139
context 'when the request needs to be URL-escaped' do
144-
145140
let(:url) do
146141
'foo%5Ebar/_msearch'
147142
end
@@ -167,7 +162,6 @@
167162
end
168163

169164
context 'when the URL params need to be URL-encoded' do
170-
171165
let(:url) do
172166
'_msearch'
173167
end
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
# Licensed to Elasticsearch B.V. under one or more contributor
2+
# license agreements. See the NOTICE file distributed with
3+
# this work for additional information regarding copyright
4+
# ownership. Elasticsearch B.V. licenses this file to you under
5+
# the Apache License, Version 2.0 (the "License"); you may
6+
# not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
require 'spec_helper'
19+
require 'elastic/transport'
20+
21+
describe 'msearch custom' do
22+
context 'raise exception' do
23+
it 'requires the :body argument' do
24+
expect do
25+
Elasticsearch::Client.new.msearch
26+
end.to raise_exception(ArgumentError)
27+
end
28+
end
29+
30+
context 'when not setting headers' do
31+
let(:client) do
32+
Elasticsearch::Client.new
33+
end
34+
35+
let(:expected_headers) do
36+
{
37+
'accept' => 'application/vnd.elasticsearch+x-ndjson; compatible-with=9',
38+
'content-type' => 'application/vnd.elasticsearch+x-ndjson; compatible-with=9'
39+
}
40+
end
41+
42+
it 'does not override headers' do
43+
allow(client)
44+
.to receive(:perform_request)
45+
.with(
46+
Elasticsearch::API::HTTP_POST,
47+
'_msearch',
48+
{},
49+
{},
50+
expected_headers,
51+
{ endpoint: 'msearch' }
52+
)
53+
expect(client.msearch(body: {})).to be_a Elasticsearch::API::Response
54+
end
55+
end
56+
57+
context 'when using compatibility headers for version 8' do
58+
let(:client) do
59+
Elasticsearch::Client.new(
60+
transport_options: {
61+
headers: custom_headers
62+
}
63+
)
64+
end
65+
66+
let(:custom_headers) do
67+
{
68+
accept: 'application/vnd.elasticsearch+json; compatible-with=8',
69+
content_type: 'application/vnd.elasticsearch+json; compatible-with=8'
70+
}
71+
end
72+
73+
let(:expected_headers) do
74+
{
75+
accept: 'application/vnd.elasticsearch+x-ndjson; compatible-with=8',
76+
content_type: 'application/vnd.elasticsearch+x-ndjson; compatible-with=8'
77+
}
78+
end
79+
80+
it 'does not override version in headers' do
81+
allow(client)
82+
.to receive(:perform_request)
83+
.with(
84+
Elasticsearch::API::HTTP_POST,
85+
'_msearch',
86+
{},
87+
{},
88+
expected_headers,
89+
{ endpoint: 'msearch' }
90+
)
91+
expect(client.msearch(body: {})).to be_a Elasticsearch::API::Response
92+
end
93+
end
94+
95+
context 'when using custom headers in request' do
96+
let(:client) do
97+
Elasticsearch::Client.new(
98+
transport_options: {
99+
headers: custom_headers
100+
}
101+
)
102+
end
103+
104+
let(:custom_headers) do
105+
{
106+
accept: 'application/vnd.elasticsearch+json; compatible-with=8',
107+
content_type: 'application/vnd.elasticsearch+json; compatible-with=8'
108+
}
109+
end
110+
111+
let(:expected_headers) do
112+
{
113+
accept: 'application/vnd.elasticsearch+x-ndjson; compatible-with=8',
114+
content_type: 'application/vnd.elasticsearch+x-ndjson; compatible-with=8',
115+
x_custom: 'Custom header'
116+
}
117+
end
118+
119+
it 'does not override version in headers' do
120+
allow(client)
121+
.to receive(:perform_request)
122+
.with(
123+
Elasticsearch::API::HTTP_POST,
124+
'_msearch',
125+
{},
126+
{},
127+
expected_headers,
128+
{ endpoint: 'msearch' }
129+
)
130+
expect(client.msearch(body: {}, headers: { x_custom: 'Custom header' })).to be_a Elasticsearch::API::Response
131+
end
132+
end
133+
end

0 commit comments

Comments
 (0)