elastic-transport 8.0.0 → 8.4.0
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.
- checksums.yaml +4 -4
- data/.github/workflows/license.yml +2 -2
- data/.github/workflows/otel.yml +48 -0
- data/.github/workflows/tests.yml +45 -5
- data/.gitignore +1 -1
- data/CHANGELOG.md +131 -8
- data/CONTRIBUTING.md +64 -0
- data/Gemfile +10 -9
- data/Gemfile-faraday1.gemfile +40 -0
- data/README.md +7 -528
- data/Rakefile +48 -1
- data/elastic-transport.gemspec +6 -9
- data/lib/elastic/transport/client.rb +66 -45
- data/lib/elastic/transport/meta_header.rb +21 -12
- data/lib/elastic/transport/opentelemetry.rb +166 -0
- data/lib/elastic/transport/transport/base.rb +74 -54
- data/lib/elastic/transport/transport/errors.rb +2 -4
- data/lib/elastic/transport/transport/http/curb.rb +30 -26
- data/lib/elastic/transport/transport/http/faraday.rb +30 -27
- data/lib/elastic/transport/transport/http/manticore.rb +10 -4
- data/lib/elastic/transport/transport/response.rb +3 -3
- data/lib/elastic/transport/transport/serializer/multi_json.rb +3 -3
- data/lib/elastic/transport/transport/sniffer.rb +3 -1
- data/lib/elastic/transport/version.rb +1 -1
- data/lib/elastic/transport.rb +1 -0
- data/spec/elastic/transport/base_spec.rb +26 -25
- data/spec/elastic/transport/client_spec.rb +91 -18
- data/spec/elastic/transport/http/manticore_spec.rb +20 -2
- data/spec/elastic/transport/meta_header_spec.rb +26 -11
- data/spec/elastic/transport/opentelemetry_spec.rb +325 -0
- data/spec/elastic/transport/sniffer_spec.rb +18 -0
- data/spec/spec_helper.rb +16 -1
- data/test/integration/jruby_test.rb +1 -1
- data/test/integration/transport_test.rb +86 -40
- data/test/test_helper.rb +9 -6
- data/test/unit/adapters_test.rb +104 -0
- data/test/unit/connection_test.rb +35 -37
- data/test/unit/transport_base_test.rb +7 -8
- data/test/unit/transport_curb_test.rb +2 -3
- data/test/unit/transport_manticore_test.rb +1 -1
- metadata +23 -76
@@ -0,0 +1,325 @@
|
|
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
|
+
if defined?(::OpenTelemetry)
|
21
|
+
describe Elastic::Transport::OpenTelemetry do
|
22
|
+
let(:exporter) { EXPORTER }
|
23
|
+
before { exporter.reset }
|
24
|
+
after { exporter.reset }
|
25
|
+
let(:span) { exporter.finished_spans[0] }
|
26
|
+
|
27
|
+
let(:client) do
|
28
|
+
Elastic::Transport::Client.new(hosts: ELASTICSEARCH_HOSTS).tap do |_client|
|
29
|
+
allow(_client).to receive(:__build_connections)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
let(:otel) { described_class.new }
|
34
|
+
|
35
|
+
context 'when the client is created with a tracer provider' do
|
36
|
+
let(:tracer_provider) do
|
37
|
+
double('tracer_provider').tap do |tp|
|
38
|
+
expect(tp).to receive(:tracer).with(
|
39
|
+
Elastic::Transport::OpenTelemetry::OTEL_TRACER_NAME, Elastic::Transport::VERSION
|
40
|
+
)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'uses the tracer provider to get a tracer' do
|
45
|
+
Elastic::Transport::Client.new(opentelemetry_tracer_provider: tracer_provider)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
context 'when path parameters' do
|
50
|
+
before do
|
51
|
+
client.perform_request('DELETE', '/users', nil, nil, nil)
|
52
|
+
rescue
|
53
|
+
end
|
54
|
+
after do
|
55
|
+
client.perform_request('DELETE', '/users', nil, nil, nil)
|
56
|
+
rescue
|
57
|
+
end
|
58
|
+
|
59
|
+
it 'creates a span with path parameters' do
|
60
|
+
client.perform_request(
|
61
|
+
'POST', '/users/_create/abc', nil, { name: 'otel-test' }, nil,
|
62
|
+
defined_params: {'index' => 'users', 'id' => 'abc'}, endpoint: 'create'
|
63
|
+
)
|
64
|
+
|
65
|
+
span = exporter.finished_spans.find { |s| s.name == 'create' }
|
66
|
+
expect(span.name).to eql('create')
|
67
|
+
expect(span.attributes['db.system']).to eql('elasticsearch')
|
68
|
+
expect(span.attributes['db.elasticsearch.path_parts.index']).to eql('users')
|
69
|
+
expect(span.attributes['db.elasticsearch.path_parts.id']).to eq('abc')
|
70
|
+
expect(span.attributes['db.operation']).to eq('create')
|
71
|
+
expect(span.attributes['db.statement']).to be_nil
|
72
|
+
expect(span.attributes['http.request.method']).to eq('POST')
|
73
|
+
expect(span.attributes['server.address']).to eq('localhost')
|
74
|
+
expect(span.attributes['server.port']).to eq(TEST_PORT.to_i)
|
75
|
+
end
|
76
|
+
|
77
|
+
context 'with list a path parameter' do
|
78
|
+
it 'creates a span with path parameters' do
|
79
|
+
client.perform_request(
|
80
|
+
'GET', '_cluster/state/foo,bar', {}, nil, {},
|
81
|
+
{ defined_params: { metric: ['foo', 'bar']}, endpoint: 'cluster.state' }
|
82
|
+
)
|
83
|
+
|
84
|
+
span = exporter.finished_spans.find { |s| s.name == 'cluster.state' }
|
85
|
+
expect(span.name).to eql('cluster.state')
|
86
|
+
expect(span.attributes['db.system']).to eql('elasticsearch')
|
87
|
+
expect(span.attributes['db.elasticsearch.path_parts.metric']).to eql('foo,bar')
|
88
|
+
expect(span.attributes['db.operation']).to eq('cluster.state')
|
89
|
+
expect(span.attributes['db.statement']).to be_nil
|
90
|
+
expect(span.attributes['http.request.method']).to eq('GET')
|
91
|
+
expect(span.attributes['server.address']).to eq('localhost')
|
92
|
+
expect(span.attributes['server.port']).to eq(TEST_PORT.to_i)
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
context 'when a request is instrumented' do
|
98
|
+
let(:body) do
|
99
|
+
{ query: { match: { password: { query: 'secret'} } } }
|
100
|
+
end
|
101
|
+
|
102
|
+
it 'creates a span and omits db.statement' do
|
103
|
+
client.perform_request('GET', '/_search', nil, body, nil, endpoint: 'search')
|
104
|
+
|
105
|
+
expect(span.name).to eql('search')
|
106
|
+
expect(span.attributes['db.system']).to eql('elasticsearch')
|
107
|
+
expect(span.attributes['db.operation']).to eq('search')
|
108
|
+
expect(span.attributes['db.statement']).to be_nil
|
109
|
+
expect(span.attributes['http.request.method']).to eq('GET')
|
110
|
+
expect(span.attributes['server.address']).to eq('localhost')
|
111
|
+
expect(span.attributes['server.port']).to eq(TEST_PORT.to_i)
|
112
|
+
end
|
113
|
+
|
114
|
+
context 'when body is sanitized' do
|
115
|
+
context 'no custom keys' do
|
116
|
+
let(:sanitized_body) do
|
117
|
+
{ query: { match: { password: 'REDACTED' } } }
|
118
|
+
end
|
119
|
+
|
120
|
+
around(:example) do |ex|
|
121
|
+
body_strategy = ENV[described_class::ENV_VARIABLE_BODY_STRATEGY]
|
122
|
+
ENV[described_class::ENV_VARIABLE_BODY_STRATEGY] = 'sanitize'
|
123
|
+
ex.run
|
124
|
+
ENV[described_class::ENV_VARIABLE_BODY_STRATEGY] = body_strategy
|
125
|
+
end
|
126
|
+
|
127
|
+
it 'sanitizes the body' do
|
128
|
+
client.perform_request('GET', '/_search', nil, body, nil, endpoint: 'search')
|
129
|
+
|
130
|
+
expect(span.attributes['db.statement']).to eq(sanitized_body.to_json)
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
context 'with deprecated ENV variable' do
|
135
|
+
let(:sanitized_body) do
|
136
|
+
{ query: { match: { password: 'REDACTED' } } }
|
137
|
+
end
|
138
|
+
|
139
|
+
around(:example) do |ex|
|
140
|
+
body_strategy = ENV[described_class::ENV_VARIABLE_DEPRECATED_BODY_STRATEGY]
|
141
|
+
ENV[described_class::ENV_VARIABLE_DEPRECATED_BODY_STRATEGY] = 'sanitize'
|
142
|
+
ex.run
|
143
|
+
ENV[described_class::ENV_VARIABLE_DEPRECATED_BODY_STRATEGY] = body_strategy
|
144
|
+
end
|
145
|
+
|
146
|
+
it 'sanitizes the body' do
|
147
|
+
client.perform_request('GET', '/_search', nil, body, nil, endpoint: 'search')
|
148
|
+
|
149
|
+
expect(span.attributes['db.statement']).to eq(sanitized_body.to_json)
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
context 'with custom keys' do
|
154
|
+
let(:body) do
|
155
|
+
{ query: { match: { sensitive: { query: 'secret'} } } }
|
156
|
+
end
|
157
|
+
|
158
|
+
let(:sanitized_body) do
|
159
|
+
{ query: { match: { sensitive: 'REDACTED' } } }
|
160
|
+
end
|
161
|
+
|
162
|
+
around(:example) do |ex|
|
163
|
+
body_strategy = ENV[described_class::ENV_VARIABLE_BODY_STRATEGY]
|
164
|
+
ENV[described_class::ENV_VARIABLE_BODY_STRATEGY] = 'sanitize'
|
165
|
+
|
166
|
+
keys = ENV[described_class::ENV_VARIABLE_BODY_SANITIZE_KEYS]
|
167
|
+
ENV[described_class::ENV_VARIABLE_BODY_SANITIZE_KEYS] = 'sensitive'
|
168
|
+
|
169
|
+
ex.run
|
170
|
+
|
171
|
+
ENV[described_class::ENV_VARIABLE_BODY_STRATEGY] = body_strategy
|
172
|
+
ENV[described_class::ENV_VARIABLE_BODY_SANITIZE_KEYS] = keys
|
173
|
+
end
|
174
|
+
|
175
|
+
it 'sanitizes the body' do
|
176
|
+
client.perform_request('GET', '/_search', nil, body, nil, endpoint: 'search')
|
177
|
+
|
178
|
+
expect(span.attributes['db.statement']).to eq(sanitized_body.to_json)
|
179
|
+
end
|
180
|
+
end
|
181
|
+
end
|
182
|
+
|
183
|
+
context 'when body strategy is set to raw' do
|
184
|
+
let(:body) do
|
185
|
+
{ query: { match: { sensitive: { query: 'secret'} } } }
|
186
|
+
end
|
187
|
+
|
188
|
+
around(:example) do |ex|
|
189
|
+
body_strategy = ENV[described_class::ENV_VARIABLE_BODY_STRATEGY]
|
190
|
+
ENV[described_class::ENV_VARIABLE_BODY_STRATEGY] = 'raw'
|
191
|
+
ex.run
|
192
|
+
ENV[described_class::ENV_VARIABLE_BODY_STRATEGY] = body_strategy
|
193
|
+
end
|
194
|
+
|
195
|
+
context 'when the body is a string' do
|
196
|
+
it 'includes the raw body' do
|
197
|
+
client.perform_request('GET', '/_search', nil, body.to_json, nil, endpoint: 'search')
|
198
|
+
expect(span.attributes['db.statement']).to eq(body.to_json)
|
199
|
+
end
|
200
|
+
end
|
201
|
+
|
202
|
+
context' when the body is a hash' do
|
203
|
+
it 'includes the raw body' do
|
204
|
+
client.perform_request('GET', '/_search', nil, body, nil, endpoint: 'search')
|
205
|
+
expect(span.attributes['db.statement']).to eq(body.to_json)
|
206
|
+
end
|
207
|
+
end
|
208
|
+
end
|
209
|
+
|
210
|
+
context 'when body strategy is set to omit' do
|
211
|
+
let(:body) do
|
212
|
+
{ query: { match: { sensitive: { query: 'secret'} } } }
|
213
|
+
end
|
214
|
+
|
215
|
+
around(:example) do |ex|
|
216
|
+
body_strategy = ENV[described_class::ENV_VARIABLE_BODY_STRATEGY]
|
217
|
+
ENV[described_class::ENV_VARIABLE_BODY_STRATEGY] = 'omit'
|
218
|
+
ex.run
|
219
|
+
ENV[described_class::ENV_VARIABLE_BODY_STRATEGY] = body_strategy
|
220
|
+
end
|
221
|
+
|
222
|
+
it 'does not include anything' do
|
223
|
+
client.perform_request('GET', '/_search', nil, body, nil, endpoint: 'search')
|
224
|
+
expect(span.attributes['db.statement']).to be_nil
|
225
|
+
end
|
226
|
+
end
|
227
|
+
|
228
|
+
context 'a non-search endpoint' do
|
229
|
+
let(:body) do
|
230
|
+
{ query: { match: { something: "test" } } }
|
231
|
+
end
|
232
|
+
|
233
|
+
it 'does not capture db.statement' do
|
234
|
+
client.perform_request(
|
235
|
+
'POST', '_all/_delete_by_query', nil, body, nil, endpoint: 'delete_by_query'
|
236
|
+
)
|
237
|
+
|
238
|
+
expect(span.attributes['db.statement']).to be_nil
|
239
|
+
end
|
240
|
+
end
|
241
|
+
|
242
|
+
context 'when no endpoint or defined params are provided' do
|
243
|
+
it 'creates a span with default values' do
|
244
|
+
client.perform_request(
|
245
|
+
'GET', '_cluster/state/foo,bar', {}, nil, {}
|
246
|
+
)
|
247
|
+
|
248
|
+
span = exporter.finished_spans.find { |s| s.name == 'GET' }
|
249
|
+
expect(span.name).to eql('GET')
|
250
|
+
expect(span.attributes['db.system']).to eql('elasticsearch')
|
251
|
+
expect(span.attributes['db.elasticsearch.path_parts']).to be_nil
|
252
|
+
expect(span.attributes['db.operation']).to be_nil
|
253
|
+
expect(span.attributes['db.statement']).to be_nil
|
254
|
+
expect(span.attributes['http.request.method']).to eq('GET')
|
255
|
+
expect(span.attributes['server.address']).to eq('localhost')
|
256
|
+
expect(span.attributes['server.port']).to eq(TEST_PORT.to_i)
|
257
|
+
end
|
258
|
+
end
|
259
|
+
end
|
260
|
+
|
261
|
+
context 'when the ENV variable OTEL_RUBY_INSTRUMENTATION_ELASTICSEARCH_ENABLED is set' do
|
262
|
+
context 'to true' do
|
263
|
+
around do |ex|
|
264
|
+
original_setting = ENV[described_class::ENV_VARIABLE_ENABLED]
|
265
|
+
ENV[described_class::ENV_VARIABLE_ENABLED] = 'true'
|
266
|
+
ex.run
|
267
|
+
ENV[described_class::ENV_VARIABLE_ENABLED] = original_setting
|
268
|
+
end
|
269
|
+
|
270
|
+
it 'instruments' do
|
271
|
+
client.perform_request('GET', '/_search', nil, nil, nil, endpoint: 'search')
|
272
|
+
expect(span.name).to eq('search')
|
273
|
+
end
|
274
|
+
end
|
275
|
+
|
276
|
+
context 'to false' do
|
277
|
+
around do |ex|
|
278
|
+
original_setting = ENV[described_class::ENV_VARIABLE_ENABLED]
|
279
|
+
ENV[described_class::ENV_VARIABLE_ENABLED] = 'false'
|
280
|
+
ex.run
|
281
|
+
ENV[described_class::ENV_VARIABLE_ENABLED] = original_setting
|
282
|
+
end
|
283
|
+
|
284
|
+
it 'does not instrument' do
|
285
|
+
client.perform_request('GET', '/_search', nil, nil, nil, endpoint: 'search')
|
286
|
+
expect(span).to be_nil
|
287
|
+
end
|
288
|
+
end
|
289
|
+
end
|
290
|
+
|
291
|
+
describe Elastic::Transport::OpenTelemetry::Sanitizer do
|
292
|
+
let(:key_patterns) { nil }
|
293
|
+
|
294
|
+
context '#sanitize' do
|
295
|
+
let(:body) do
|
296
|
+
{ query: { match: { password: "test" } } }
|
297
|
+
end
|
298
|
+
|
299
|
+
let(:expected_body) do
|
300
|
+
{ query: { match: { password: "REDACTED" } } }
|
301
|
+
end
|
302
|
+
|
303
|
+
it 'redacts sensitive values' do
|
304
|
+
expect(described_class.sanitize(body, key_patterns)).to eq(expected_body)
|
305
|
+
end
|
306
|
+
|
307
|
+
context 'with specified key patterns' do
|
308
|
+
let(:key_patterns) { [/something/] }
|
309
|
+
|
310
|
+
let(:body) do
|
311
|
+
{ query: { match: { something: "test" } } }
|
312
|
+
end
|
313
|
+
|
314
|
+
let(:expected_body) do
|
315
|
+
{ query: { match: { something: "REDACTED" } } }
|
316
|
+
end
|
317
|
+
|
318
|
+
it 'redacts sensitive values' do
|
319
|
+
expect(described_class.sanitize(body, key_patterns)).to eq(expected_body)
|
320
|
+
end
|
321
|
+
end
|
322
|
+
end
|
323
|
+
end
|
324
|
+
end
|
325
|
+
end
|
@@ -257,6 +257,24 @@ describe Elastic::Transport::Transport::Sniffer do
|
|
257
257
|
end
|
258
258
|
end
|
259
259
|
|
260
|
+
context 'when the address is IPv4' do
|
261
|
+
let(:publish_address) do
|
262
|
+
'inet[/127.0.0.1:9200]'
|
263
|
+
end
|
264
|
+
|
265
|
+
it 'parses the response' do
|
266
|
+
expect(hosts.size).to eq(1)
|
267
|
+
end
|
268
|
+
|
269
|
+
it 'correctly parses the host' do
|
270
|
+
expect(hosts[0][:host]).to eq('127.0.0.1')
|
271
|
+
end
|
272
|
+
|
273
|
+
it 'correctly parses the port' do
|
274
|
+
expect(hosts[0][:port]).to eq('9200')
|
275
|
+
end
|
276
|
+
end
|
277
|
+
|
260
278
|
context 'when the transport has :randomize_hosts option' do
|
261
279
|
let(:raw_response) do
|
262
280
|
{ 'nodes' => { 'n1' => { 'http' => { 'publish_address' => '127.0.0.1:9250' } },
|
data/spec/spec_helper.rb
CHANGED
@@ -21,7 +21,6 @@ end
|
|
21
21
|
|
22
22
|
require 'elastic-transport'
|
23
23
|
require 'logger'
|
24
|
-
require 'ansi/code'
|
25
24
|
require 'hashie/mash'
|
26
25
|
if defined?(JRUBY_VERSION)
|
27
26
|
require 'elastic/transport/transport/http/manticore'
|
@@ -74,6 +73,10 @@ def default_client
|
|
74
73
|
$client ||= Elastic::Transport::Client.new(hosts: ELASTICSEARCH_HOSTS)
|
75
74
|
end
|
76
75
|
|
76
|
+
def is_faraday_v2?
|
77
|
+
Gem::Version.new(Faraday::VERSION) >= Gem::Version.new(2)
|
78
|
+
end
|
79
|
+
|
77
80
|
module Config
|
78
81
|
def self.included(context)
|
79
82
|
# Get the hosts to use to connect an elasticsearch client.
|
@@ -88,3 +91,15 @@ RSpec.configure do |config|
|
|
88
91
|
config.formatter = 'documentation'
|
89
92
|
config.color = true
|
90
93
|
end
|
94
|
+
|
95
|
+
if ENV['TEST_WITH_OTEL'] == 'true'
|
96
|
+
require 'opentelemetry-sdk'
|
97
|
+
EXPORTER = OpenTelemetry::SDK::Trace::Export::InMemorySpanExporter.new
|
98
|
+
span_processor = OpenTelemetry::SDK::Trace::Export::SimpleSpanProcessor.new(EXPORTER)
|
99
|
+
|
100
|
+
OpenTelemetry::SDK.configure do |c|
|
101
|
+
c.error_handler = ->(exception:, message:) { raise(exception || message) }
|
102
|
+
c.logger = Logger.new($stderr, level: ENV.fetch('OTEL_LOG_LEVEL', 'fatal').to_sym)
|
103
|
+
c.add_span_processor span_processor
|
104
|
+
end
|
105
|
+
end
|
@@ -18,9 +18,8 @@
|
|
18
18
|
require 'test_helper'
|
19
19
|
|
20
20
|
class Elastic::Transport::ClientIntegrationTest < Minitest::Test
|
21
|
-
context
|
21
|
+
context 'Transport' do
|
22
22
|
setup do
|
23
|
-
begin; Object.send(:remove_const, :Patron); rescue NameError; end
|
24
23
|
uri = URI(HOST)
|
25
24
|
@host = {
|
26
25
|
host: uri.host,
|
@@ -30,69 +29,116 @@ class Elastic::Transport::ClientIntegrationTest < Minitest::Test
|
|
30
29
|
}
|
31
30
|
end
|
32
31
|
|
33
|
-
should
|
34
|
-
require 'typhoeus'
|
35
|
-
require 'typhoeus/adapters/faraday'
|
36
|
-
|
32
|
+
should 'use the default Faraday adapter' do
|
37
33
|
transport = Elastic::Transport::Transport::HTTP::Faraday.new(hosts: [@host]) do |f|
|
38
34
|
f.response :logger
|
39
|
-
f.adapter :typhoeus
|
40
35
|
end
|
41
36
|
|
42
37
|
client = Elastic::Transport::Client.new(transport: transport)
|
38
|
+
assert_equal(client.transport.connections.first.connection.adapter, Faraday::Adapter::NetHttp)
|
43
39
|
client.perform_request 'GET', ''
|
44
|
-
end
|
40
|
+
end
|
45
41
|
|
46
|
-
|
47
|
-
|
42
|
+
unless jruby?
|
43
|
+
should 'allow to customize the Faraday adapter to Typhoeus' do
|
44
|
+
if is_faraday_v2?
|
45
|
+
require 'faraday/typhoeus'
|
46
|
+
else
|
47
|
+
require 'typhoeus'
|
48
|
+
end
|
49
|
+
|
50
|
+
transport = Elastic::Transport::Transport::HTTP::Faraday.new(hosts: [@host]) do |f|
|
51
|
+
f.response :logger
|
52
|
+
f.adapter :typhoeus
|
53
|
+
end
|
54
|
+
|
55
|
+
client = Elastic::Transport::Client.new(transport: transport)
|
56
|
+
assert_equal(client.transport.connections.first.connection.adapter, Faraday::Adapter::Typhoeus)
|
57
|
+
client.perform_request 'GET', ''
|
58
|
+
end
|
48
59
|
|
49
|
-
|
50
|
-
|
51
|
-
|
60
|
+
should 'use the Curb client' do
|
61
|
+
require 'curb'
|
62
|
+
require 'elastic/transport/transport/http/curb'
|
63
|
+
|
64
|
+
transport = Elastic::Transport::Transport::HTTP::Curb.new(hosts: [@host]) do |curl|
|
65
|
+
curl.verbose = true
|
66
|
+
end
|
67
|
+
|
68
|
+
client = Elastic::Transport::Client.new(transport: transport)
|
69
|
+
assert_equal(client.transport.class, Elastic::Transport::Transport::HTTP::Curb)
|
70
|
+
client.perform_request 'GET', ''
|
52
71
|
end
|
53
72
|
|
54
|
-
|
55
|
-
|
56
|
-
|
73
|
+
should 'deserialize JSON responses in the Curb client' do
|
74
|
+
require 'curb'
|
75
|
+
require 'elastic/transport/transport/http/curb'
|
57
76
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
options: { transport_options: { params: { format: 'yaml' } } }
|
62
|
-
)
|
77
|
+
transport = Elastic::Transport::Transport::HTTP::Curb.new(hosts: [@host]) do |curl|
|
78
|
+
curl.verbose = true
|
79
|
+
end
|
63
80
|
|
64
|
-
|
65
|
-
|
81
|
+
client = Elastic::Transport::Client.new(transport: transport)
|
82
|
+
response = client.perform_request 'GET', ''
|
66
83
|
|
67
|
-
|
84
|
+
assert_respond_to(response.body, :to_hash)
|
85
|
+
assert_not_nil response.body['name']
|
86
|
+
assert_equal 'application/json', response.headers['content-type']
|
87
|
+
assert_equal 'Elasticsearch', response.headers['x-elastic-product']
|
88
|
+
end
|
89
|
+
|
90
|
+
should 'allow to customize the Faraday adapter to Patron' do
|
91
|
+
if is_faraday_v2?
|
92
|
+
require 'faraday/patron'
|
93
|
+
else
|
94
|
+
require 'patron'
|
95
|
+
end
|
96
|
+
transport = Elastic::Transport::Transport::HTTP::Faraday.new(hosts: [@host]) do |f|
|
97
|
+
f.response :logger
|
98
|
+
f.adapter :patron
|
99
|
+
end
|
100
|
+
|
101
|
+
client = Elastic::Transport::Client.new(transport: transport)
|
102
|
+
assert_equal(client.transport.connections.first.connection.adapter, Faraday::Adapter::Patron)
|
103
|
+
client.perform_request 'GET', ''
|
104
|
+
end
|
68
105
|
end
|
69
106
|
|
70
|
-
should
|
71
|
-
require '
|
72
|
-
require 'elastic/transport/transport/http/curb'
|
107
|
+
should 'allow to customize the Faraday adapter to NetHttpPersistent' do
|
108
|
+
require 'faraday/net_http_persistent'
|
73
109
|
|
74
|
-
transport = Elastic::Transport::Transport::HTTP::
|
75
|
-
|
110
|
+
transport = Elastic::Transport::Transport::HTTP::Faraday.new(hosts: [@host]) do |f|
|
111
|
+
f.response :logger
|
112
|
+
f.adapter :net_http_persistent
|
76
113
|
end
|
77
114
|
|
78
115
|
client = Elastic::Transport::Client.new(transport: transport)
|
116
|
+
assert_equal(client.transport.connections.first.connection.adapter, Faraday::Adapter::NetHttpPersistent)
|
79
117
|
client.perform_request 'GET', ''
|
80
|
-
end
|
118
|
+
end
|
81
119
|
|
82
|
-
should
|
83
|
-
require '
|
84
|
-
require 'elastic/transport/transport/http/curb'
|
120
|
+
should 'allow to customize the Faraday adapter to HTTPClient' do
|
121
|
+
require 'faraday/httpclient'
|
85
122
|
|
86
|
-
transport = Elastic::Transport::Transport::HTTP::
|
87
|
-
|
123
|
+
transport = Elastic::Transport::Transport::HTTP::Faraday.new(hosts: [@host]) do |f|
|
124
|
+
f.response :logger
|
125
|
+
f.adapter :httpclient
|
88
126
|
end
|
89
127
|
|
90
128
|
client = Elastic::Transport::Client.new(transport: transport)
|
91
|
-
|
129
|
+
assert_equal(client.transport.connections.first.connection.adapter, Faraday::Adapter::HTTPClient)
|
130
|
+
client.perform_request 'GET', ''
|
131
|
+
end
|
92
132
|
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
133
|
+
should 'allow to define connection parameters and pass them' do
|
134
|
+
transport = Elastic::Transport::Transport::HTTP::Faraday.new(
|
135
|
+
hosts: [@host],
|
136
|
+
options: { transport_options: { params: { format: 'yaml' } } }
|
137
|
+
)
|
138
|
+
|
139
|
+
client = Elastic::Transport::Client.new transport: transport
|
140
|
+
response = client.perform_request 'GET', ''
|
141
|
+
assert response.body.start_with?("---\n"), "Response body should be YAML: #{response.body.inspect}"
|
142
|
+
end
|
97
143
|
end
|
98
144
|
end
|
data/test/test_helper.rb
CHANGED
@@ -14,6 +14,7 @@
|
|
14
14
|
# KIND, either express or implied. See the License for the
|
15
15
|
# specific language governing permissions and limitations
|
16
16
|
# under the License.
|
17
|
+
require 'uri'
|
17
18
|
|
18
19
|
password = ENV['ELASTIC_PASSWORD'] || 'changeme'
|
19
20
|
host = ENV['TEST_ES_SERVER'] || 'http://localhost:9200'
|
@@ -31,15 +32,13 @@ end
|
|
31
32
|
|
32
33
|
require 'minitest/autorun'
|
33
34
|
require 'minitest/reporters'
|
34
|
-
require 'shoulda/context'
|
35
35
|
require 'mocha/minitest'
|
36
|
-
require '
|
36
|
+
require 'shoulda/context'
|
37
37
|
|
38
|
-
require 'require-prof' if ENV["REQUIRE_PROF"]
|
39
38
|
require 'elastic-transport'
|
40
|
-
require 'logger'
|
41
|
-
|
42
39
|
require 'hashie'
|
40
|
+
require 'logger'
|
41
|
+
require 'require-prof' if ENV["REQUIRE_PROF"]
|
43
42
|
|
44
43
|
RequireProf.print_timing_infos if ENV["REQUIRE_PROF"]
|
45
44
|
|
@@ -80,4 +79,8 @@ module Minitest
|
|
80
79
|
end
|
81
80
|
end
|
82
81
|
|
83
|
-
|
82
|
+
def is_faraday_v2?
|
83
|
+
Gem::Version.new(Faraday::VERSION) >= Gem::Version.new(2)
|
84
|
+
end
|
85
|
+
|
86
|
+
Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new(print_failure_summary: true)
|