Skip to content

Commit 45c7f4d

Browse files
committed
[API][TESTS] Updates tests for NDJSON headers endpoints
- Refactors mocking transport stuff for headers
1 parent 1139ea8 commit 45c7f4d

File tree

10 files changed

+411
-36
lines changed

10 files changed

+411
-36
lines changed

elasticsearch-api/spec/spec_helper.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ def self.included(context)
5959
end
6060
end
6161

62+
Transport ||= Struct.new('Transport', :options)
63+
64+
def dummy_ndjson_headers
65+
allow(client_double).to receive(:transport).and_return Transport.new({ transport_options: { headers: {} } })
66+
allow(Elasticsearch::API::Utils).to receive(:update_ndjson_headers!).and_return({})
67+
end
68+
6269
RSpec.configure do |config|
6370
config.include(HelperModule)
6471
config.filter_run_excluding skip: true

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

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,28 +24,17 @@
2424
'_fleet/_fleet_msearch',
2525
{},
2626
{},
27-
headers,
27+
{},
2828
{ endpoint: 'fleet.msearch' }
2929
]
3030
end
3131

32-
let(:headers) {
33-
{
34-
'Content-Type' => 'application/vnd.elasticsearch+x-ndjson; compatible-with=9'
35-
}
36-
}
32+
# This test only cares about the body, there's another test for the ndjson headers.
33+
before do
34+
dummy_ndjson_headers
35+
end
3736

3837
it 'performs the request' do
3938
expect(client_double.fleet.msearch(body: {})).to be_a Elasticsearch::API::Response
4039
end
41-
42-
let(:client) do
43-
Class.new { include Elasticsearch::API }.new
44-
end
45-
46-
it 'requires the :body argument' do
47-
expect {
48-
client.fleet.msearch
49-
}.to raise_exception(ArgumentError)
50-
end
5140
end

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,9 @@
4545
{}
4646
end
4747

48-
let(:transport_double) do
49-
Transport ||= Struct.new('Transport', :options)
50-
Transport.new({ transport_options: { headers: {} } })
51-
end
52-
48+
# This test only cares about the body, there's another test for the ndjson headers.
5349
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)
50+
dummy_ndjson_headers
5651
end
5752

5853
context 'when the body is an object' do

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,12 @@
3737
{}
3838
end
3939

40-
let(:headers) {
41-
{
42-
'Content-Type' => 'application/vnd.elasticsearch+x-ndjson; compatible-with=9'
43-
}
44-
}
40+
let(:headers) {{}}
41+
42+
# This test only cares about the body, there's another test for the ndjson headers.
43+
before do
44+
dummy_ndjson_headers
45+
end
4546

4647
let(:url) do
4748
'_msearch/template'

elasticsearch-api/spec/unit/actions/text_structure/find_structure_spec.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,23 @@
1717

1818
require 'spec_helper'
1919

20-
describe 'client.text_structure#test_grok_pattern' do
20+
describe 'client.text_structure#find_structure' do
2121
let(:expected_args) do
2222
[
2323
'POST',
2424
'_text_structure/find_structure',
2525
{},
2626
{},
27-
headers,
27+
{},
2828
{ endpoint: 'text_structure.find_structure' }
2929
]
3030
end
3131

32-
let(:headers) {
33-
{
34-
'Content-Type' => 'application/vnd.elasticsearch+x-ndjson; compatible-with=9'
35-
}
36-
}
32+
# This test only cares about the body, there's another test for the ndjson headers.
33+
before do
34+
dummy_ndjson_headers
35+
allow(client_double.text_structure.client).to receive(:transport).and_return Transport.new({ transport_options: { headers: {} } })
36+
end
3737

