elasticsearch-transport 7.5.0 → 7.17.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +26 -13
  3. data/README.md +159 -64
  4. data/Rakefile +25 -13
  5. data/elasticsearch-transport.gemspec +57 -63
  6. data/lib/elasticsearch/transport/client.rb +183 -58
  7. data/lib/elasticsearch/transport/meta_header.rb +135 -0
  8. data/lib/elasticsearch/transport/redacted.rb +16 -3
  9. data/lib/elasticsearch/transport/transport/base.rb +69 -30
  10. data/lib/elasticsearch/transport/transport/connections/collection.rb +18 -8
  11. data/lib/elasticsearch/transport/transport/connections/connection.rb +25 -9
  12. data/lib/elasticsearch/transport/transport/connections/selector.rb +16 -3
  13. data/lib/elasticsearch/transport/transport/errors.rb +17 -3
  14. data/lib/elasticsearch/transport/transport/http/curb.rb +60 -35
  15. data/lib/elasticsearch/transport/transport/http/faraday.rb +32 -9
  16. data/lib/elasticsearch/transport/transport/http/manticore.rb +51 -31
  17. data/lib/elasticsearch/transport/transport/loggable.rb +16 -3
  18. data/lib/elasticsearch/transport/transport/response.rb +16 -4
  19. data/lib/elasticsearch/transport/transport/serializer/multi_json.rb +16 -3
  20. data/lib/elasticsearch/transport/transport/sniffer.rb +35 -15
  21. data/lib/elasticsearch/transport/version.rb +17 -4
  22. data/lib/elasticsearch/transport.rb +35 -33
  23. data/lib/elasticsearch-transport.rb +16 -3
  24. data/spec/elasticsearch/connections/collection_spec.rb +28 -3
  25. data/spec/elasticsearch/connections/selector_spec.rb +16 -3
  26. data/spec/elasticsearch/transport/base_spec.rb +104 -43
  27. data/spec/elasticsearch/transport/client_spec.rb +727 -163
  28. data/spec/elasticsearch/transport/http/curb_spec.rb +126 -0
  29. data/spec/elasticsearch/transport/http/faraday_spec.rb +141 -0
  30. data/spec/elasticsearch/transport/http/manticore_spec.rb +143 -0
  31. data/spec/elasticsearch/transport/meta_header_spec.rb +301 -0
  32. data/spec/elasticsearch/transport/sniffer_spec.rb +16 -16
  33. data/spec/spec_helper.rb +28 -6
  34. data/test/integration/jruby_test.rb +43 -0
  35. data/test/integration/transport_test.rb +46 -29
  36. data/test/profile/client_benchmark_test.rb +16 -3
  37. data/test/test_helper.rb +22 -25
  38. data/test/unit/connection_test.rb +23 -5
  39. data/test/unit/response_test.rb +18 -5
  40. data/test/unit/serializer_test.rb +16 -3
  41. data/test/unit/transport_base_test.rb +33 -11
  42. data/test/unit/transport_curb_test.rb +16 -4
  43. data/test/unit/transport_faraday_test.rb +18 -5
  44. data/test/unit/transport_manticore_test.rb +258 -158
  45. metadata +80 -71
@@ -1,169 +1,269 @@
1
- # Licensed to Elasticsearch B.V under one or more agreements.
2
- # Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3
- # See the LICENSE file in the project root for more information
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.
4
17
 
5
18
  require 'test_helper'
6
19
 
7
- unless JRUBY
8
- version = ( defined?(RUBY_ENGINE) ? RUBY_ENGINE : 'Ruby' ) + ' ' + RUBY_VERSION
9
- puts "SKIP: '#{File.basename(__FILE__)}' only supported on JRuby (you're running #{version})"
10
- else
20
+ if JRUBY
11
21
  require 'elasticsearch/transport/transport/http/manticore'
12
22
  require 'manticore'
13
23
 
