elasticsearch-transport 7.4.0 → 7.17.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +30 -13
  3. data/Gemfile-faraday1.gemfile +47 -0
  4. data/README.md +159 -64
  5. data/Rakefile +63 -13
  6. data/elasticsearch-transport.gemspec +55 -63
  7. data/lib/elasticsearch/transport/client.rb +184 -59
  8. data/lib/elasticsearch/transport/meta_header.rb +135 -0
  9. data/lib/elasticsearch/transport/redacted.rb +16 -3
  10. data/lib/elasticsearch/transport/transport/base.rb +69 -30
  11. data/lib/elasticsearch/transport/transport/connections/collection.rb +18 -8
  12. data/lib/elasticsearch/transport/transport/connections/connection.rb +25 -9
  13. data/lib/elasticsearch/transport/transport/connections/selector.rb +16 -3
  14. data/lib/elasticsearch/transport/transport/errors.rb +17 -3
  15. data/lib/elasticsearch/transport/transport/http/curb.rb +60 -35
  16. data/lib/elasticsearch/transport/transport/http/faraday.rb +32 -9
  17. data/lib/elasticsearch/transport/transport/http/manticore.rb +57 -32
  18. data/lib/elasticsearch/transport/transport/loggable.rb +16 -3
  19. data/lib/elasticsearch/transport/transport/response.rb +17 -5
  20. data/lib/elasticsearch/transport/transport/serializer/multi_json.rb +16 -3
  21. data/lib/elasticsearch/transport/transport/sniffer.rb +35 -15
  22. data/lib/elasticsearch/transport/version.rb +17 -4
  23. data/lib/elasticsearch/transport.rb +35 -33
  24. data/lib/elasticsearch-transport.rb +16 -3
  25. data/spec/elasticsearch/connections/collection_spec.rb +28 -3
  26. data/spec/elasticsearch/connections/selector_spec.rb +16 -3
  27. data/spec/elasticsearch/transport/base_spec.rb +107 -49
  28. data/spec/elasticsearch/transport/client_spec.rb +734 -164
  29. data/spec/elasticsearch/transport/http/curb_spec.rb +126 -0
  30. data/spec/elasticsearch/transport/http/faraday_spec.rb +141 -0
  31. data/spec/elasticsearch/transport/http/manticore_spec.rb +161 -0
  32. data/spec/elasticsearch/transport/meta_header_spec.rb +301 -0
  33. data/spec/elasticsearch/transport/sniffer_spec.rb +16 -16
  34. data/spec/spec_helper.rb +32 -6
  35. data/test/integration/jruby_test.rb +43 -0
  36. data/test/integration/transport_test.rb +109 -46
  37. data/test/profile/client_benchmark_test.rb +16 -3
  38. data/test/test_helper.rb +26 -25
  39. data/test/unit/adapters_test.rb +88 -0
  40. data/test/unit/connection_test.rb +23 -5
  41. data/test/unit/response_test.rb +18 -5
  42. data/test/unit/serializer_test.rb +16 -3
  43. data/test/unit/transport_base_test.rb +33 -11
  44. data/test/unit/transport_curb_test.rb +16 -4
  45. data/test/unit/transport_faraday_test.rb +18 -5
  46. data/test/unit/transport_manticore_test.rb +258 -158
  47. metadata +64 -76