3838
it 'performs the request' do
3939
expect(client_double.text_structure.find_structure(body: {})).to be_a Elasticsearch::API::Response
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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+
20+
describe 'text_structure.find_structure headers test' do
21+
context 'when not setting headers' do
22+
let(:client) do
23+
Elasticsearch::Client.new
24+
end
25+
26+
let(:expected_headers) do
27+
{
28+
'accept' => 'application/vnd.elasticsearch+x-ndjson; compatible-with=9',
29+
'content-type' => 'application/vnd.elasticsearch+x-ndjson; compatible-with=9'
30+
}
31+
end
32+
33+
it 'does not override headers' do
34+
allow(client)
35+
.to receive(:perform_request)
36+
.with(
37+
Elasticsearch::API::HTTP_POST,
38+
'_text_structure/find_structure',
39+
{},
40+
{},
41+
expected_headers,
42+
{ endpoint: 'text_structure.find_structure' }
43+
)
44+
expect(client.text_structure.find_structure(body: {})).to be_a Elasticsearch::API::Response
45+
end
46+
end
47+
48+
context 'when using compatibility headers for version 8' do
49+
let(:client) do
50+
Elasticsearch::Client.new(
51+
transport_options: {
52+
headers: custom_headers
53+
}
54+
)
55+
end
56+
57+
let(:custom_headers) do
58+
{
59+
accept: 'application/vnd.elasticsearch+json; compatible-with=8',
60+
content_type: 'application/vnd.elasticsearch+json; compatible-with=8'
61+
}
62+
end
63+
64+
let(:expected_headers) do
65+
{
66+
accept: 'application/vnd.elasticsearch+x-ndjson; compatible-with=8',
67+
content_type: 'application/vnd.elasticsearch+x-ndjson; compatible-with=8'
68+
}
69+
end
70+
71+
it 'does not override version in headers' do
72+
allow(client)
73+
.to receive(:perform_request)
74+
.with(
75+
Elasticsearch::API::HTTP_POST,
76+
'_text_structure/find_structure',
77+
{},
78+
{},
79+
expected_headers,
80+
{ endpoint: 'text_structure.find_structure' }
81+
)
82+
expect(client.text_structure.find_structure(body: {})).to be_a Elasticsearch::API::Response
83+
end
84+
end
85+
86+
context 'when using custom headers in request' do
87+
let(:client) do
88+
Elasticsearch::Client.new(
89+
transport_options: {
90+
headers: custom_headers
91+
}
92+
)
93+
end
94+
95+
let(:custom_headers) do
96+
{
97+
accept: 'application/vnd.elasticsearch+json; compatible-with=8',
98+
content_type: 'application/vnd.elasticsearch+json; compatible-with=8'
99+
}
100+
end
101+
102+
let(:expected_headers) do
103+
{
104+
accept: 'application/vnd.elasticsearch+x-ndjson; compatible-with=8',
105+
content_type: 'application/vnd.elasticsearch+x-ndjson; compatible-with=8',
106+
x_custom: 'Custom header'
107+
}
108+
end
109+
110+
it 'does not override version in headers' do
111+
allow(client)
112+
.to receive(:perform_request)
113+
.with(
114+
Elasticsearch::API::HTTP_POST,
115+
'_text_structure/find_structure',
116+
{},
117+
{},
118+
expected_headers,
119+
{ endpoint: 'text_structure.find_structure' }
120+
)
121+
expect(client.text_structure.find_structure(body: {}, headers: { x_custom: 'Custom header' })).to be_a Elasticsearch::API::Response
122+
end
123+
end
124+
end
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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+
20+
describe 'fleet.msearch exception' do
21+
let(:client) do
22+
Class.new { include Elasticsearch::API }.new
23+
end
24+
25+
it 'requires the :body argument' do
26+
expect {
27+
client.fleet.msearch
28+
}.to raise_exception(ArgumentError)
29+
end
30+
end
31+
32+
describe 'fleet.msearch headers test' do
33+
context 'when not setting headers' do
34+
let(:client) do
35+
Elasticsearch::Client.new
36+
end
37+
38+
let(:expected_headers) do
39+
{
40+
'accept' => 'application/vnd.elasticsearch+x-ndjson; compatible-with=9',
41+
'content-type' => 'application/vnd.elasticsearch+x-ndjson; compatible-with=9'
42+
}
43+
end
44+
45+
it 'does not override headers' do
46+
allow(client)
47+
.to receive(:perform_request)
48+
.with(
49+
Elasticsearch::API::HTTP_POST,
50+
'_fleet/_fleet_msearch',
51+
{},
52+
{},
53+
expected_headers,
54+
{ endpoint: 'fleet.msearch' }
55+
)
56+
expect(client.fleet.msearch(body: {})).to be_a Elasticsearch::API::Response
57+
end
58+
end
59+
60+
context 'when using compatibility headers for version 8' do
61+
let(:client) do
62+
Elasticsearch::Client.new(
63+
transport_options: {
64+
headers: custom_headers
65+
}
66+
)
67+
end
68+
69+
let(:custom_headers) do
70+
{
71+
accept: 'application/vnd.elasticsearch+json; compatible-with=8',
72+
content_type: 'application/vnd.elasticsearch+json; compatible-with=8'
73+
}
74+
end
75+
76+
let(:expected_headers) do
77+
{
78+
accept: 'application/vnd.elasticsearch+x-ndjson; compatible-with=8',
79+
content_type: 'application/vnd.elasticsearch+x-ndjson; compatible-with=8'
80+
}
81+
end
82+
83+
it 'does not override version in headers' do
84+
allow(client)
85+
.to receive(:perform_request)
86+
.with(
87+
Elasticsearch::API::HTTP_POST,
88+
'_fleet/_fleet_msearch',
89+
{},
90+
{},
91+
expected_headers,
92+
{ endpoint: 'fleet.msearch' }
93+
)
94+
expect(client.fleet.msearch(body: {})).to be_a Elasticsearch::API::Response
95+
end
96+
end
97+
98+
context 'when using custom headers in request' do
99+
let(:client) do
100+
Elasticsearch::Client.new(
101+
transport_options: {
102+
headers: custom_headers
103+
}
104+
)
105+
end
106+
107+
let(:custom_headers) do
108+
{
109+
accept: 'application/vnd.elasticsearch+json; compatible-with=8',
110+
content_type: 'application/vnd.elasticsearch+json; compatible-with=8'
111+
}
112+
end
113+
114+
let(:expected_headers) do
115+
{
116+
accept: 'application/vnd.elasticsearch+x-ndjson; compatible-with=8',
117+
content_type: 'application/vnd.elasticsearch+x-ndjson; compatible-with=8',
118+
x_custom: 'Custom header'
119+
}
120+
end
121+
122+
it 'does not override version in headers' do
123+
allow(client)
124+
.to receive(:perform_request)
125+
.with(
126+
Elasticsearch::API::HTTP_POST,
127+
'_fleet/_fleet_msearch',
128+
{},
129+
{},
130+
expected_headers,
131+
{ endpoint: 'fleet.msearch' }
132+
)
133+
expect(client.fleet.msearch(body: {}, headers: { x_custom: 'Custom header' })).to be_a Elasticsearch::API::Response
134+
end
135+
end
136+
end

elasticsearch-api/spec/unit/msearch_spec.rb renamed to elasticsearch-api/spec/unit/ndjson_endpoints/msearch_spec.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
# under the License.
1717

1818
require 'spec_helper'
19-
require 'elastic/transport'
2019

2120
describe 'msearch custom' do
2221
context 'raise exception' do

0 commit comments

Comments
 (0)