14
- class Elasticsearch::Transport::Transport::HTTP::ManticoreTest < Minitest::Test
15
- include Elasticsearch::Transport::Transport::HTTP
16
-
17
- context "Manticore transport" do
18
- setup do
19
- @transport = Manticore.new :hosts => [ { :host => '127.0.0.1', :port => 8080 } ]
20
- end
21
-
22
- should "implement host_unreachable_exceptions" do
23
- assert_instance_of Array, @transport.host_unreachable_exceptions
24
- end
25
-
26
- should "implement __build_connections" do
27
- assert_equal 1, @transport.hosts.size
28
- assert_equal 1, @transport.connections.size
29
-
30
- assert_instance_of ::Manticore::Client, @transport.connections.first.connection
31
- end
32
-
33
- should "not close connections in __close_connections" do
34
- assert_equal 1, @transport.connections.size
35
- @transport.__close_connections
36
- assert_equal 1, @transport.connections.size
37
- end
38
-
39
- should "perform the request" do
40
- @transport.connections.first.connection.expects(:get).returns(stub_everything)
41
- @transport.perform_request 'GET', '/'
42
- end
43
-
44
- should "set body for GET request" do
45
- @transport.connections.first.connection.expects(:get).
46
- with('http://127.0.0.1:8080//', {:body => '{"foo":"bar"}',
47
- :headers => {"Content-Type" => "application/json",
48
- "User-Agent" => @transport.send(:user_agent_header)}}).returns(stub_everything)
49
- @transport.perform_request 'GET', '/', {}, '{"foo":"bar"}'
50
- end
51
-
52
- should "set body for PUT request" do
53
- @transport.connections.first.connection.expects(:put).
54
- with('http://127.0.0.1:8080//', {:body => '{"foo":"bar"}',
55
- :headers => {"Content-Type" => "application/json",
56
- "User-Agent" => @transport.send(:user_agent_header)}}).returns(stub_everything)
57
- @transport.perform_request 'PUT', '/', {}, {:foo => 'bar'}
58
- end
59
-
60
- should "serialize the request body" do
61
- @transport.connections.first.connection.expects(:post).
62
- with('http://127.0.0.1:8080//', {:body => '{"foo":"bar"}',
63
- :headers => {"Content-Type" => "application/json",
64
- "User-Agent" => @transport.send(:user_agent_header)}}).returns(stub_everything)
65
- @transport.perform_request 'POST', '/', {}, {'foo' => 'bar'}
66
- end
67
-
68
- should "set custom headers for PUT request" do
69
- @transport.connections.first.connection.expects(:put).
70
- with('http://127.0.0.1:8080//', {:body => '{"foo":"bar"}',
71
- :headers => {"Content-Type" => "application/json",
72
- "User-Agent" => @transport.send(:user_agent_header)}})
73
- .returns(stub_everything)
74
- @transport.perform_request 'PUT', '/', {}, '{"foo":"bar"}', {"Content-Type" => "application/x-ndjson"}
75
- end
76
-
77
- should "not serialize a String request body" do
78
- @transport.connections.first.connection.expects(:post).
79
- with('http://127.0.0.1:8080//', {:body => '{"foo":"bar"}',
80
- :headers => {"Content-Type" => "application/json",
81
- "User-Agent" => @transport.send(:user_agent_header)}}).returns(stub_everything)
82
- @transport.serializer.expects(:dump).never
83
- @transport.perform_request 'POST', '/', {}, '{"foo":"bar"}'
84
- end
85
-
86
- should "set application/json header" do
87
- options = {
88
- :headers => { "content-type" => "application/json"}
89
- }
90
-
91
- transport = Manticore.new :hosts => [ { :host => 'localhost', :port => 8080 } ], :options => options
92
-
93
- transport.connections.first.connection.stub("http://localhost:8080//", :body => "\"\"", :headers => {"Content-Type" => "application/x-ndjson",
94
- "User-Agent" => @transport.send(:user_agent_header)}, :code => 200 )
95
-
96
- response = transport.perform_request 'GET', '/', {}
97
- assert_equal response.status, 200
98
- end
99
-
100
- should "set headers from 'transport_options'" do
101
- options = {
102
- :transport_options => {
103
- :headers => { "Content-Type" => "foo/bar"}
104
- }
105
- }
106
-
107
- transport = Manticore.new :hosts => [ { :host => 'localhost', :port => 8080 } ], :options => options
108
-
109
- assert_equal('foo/bar', transport.connections.first.connection.instance_variable_get(:@options)[:headers]['Content-Type'])
110
- # TODO: Needs to check @request_options
111
- end
112
-
113
- should "handle HTTP methods" do
114
- @transport.connections.first.connection.expects(:delete).with('http://127.0.0.1:8080//', { headers: {"Content-Type" => "application/json",
115
- "User-Agent" => @transport.send(:user_agent_header)}}).returns(stub_everything)
116
- @transport.connections.first.connection.expects(:head).with('http://127.0.0.1:8080//', { headers: {"Content-Type" => "application/json",
117
- "User-Agent" => @transport.send(:user_agent_header)}}).returns(stub_everything)
118
- @transport.connections.first.connection.expects(:get).with('http://127.0.0.1:8080//', { headers: {"Content-Type" => "application/json",
119
- "User-Agent" => @transport.send(:user_agent_header)}}).returns(stub_everything)
120
- @transport.connections.first.connection.expects(:put).with('http://127.0.0.1:8080//', { headers: {"Content-Type" => "application/json",
121
- "User-Agent" => @transport.send(:user_agent_header)}}).returns(stub_everything)
122
- @transport.connections.first.connection.expects(:post).with('http://127.0.0.1:8080//', { headers: {"Content-Type" => "application/json",
123
- "User-Agent" => @transport.send(:user_agent_header)}}).returns(stub_everything)
124
-
125
- %w| HEAD GET PUT POST DELETE |.each { |method| @transport.perform_request method, '/' }
126
-
127
- assert_raise(ArgumentError) { @transport.perform_request 'FOOBAR', '/' }
128
- end
129
-
130
- should "allow to set options for Manticore" do
131
- options = { :headers => {"User-Agent" => "myapp-0.0" }}
132
- transport = Manticore.new :hosts => [ { :host => 'foobar', :port => 1234 } ], :options => options
133
- transport.connections.first.connection
134
- .expects(:get)
135
- .with do |host, options|
136
- assert_equal 'myapp-0.0', options[:headers]['User-Agent']
137
- true
24
+ module Elasticsearch
25
+ module Transport
26
+ module Transport
27
+ module HTTP
28
+ class ManticoreTest < Minitest::Test
29
+ include Elasticsearch::Transport::Transport::HTTP
30
+
31
+ def common_headers
32
+ {
33
+ 'Content-Type' => 'application/json',
34
+ 'User-Agent' => @transport.send(:user_agent_header)
35
+ }
36
+ end
37
+
38
+ context 'Manticore transport' do
39
+ setup do
40
+ @transport = Manticore.new(hosts: [{ host: '127.0.0.1', port: 8080 }])
41
+ end
42
+
43
+ should 'implement host_unreachable_exceptions' do
44
+ assert_instance_of Array, @transport.host_unreachable_exceptions
45
+ end
46
+
47
+ should 'implement __build_connections' do
48
+ assert_equal 1, @transport.hosts.size
49
+ assert_equal 1, @transport.connections.size
50
+
51
+ assert_instance_of(::Manticore::Client, @transport.connections.first.connection)
52
+ end
53
+
54
+ should 'not close connections in __close_connections' do
55
+ assert_equal 1, @transport.connections.size
56
+ @transport.__close_connections
57
+ assert_equal 1, @transport.connections.size
58
+ end
59
+
60
+ should 'perform the request' do
61
+ @transport.connections.first.connection.expects(:get).returns(stub_everything)
62
+ response = @transport.perform_request('GET', '/')
63
+ end
64
+
65
+ should 'set body for GET request' do
66
+ @transport.connections.first.connection.expects(:get)
67
+ .with(
68
+ 'http://127.0.0.1:8080/',
69
+ {
70
+ body: '{"foo":"bar"}',
71
+ headers: common_headers
72
+ }
73
+ ).returns(stub_everything)
74
+ @transport.perform_request 'GET', '/', {}, '{"foo":"bar"}'
75
+ end
76
+
77
+ should 'set body for PUT request' do
78
+ @transport.connections.first.connection.expects(:put)
79
+ .with(
80
+ 'http://127.0.0.1:8080/',
81
+ {
82
+ body: '{"foo":"bar"}',
83
+ headers: {
84
+ 'Content-Type' => 'application/json',
85
+ 'User-Agent' => @transport.send(:user_agent_header)
86
+ }
87
+ }
88
+ ).returns(stub_everything)
89
+ @transport.perform_request 'PUT', '/', {}, { foo: 'bar' }
90
+ end
91
+
92
+ should 'serialize the request body' do
93
+ @transport.connections.first.connection.expects(:post)
94
+ .with(
95
+ 'http://127.0.0.1:8080/',
96
+ {
97
+ body: '{"foo":"bar"}',
98
+ headers: {
99
+ 'Content-Type' => 'application/json',
100
+ 'User-Agent' => @transport.send(:user_agent_header)
101
+ }
102
+ }
103
+ ).returns(stub_everything)
104
+ @transport.perform_request 'POST', '/', {}, { 'foo' => 'bar' }
105
+ end
106
+
107
+ should 'set custom headers for PUT request' do
108
+ @transport.connections.first.connection.expects(:put)
109
+ .with(
110
+ 'http://127.0.0.1:8080/',
111
+ {
112
+ body: '{"foo":"bar"}',
113
+ headers: {
114
+ 'Content-Type' => 'application/json',
115
+ 'User-Agent' => @transport.send(:user_agent_header)
116
+ }
117
+ }
118
+ ).returns(stub_everything)
119
+ @transport.perform_request 'PUT', '/', {}, '{"foo":"bar"}', { 'Content-Type' => 'application/x-ndjson' }
120
+ end
121
+
122
+ should 'not serialize a String request body' do
123
+ @transport.connections.first.connection.expects(:post)
124
+ .with(
125
+ 'http://127.0.0.1:8080/',
126
+ {
127
+ body: '{"foo":"bar"}',
128
+ headers: {
129
+ 'Content-Type' => 'application/json',
130
+ 'User-Agent' => @transport.send(:user_agent_header)
131
+ }
132
+ }
133
+ ).returns(stub_everything)
134
+ @transport.serializer.expects(:dump).never
135
+ @transport.perform_request 'POST', '/', {}, '{"foo":"bar"}'
136
+ end
137
+
138
+ should 'set application/json header' do
139
+ options = {
140
+ headers: { 'content-type' => 'application/json' }
141
+ }
142
+
143
+ transport = Manticore.new(hosts: [{ host: 'localhost', port: 8080 }], options: options)
144
+ transport.connections.first.connection.stub(
145
+ 'http://localhost:8080/',
146
+ body: '""',
147
+ headers: {
148
+ 'Content-Type' => 'application/x-ndjson',
149
+ 'User-Agent' => @transport.send(:user_agent_header)
150
+ },
151
+ code: 200
152
+ )
153
+ response = transport.perform_request('GET', '/', {})
154
+ assert_equal response.status, 200
155
+ end
156
+
157
+ should "set headers from 'transport_options'" do
158
+ options = {
159
+ transport_options: {
160
+ headers: { 'Content-Type' => 'foo/bar' }
161
+ }
162
+ }
163
+
164
+ transport = Manticore.new(hosts: [{ host: 'localhost', port: 8080 }], options: options)
165
+
166
+ assert_equal(
167
+ 'foo/bar',
168
+ transport.connections.first.connection.instance_variable_get(:@options)[:headers]['Content-Type']
169
+ )
170
+ # TODO: Needs to check @request_options
171
+ end
172
+
173
+ should 'handle HTTP methods' do
174
+ @transport.connections.first.connection.expects(:delete).with('http://127.0.0.1:8080/', { headers: common_headers }).returns(stub_everything)
175
+ @transport.connections.first.connection.expects(:head).with('http://127.0.0.1:8080/', { headers: common_headers }).returns(stub_everything)
176
+ @transport.connections.first.connection.expects(:get).with('http://127.0.0.1:8080/', { headers: common_headers }).returns(stub_everything)
177
+ @transport.connections.first.connection.expects(:put).with('http://127.0.0.1:8080/', { headers: common_headers }).returns(stub_everything)
178
+ @transport.connections.first.connection.expects(:post).with('http://127.0.0.1:8080/', { headers: common_headers }).returns(stub_everything)
179
+
180
+ %w[HEAD GET PUT POST DELETE].each { |method| @transport.perform_request method, '/' }
181
+
182
+ assert_raise(ArgumentError) { @transport.perform_request 'FOOBAR', '/' }
183
+ end
184
+
185
+ should 'allow to set options for Manticore' do
186
+ options = { headers: { 'User-Agent' => 'myapp-0.0' } }
187
+ transport = Manticore.new(hosts: [{ host: 'foobar', port: 1234 }], options: options)
188
+ transport.connections.first.connection
189
+ .expects(:get)
190
+ .with do |_host, _options|
191
+ assert_equal 'myapp-0.0', _options[:headers]['User-Agent']
192
+ true
193
+ end
194
+ .returns(stub_everything)
195
+
196
+ transport.perform_request 'GET', '/', {}
197
+ end
198
+
199
+ should 'allow to set ssl options for Manticore' do
200
+ options = {
201
+ ssl: {
202
+ truststore: 'test.jks',
203
+ truststore_password: 'test',
204
+ verify: false
205
+ }
206
+ }
207
+
208
+ ::Manticore::Client.expects(:new).with(options)
209
+ transport = Manticore.new(hosts: [{ host: 'foobar', port: 1234 }], options: options)
210
+ assert_equal(transport.options[:ssl][:truststore_password], 'test')
211
+ end
212
+
213
+ should 'pass :transport_options to Manticore::Client' do
214
+ options = {
215
+ transport_options: { potatoes: 1 }
216
+ }
217
+
218
+ ::Manticore::Client.expects(:new).with(potatoes: 1, ssl: {})
219
+ transport = Manticore.new(hosts: [{ host: 'foobar', port: 1234 }], options: options)
220
+ assert_equal(transport.options[:transport_options][:potatoes], 1)
221
+ end
222
+
223
+ context 'custom headers' do
224
+ should 'allow authorization headers' do
225
+ transport_options = { headers: { 'Authorization' => 'Basic token' } }
226
+ transport = Manticore.new(
227
+ hosts: [{ host: 'foobar', port: 1234 }],
228
+ transport_options: transport_options
229
+ )
230
+
231
+ assert_equal(
232
+ transport.instance_variable_get(:@request_options)[:headers]['Authorization'],
233
+ 'Basic token'
234
+ )
235
+ transport.connections.first.connection.expects(:get).with do |_host, options|
236
+ assert_equal('Basic token', options[:headers]['Authorization'])
237
+ true
238
+ end.returns(stub_everything)
239
+ transport.perform_request('GET', '/', {})
240
+ end
241
+
242
+ should 'allow user agent headers' do
243
+ transport_options = { headers: { 'User-Agent' => 'Custom UA' } }
244
+ transport = Manticore.new(
245
+ hosts: [{ host: 'localhost' }],
246
+ transport_options: transport_options
247
+ )
248
+ transport.connections.first.connection.expects(:get).with do |_host, options|
249
+ assert_equal('Custom UA', options[:headers]['User-Agent'])
250
+ true
251
+ end.returns(stub_everything)
252
+ transport.perform_request('GET', '/', {})
253
+
254
+ assert_equal(
255
+ transport.instance_variable_get('@request_options')[:headers]['User-Agent'],
256
+ 'Custom UA'
257
+ )
258
+ end
259
+ end
260
+ end
138
261
  end
139
- .returns(stub_everything)
140
-
141
- transport.perform_request 'GET', '/', {}
142
- end
143
-
144
- should "allow to set ssl options for Manticore" do
145
- options = {
146
- :ssl => {
147
- :truststore => "test.jks",
148
- :truststore_password => "test",
149
- :verify => false
150
- }
151
- }
152
-
153
- ::Manticore::Client.expects(:new).with(options)
154
- transport = Manticore.new :hosts => [ { :host => 'foobar', :port => 1234 } ], :options => options
155
- end
156
-
157
- should "pass :transport_options to Manticore::Client" do
158
- options = {
159
- :transport_options => { :potatoes => 1 }
160
- }
161
-
162
- ::Manticore::Client.expects(:new).with(:potatoes => 1, :ssl => {})
163
- transport = Manticore.new :hosts => [ { :host => 'foobar', :port => 1234 } ], :options => options
262
+ end
164
263
  end
165
264
  end
166
-
167
265
  end
168
-
266
+ else
267
+ version = "#{defined?(RUBY_ENGINE) ? RUBY_ENGINE : 'Ruby'} #{RUBY_VERSION}"
268
+ puts "SKIP: '#{File.basename(__FILE__)}' only supported on JRuby (you're running #{version})"
169
269
  end