@@ -0,0 +1,126 @@
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
+ unless defined?(JRUBY_VERSION)
19
+ require_relative '../../../spec_helper'
20
+
21
+ describe Elasticsearch::Transport::Transport::HTTP::Curb do
22
+ let(:client) do
23
+ Elasticsearch::Transport::Client.new(transport_class: described_class)
24
+ end
25
+
26
+ describe '#perform_request' do
27
+ subject(:perform_request) { client.perform_request(*args) }
28
+ let(:args) do
29
+ ['POST', '/', {}, body, headers]
30
+ end
31
+ let(:body) { '{"foo":"bar"}' }
32
+ let(:headers) { { 'Content-Type' => 'application/x-ndjson' } }
33
+
34
+ before do
35
+ allow_any_instance_of(Curl::Easy).to receive(:http).and_return(true)
36
+ end
37
+
38
+ it 'convert body to json' do
39
+ expect(client.transport).to receive(:__convert_to_json).with(body)
40
+ perform_request
41
+ end
42
+
43
+ it 'call compress_request' do
44
+ expect(client.transport).to receive(:compress_request).with(body, headers)
45
+ perform_request
46
+ end
47
+
48
+ it 'return response' do
49
+ expect(perform_request).to be_kind_of(Elasticsearch::Transport::Transport::Response)
50
+ end
51
+
52
+ it 'put body' do
53
+ expect(client.transport.connections.first.connection).to receive('put_data=').with(body)
54
+ perform_request
55
+ end
56
+
57
+ context 'when body nil' do
58
+ let(:body) { nil }
59
+
60
+ it 'convert body to json' do
61
+ expect(client.transport).not_to receive(:__convert_to_json)
62
+ perform_request
63
+ end
64
+
65
+ it 'call compress_request' do
66
+ expect(client.transport).to receive(:compress_request).with(body, headers)
67
+ perform_request
68
+ end
69
+
70
+ it 'put body' do
71
+ expect(client.transport.connections.first.connection).not_to receive('put_data=')
72
+ perform_request
73
+ end
74
+ end
75
+
76
+ context 'when body is hash' do
77
+ let(:body) { { foo: 'bar' } }
78
+ let(:body_string) { '{"foo":"bar"}' }
79
+
80
+ it 'convert body to json' do
81
+ expect(client.transport).to receive(:__convert_to_json).with(body)
82
+ perform_request
83
+ end
84
+
85
+ it 'call compress_request' do
86
+ expect(client.transport).to receive(:compress_request).with(body_string, headers)
87
+ perform_request
88
+ end
89
+
90
+ it 'put body' do
91
+ expect(client.transport.connections.first.connection).to receive('put_data=').with(body_string)
92
+ perform_request
93
+ end
94
+ end
95
+
96
+ context 'when compression enabled' do
97
+ let(:client) do
98
+ Elasticsearch::Transport::Client.new(transport_class: described_class, compression: true)
99
+ end
100
+ let(:body_string) { '{"foo":"bar"}' }
101
+ let(:compressed_body) do
102
+ gzip = Zlib::GzipWriter.new(StringIO.new)
103
+ gzip << body_string
104
+ gzip.close.string
105
+ end
106
+
107
+ before { allow(client.transport).to receive(:decompress_response).and_return('') }
108
+
109
+ it 'put compressed body' do
110
+ expect(client.transport.connections.first.connection).to receive('put_data=').with(compressed_body)
111
+ perform_request
112
+ end
113
+
114
+ it 'set Content-Encoding header' do
115
+ perform_request
116
+ expect(client.transport.connections.first.connection.headers).to include('Content-Encoding')
117
+ end
118
+
119
+ it 'set Content-Encoding to gzip' do
120
+ perform_request
121
+ expect(client.transport.connections.first.connection.headers['Content-Encoding']).to eql('gzip')
122
+ end
123
+ end
124
+ end
125
+ end
126
+ end
@@ -0,0 +1,141 @@
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_relative '../../../spec_helper'
19
+
20
+ describe Elasticsearch::Transport::Transport::HTTP::Faraday do
21
+ let(:client) do
22
+ Elasticsearch::Transport::Client.new(transport_class: described_class)
23
+ end
24
+
25
+ describe '#perform_request' do
26
+ subject(:perform_request) { client.perform_request(*args) }
27
+ let(:args) do
28
+ ['POST', '/', {}, body, headers]
29
+ end
30
+ let(:body) { '{"foo":"bar"}' }
31
+ let(:headers) { { 'Content-Type' => 'application/x-ndjson' } }
32
+ let(:response) { instance_double('Faraday::Response', status: 200, body: '', headers: headers) }
33
+ let(:expected_headers) do
34
+ client.transport.connections.first.connection.headers.merge(headers)
35
+ end
36
+
37
+ before do
38
+ allow_any_instance_of(Faraday::Connection).to receive(:run_request).and_return(response)
39
+ end
40
+
41
+ it 'convert body to json' do
42
+ expect(client.transport).to receive(:__convert_to_json).with(body)
43
+ perform_request
44
+ end
45
+
46
+ it 'call compress_request' do
47
+ expect(client.transport).to receive(:compress_request).with(body, expected_headers)
48
+ perform_request
49
+ end
50
+
51
+ it 'return response' do
52
+ expect(perform_request).to be_kind_of(Elasticsearch::Transport::Transport::Response)
53
+ end
54
+
55
+ it 'run body with preper params' do
56
+ expect(
57
+ client.transport.connections.first.connection
58
+ ).to receive(:run_request).with(:post, 'http://localhost:9200/', body, expected_headers).and_return(response)
59
+ perform_request
60
+ end
61
+
62
+ context 'when body nil' do
63
+ let(:body) { nil }
64
+ let(:request_params) { [:post, 'http://localhost:9200/', body, expected_headers] }
65
+
66
+ it 'convert body to json' do
67
+ expect(client.transport).not_to receive(:__convert_to_json)
68
+ perform_request
69
+ end
70
+
71
+ it 'call compress_request' do
72
+ expect(client.transport).to receive(:compress_request).with(body, expected_headers)
73
+ perform_request
74
+ end
75
+
76
+ it 'run body with preper params' do
77
+ expect(
78
+ client.transport.connections.first.connection
79
+ ).to receive(:run_request).with(*request_params).and_return(response)
80
+ perform_request
81
+ end
82
+ end
83
+
84
+ context 'when body is hash' do
85
+ let(:body) { { foo: 'bar' } }
86
+ let(:body_string) { '{"foo":"bar"}' }
87
+ let(:request_params) { [:post, 'http://localhost:9200/', body_string, expected_headers] }
88
+
89
+ it 'convert body to json' do
90
+ expect(client.transport).to receive(:__convert_to_json).with(body)
91
+ perform_request
92
+ end
93
+
94
+ it 'call compress_request' do
95
+ expect(client.transport).to receive(:compress_request).with(body_string, expected_headers)
96
+ perform_request
97
+ end
98
+
99
+ it 'run body with preper params' do
100
+ expect(
101
+ client.transport.connections.first.connection
102
+ ).to receive(:run_request).with(*request_params).and_return(response)
103
+ perform_request
104
+ end
105
+ end
106
+
107
+ context 'when compression enabled' do
108
+ let(:client) do
109
+ Elasticsearch::Transport::Client.new(transport_class: described_class, compression: true)
110
+ end
111
+ let(:body_string) { '{"foo":"bar"}' }
112
+ let(:expected_headers) { super().merge({ "Content-Encoding" => "gzip", "Accept-Encoding" => "gzip"}) }
113
+ let(:request_params) { [:post, 'http://localhost:9200/', compressed_body, expected_headers] }
114
+ let(:compressed_body) do
115
+ gzip = Zlib::GzipWriter.new(StringIO.new)
116
+ gzip << body_string
117
+ gzip.close.string
118
+ end
119
+
120
+ it 'run body with preper params' do
121
+ expect(
122
+ client.transport.connections.first.connection
123
+ ).to receive(:run_request).with(*request_params).and_return(response)
124
+ perform_request
125
+ end
126
+
127
+ context 'when client makes second request with nil boby' do
128
+ before { perform_request }
129
+
130
+ it 'remove Content-Encoding header' do
131
+ expected_headers.delete("Content-Encoding")
132
+ expect(
133
+ client.transport.connections.first.connection
134
+ ).to receive(:run_request).with(:post, 'http://localhost:9200/', nil, expected_headers)
135
+ .and_return(response)
136
+ client.perform_request('POST', '/', {}, nil, headers)
137
+ end
138
+ end
139
+ end
140
+ end
141
+ end
@@ -0,0 +1,161 @@
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
+ if defined?(JRUBY_VERSION)
19
+ require_relative '../../../spec_helper'
20
+
21
+ describe Elasticsearch::Transport::Transport::HTTP::Manticore do
22
+ let(:client) do
23
+ Elasticsearch::Transport::Client.new(transport_class: described_class)
24
+ end
25
+
26
+ describe '#perform_request' do
27
+ subject(:perform_request) { client.perform_request(*args) }
28
+ let(:args) do
29
+ ['POST', '/', {}, body, headers]
30
+ end
31
+ let(:body) { '{"foo":"bar"}' }
32
+ let(:headers) { { 'Content-Type' => 'application/json' } }
33
+ let(:response) { instance_double('Manticore::Response', code: 200, read_body: '', headers: headers) }
34
+ let(:expected_headers) do
35
+ client.transport.instance_variable_get('@request_options')[:headers].merge(headers)
36
+ end
37
+
38
+ before do
39
+ allow_any_instance_of(Manticore::Client).to receive(:post).and_return(response)
40
+ end
41
+
42
+ it 'convert body to json' do
43
+ expect(client.transport).to receive(:__convert_to_json).with(body)
44
+ perform_request
45
+ end
46
+
47
+ it 'call compress_request' do
48
+ expect(client.transport).to receive(:compress_request).with(body, expected_headers)
49
+ perform_request
50
+ end
51
+
52
+ it 'return response' do
53
+ expect(perform_request).to be_kind_of(Elasticsearch::Transport::Transport::Response)
54
+ end
55
+
56
+ it 'run body with proper params' do
57
+ expect(
58
+ client.transport.connections.first.connection
59
+ ).to receive(:post).with('http://localhost:9200/', { body: body, headers: expected_headers }).and_return(response)
60
+ perform_request
61
+ end
62
+
63
+ context 'when body nil' do
64
+ let(:body) { nil }
65
+ let(:request_params) { ['http://localhost:9200/', { body: body, headers: expected_headers }] }
66
+
67
+ it 'convert body to json' do
68
+ expect(client.transport).not_to receive(:__convert_to_json)
69
+ perform_request
70
+ end
71
+
72
+ it 'call compress_request' do
73
+ expect(client.transport).to receive(:compress_request).with(body, expected_headers)
74
+ perform_request
75
+ end
76
+
77
+ it 'run body with preper params' do
78
+ expect(
79
+ client.transport.connections.first.connection
80
+ ).to receive(:post).with('http://localhost:9200/', { headers: expected_headers }).and_return(response)
81
+ perform_request
82
+ end
83
+ end
84
+
85
+ context 'when body is hash' do
86
+ let(:body) { { foo: 'bar' } }
87
+ let(:body_string) { '{"foo":"bar"}' }
88
+ let(:request_params) { ['http://localhost:9200/', { body: body_string, headers: expected_headers }] }
89
+
90
+ it 'convert body to json' do
91
+ expect(client.transport).to receive(:__convert_to_json).with(body)
92
+ perform_request
93
+ end
94
+
95
+ it 'call compress_request' do
96
+ expect(client.transport).to receive(:compress_request).with(body_string, expected_headers)
97
+ perform_request
98
+ end
99
+
100
+ it 'run body with preper params' do
101
+ expect(
102
+ client.transport.connections.first.connection
103
+ ).to receive(:post).with(*request_params).and_return(response)
104
+ perform_request
105
+ end
106
+ end
107
+
108
+ context 'when compression enabled' do
109
+ let(:client) do
110
+ Elasticsearch::Transport::Client.new(transport_class: described_class, compression: true)
111
+ end
112
+ let(:body_string) { '{"foo":"bar"}' }
113
+ let(:expected_headers) { super().merge({ "Content-Encoding" => "gzip", "Accept-Encoding" => "gzip"}) }
114
+ let(:request_params) { ['http://localhost:9200/', { body: compressed_body, headers: expected_headers }] }
115
+ let(:compressed_body) do
116
+ gzip = Zlib::GzipWriter.new(StringIO.new)
117
+ gzip << body_string
118
+ gzip.close.string
119
+ end
120
+
121
+ it 'run body with preper params' do
122
+ expect(
123
+ client.transport.connections.first.connection
124
+ ).to receive(:post).with(*request_params).and_return(response)
125
+ perform_request
126
+ end
127
+
128
+ context 'when client makes second request with nil boby' do
129
+ before { perform_request }
130
+
131
+ it 'remove Content-Encoding header' do
132
+ expected_headers.delete("Content-Encoding")
133
+ expect(
134
+ client.transport.connections.first.connection
135
+ ).to receive(:post).with('http://localhost:9200/', { headers: expected_headers })
136
+ .and_return(response)
137
+ client.perform_request('POST', '/', {}, nil, headers)
138
+ end
139
+ end
140
+ end
141
+
142
+ context 'headers' do
143
+ it 'sends custom headers' do
144
+ client = Elasticsearch::Transport::Client.new(
145
+ transport_class: described_class,
146
+ transport_options: { headers: { 'Elastic-Api-Version'=>'2023-10-31' } }
147
+ )
148
+ expect(
149
+ client.transport.connections.first.connection
150
+ ).to receive(:get).with(
151
+ 'http://localhost:9200/',
152
+ {
153
+ headers: expected_headers.merge({ 'Elastic-Api-Version'=>'2023-10-31' })
154
+ }
155
+ ).and_return(response)
156
+ client.perform_request('GET', '/', {}, nil, headers)
157
+ end
158
+ end
159
+ end
160
+ end
161
+ end