elasticsearch-transport 7.10.1 → 7.12.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 420122a402e799e70ce7a879b1cabe62cd4750fc356438c0f980a2b78d852fc4
4
- data.tar.gz: 420a016f766aa19fd8c6400ae8dd78c95d76090c8de38cc1658bbd06348c3136
3
+ metadata.gz: fc7f64d6af1a163b0f6a2fb2f95aaab42e8f797b998e4a3d34fed1bc21ea3ccb
4
+ data.tar.gz: 1b00c2e0cdd62483465ca14d093475a7ec2f9d976e2e50565c8e27b44b06634d
5
5
  SHA512:
6
- metadata.gz: ed848e2746878f56203c6c3a9d73a0f995f65cf5854ef262d3504f98f6a889ee74e4c5e7c67bbe0d0a89b1a03c55ddd74e56c2d6efc65adbaabcd0f557653656
7
- data.tar.gz: c7ba8b088fad4ecb5fd55870f8c50e7fb68b66ca1c157993e69cee0db4205b6371f4b69f83431196e825f22ed08affc35be5c94f5d42b4558d22feb9e0236555
6
+ metadata.gz: '0899b3da62ed4fd0fd7d6d02defa75354d85fd05ed83c9ac3fa40e6ea597011620f84ad38c30273e7fb11deb4e34f2dd3a469407a6848f56ecdcdaf35fb53a8e'
7
+ data.tar.gz: 41e7690ac6a1d40f7632ae91a16bf707040bda14783074caceaa7f91ab1f66d4c3d58b6c5ec6b2a581c4f7d69e6db94f4e849c15f5d3b8db3bed584f943c655c
data/Gemfile CHANGED
@@ -32,7 +32,7 @@ if File.exist? File.expand_path('../../elasticsearch/elasticsearch.gemspec', __F
32
32
  gem 'elasticsearch', path: File.expand_path('../../elasticsearch', __FILE__), require: false
33
33
  end
34
34
 
35
- group :development do
35
+ group :development, :test do
36
36
  gem 'rspec'
37
37
  if defined?(JRUBY_VERSION)
38
38
  gem 'pry-nav'
data/README.md CHANGED
@@ -187,18 +187,24 @@ Elasticsearch::Client.new(
187
187
 
188
188
  ### Logging
189
189
 
190
- To log requests and responses to standard output with the default logger (an instance of Ruby's {::Logger} class),
191
- set the `log` argument:
190
+ To log requests and responses to standard output with the default logger (an instance of Ruby's {::Logger} class), set the `log` argument to true:
192
191
 
193
192
  ```ruby
194
- Elasticsearch::Client.new log: true
193
+ Elasticsearch::Client.new(log: true)
194
+ ```
195
+
196
+ You can also use [ecs-logging](https://github.com/elastic/ecs-logging-ruby). `ecs-logging` is a set of libraries that allows you to transform your application logs to structured logs that comply with the [Elastic Common Schema (ECS)](https://www.elastic.co/guide/en/ecs/current/ecs-reference.html):
197
+
198
+ ```ruby
199
+ logger = EcsLogging::Logger.new($stdout)
200
+ Elasticsearch::Client.new(logger: logger)
195
201
  ```
196
202
 
197
203
 
198
204
  To trace requests and responses in the _Curl_ format, set the `trace` argument:
199
205
 
200
206
  ```ruby
201
- Elasticsearch::Client.new trace: true
207
+ Elasticsearch::Client.new(trace: true)
202
208
  ```
203
209
 
204
210
  You can customize the default logger or tracer:
@@ -211,7 +217,7 @@ You can customize the default logger or tracer:
211
217
  Or, you can use a custom `::Logger` instance:
212
218
 
213
219
  ```ruby
214
- Elasticsearch::Client.new logger: Logger.new(STDERR)
220
+ Elasticsearch::Client.new(logger: Logger.new(STDERR))
215
221
  ```
216
222
 
217
223
  You can pass the client any conforming logger implementation:
@@ -223,7 +229,7 @@ log = Logging.logger['elasticsearch']
223
229
  log.add_appenders Logging.appenders.stdout
224
230
  log.level = :info
225
231
 
226
- client = Elasticsearch::Client.new logger: log
232
+ client = Elasticsearch::Client.new(logger: log)
227
233
  ```
228
234
 
229
235
  ### Custom HTTP Headers
@@ -16,6 +16,7 @@
16
16
  # under the License.
17
17
 
18
18
  require 'base64'
19
+ require 'elasticsearch/transport/meta_header'
19
20
 
20
21
  module Elasticsearch
21
22
  module Transport
@@ -25,6 +26,7 @@ module Elasticsearch
25
26
  # See {file:README.md README} for usage and code examples.
26
27
  #
27
28
  class Client
29
+ include MetaHeader
28
30
  DEFAULT_TRANSPORT_CLASS = Transport::HTTP::Faraday
29
31
 
30
32
  DEFAULT_LOGGER = lambda do
@@ -120,8 +122,11 @@ module Elasticsearch
120
122
  #
121
123
  # @option api_key [String, Hash] :api_key Use API Key Authentication, either the base64 encoding of `id` and `api_key`
122
124
  # joined by a colon as a String, or a hash with the `id` and `api_key` values.
123
- # @option opaque_id_prefix [String] :opaque_id_prefix set a prefix for X-Opaque-Id when initializing the client. This
124
- # will be prepended to the id you set before each request if you're using X-Opaque-Id
125
+ # @option opaque_id_prefix [String] :opaque_id_prefix set a prefix for X-Opaque-Id when initializing the client.
126
+ # This will be prepended to the id you set before each request
127
+ # if you're using X-Opaque-Id
128
+ # @option enable_meta_header [Boolean] :enable_meta_header Enable sending the meta data header to Cloud.
129
+ # (Default: true)
125
130
  #
126
131
  # @yield [faraday] Access and configure the `Faraday::Connection` instance directly with a block
127
132
  #
@@ -136,6 +141,7 @@ module Elasticsearch
136
141
  @arguments[:randomize_hosts] ||= false
137
142
  @arguments[:transport_options] ||= {}
138
143
  @arguments[:http] ||= {}
144
+ @arguments[:enable_meta_header] = arguments.fetch(:enable_meta_header) { true }
139
145
  @options[:http] ||= {}
140
146
 
141
147
  set_api_key if (@api_key = @arguments[:api_key])
@@ -158,15 +164,18 @@ module Elasticsearch
158
164
  if @arguments[:transport]
159
165
  @transport = @arguments[:transport]
160
166
  else
161
- transport_class = @arguments[:transport_class] || DEFAULT_TRANSPORT_CLASS
162
- if transport_class == Transport::HTTP::Faraday
163
- @transport = transport_class.new(hosts: @seeds, options: @arguments) do |faraday|
164
- faraday.adapter(@arguments[:adapter] || __auto_detect_adapter)
165
- block&.call faraday
166
- end
167
- else
168
- @transport = transport_class.new(hosts: @seeds, options: @arguments)
169
- end
167
+ @transport_class = @arguments[:transport_class] || DEFAULT_TRANSPORT_CLASS
168
+ @transport = if @transport_class == Transport::HTTP::Faraday
169
+ @arguments[:adapter] ||= __auto_detect_adapter
170
+ set_meta_header # from include MetaHeader
171
+ @transport_class.new(hosts: @seeds, options: @arguments) do |faraday|
172
+ faraday.adapter(@arguments[:adapter])
173
+ block&.call faraday
174
+ end
175
+ else
176
+ set_meta_header # from include MetaHeader
177
+ @transport_class.new(hosts: @seeds, options: @arguments)
178
+ end
170
179
  end
171
180
  end
172
181
 
@@ -186,13 +195,17 @@ module Elasticsearch
186
195
 
187
196
  def set_api_key
188
197
  @api_key = __encode(@api_key) if @api_key.is_a? Hash
198
+ add_header('Authorization' => "ApiKey #{@api_key}")
199
+ @arguments.delete(:user)
200
+ @arguments.delete(:password)
201
+ end
202
+
203
+ def add_header(header)
189
204
  headers = @arguments[:transport_options]&.[](:headers) || {}
190
- headers.merge!('Authorization' => "ApiKey #{@api_key}")
205
+ headers.merge!(header)
191
206
  @arguments[:transport_options].merge!(
192
207
  headers: headers
193
208
  )
194
- @arguments.delete(:user)
195
- @arguments.delete(:password)
196
209
  end
197
210
 
198
211
  def extract_cloud_creds(arguments)
@@ -0,0 +1,120 @@
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 'base64'
19
+
20
+ module Elasticsearch
21
+ module Transport
22
+
23
+ # Methods for the Elastic meta header used by Cloud.
24
+ # X-Elastic-Client-Meta HTTP header which is used by Elastic Cloud and can be disabled when
25
+ # instantiating the Client with the :enable_meta_header parameter set to `false`.
26
+ #
27
+ module MetaHeader
28
+ def set_meta_header
29
+ return if @arguments[:enable_meta_header] == false
30
+
31
+ service, version = meta_header_service_version
32
+
33
+ meta_headers = {
34
+ service.to_sym => version,
35
+ rb: RUBY_VERSION,
36
+ t: Elasticsearch::Transport::VERSION
37
+ }
38
+ meta_headers.merge!(meta_header_engine) if meta_header_engine
39
+ meta_headers.merge!(meta_header_adapter) if meta_header_adapter
40
+
41
+ add_header({ 'x-elastic-client-meta' => meta_headers.map { |k, v| "#{k}=#{v}" }.join(',') })
42
+ end
43
+
44
+ def meta_header_service_version
45
+ if defined?(Elastic::META_HEADER_SERVICE_VERSION)
46
+ Elastic::META_HEADER_SERVICE_VERSION
47
+ elsif defined?(Elasticsearch::VERSION)
48
+ [:es, client_meta_version(Elasticsearch::VERSION)]
49
+ else
50
+ [:es, client_meta_version(Elasticsearch::Transport::VERSION)]
51
+ end
52
+ end
53
+
54
+ # We return the current version if it's a release, but if it's a pre/alpha/beta release we
55
+ # return <VERSION_NUMBER>p
56
+ #
57
+ def client_meta_version(version)
58
+ regexp = /^([0-9]+\.[0-9]+\.[0-9]+)(\.?[a-z0-9.-]+)?$/
59
+ match = version.match(regexp)
60
+ return "#{match[1]}p" if (match[2])
61
+
62
+ version
63
+ end
64
+
65
+ def meta_header_engine
66
+ case RUBY_ENGINE
67
+ when 'ruby'
68
+ {}
69
+ when 'jruby'
70
+ { jv: ENV_JAVA['java.version'], jr: JRUBY_VERSION }
71
+ when 'rbx'
72
+ { rbx: RUBY_VERSION }
73
+ else
74
+ { RUBY_ENGINE.to_sym => RUBY_VERSION }
75
+ end
76
+ end
77
+
78
+ # This function tries to define the version for the Faraday adapter. If it hasn't been loaded
79
+ # by the time we're calling this method, it's going to report the adapter (if we know it) but
80
+ # return 0 as the version. It won't report anything when using a custom adapter we don't
81
+ # identify.
82
+ #
83
+ # Returns a Hash<adapter_alias, version>
84
+ #
85
+ def meta_header_adapter
86
+ if @transport_class == Transport::HTTP::Faraday
87
+ version = '0'
88
+ adapter_version = case @arguments[:adapter]
89
+ when :patron
90
+ version = Patron::VERSION if defined?(::Patron::VERSION)
91
+ {pt: version}
92
+ when :net_http
93
+ version = if defined?(Net::HTTP::VERSION)
94
+ Net::HTTP::VERSION
95
+ elsif defined?(Net::HTTP::HTTPVersion)
96
+ Net::HTTP::HTTPVersion
97
+ end
98
+ {nh: version}
99
+ when :typhoeus
100
+ version = Typhoeus::VERSION if defined?(::Typhoeus::VERSION)
101
+ {ty: version}
102
+ when :httpclient
103
+ version = HTTPClient::VERSION if defined?(HTTPClient::VERSION)
104
+ {hc: version}
105
+ when :net_http_persistent
106
+ version = Net::HTTP::Persistent::VERSION if defined?(Net::HTTP::Persistent::VERSION)
107
+ {np: version}
108
+ else
109
+ {}
110
+ end
111
+ {fd: Faraday::VERSION}.merge(adapter_version)
112
+ elsif defined?(Transport::HTTP::Curb) && @transport_class == Transport::HTTP::Curb
113
+ {cl: Curl::CURB_VERSION}
114
+ elsif defined?(Transport::HTTP::Manticore) && @transport_class == Transport::HTTP::Manticore
115
+ {mc: Manticore::VERSION}
116
+ end
117
+ end
118
+ end
119
+ end
120
+ end
@@ -33,9 +33,17 @@ module Elasticsearch
33
33
  # @return [Response]
34
34
  # @see Transport::Base#perform_request
35
35
  #
36
- def perform_request(method, path, params={}, body=nil, headers=nil, opts={})
36
+ def perform_request(method, path, params = {}, body = nil, headers = nil, opts = {})
37
37
  super do |connection, url|
38
- headers = headers || connection.connection.headers
38
+ headers = if connection.connection.headers
39
+ if !headers.nil?
40
+ connection.connection.headers.merge(headers)
41
+ else
42
+ connection.connection.headers
43
+ end
44
+ else
45
+ headers
46
+ end
39
47
 
40
48
  response = connection.connection.run_request(method.downcase.to_sym,
41
49
  url,
@@ -17,6 +17,6 @@
17
17
 
18
18
  module Elasticsearch
19
19
  module Transport
20
- VERSION = "7.10.1"
20
+ VERSION = '7.12.0'.freeze
21
21
  end
22
22
  end
@@ -246,7 +246,7 @@ describe Elasticsearch::Transport::Client do
246
246
  end
247
247
 
248
248
  let(:client) do
249
- described_class.new(adapter: :patron)
249
+ described_class.new(adapter: :patron, enable_meta_header: false)
250
250
  end
251
251
 
252
252
  it 'uses Faraday with the adapter' do
@@ -260,7 +260,7 @@ describe Elasticsearch::Transport::Client do
260
260
  end
261
261
 
262
262
  let(:client) do
263
- described_class.new(adapter: :typhoeus)
263
+ described_class.new(adapter: :typhoeus, enable_meta_header: false)
264
264
  end
265
265
 
266
266
  it 'uses Faraday with the adapter' do
@@ -274,7 +274,7 @@ describe Elasticsearch::Transport::Client do
274
274
  end
275
275
 
276
276
  let(:client) do
277
- described_class.new('adapter' => :patron)
277
+ described_class.new(adapter: :patron, enable_meta_header: false)
278
278
  end
279
279
 
280
280
  it 'uses Faraday with the adapter' do
@@ -1423,7 +1423,7 @@ describe Elasticsearch::Transport::Client do
1423
1423
  allow(client).to receive(:perform_request) { OpenStruct.new(body: '') }
1424
1424
  expect { client.search(opaque_id: 'opaque_id') }.not_to raise_error
1425
1425
  expect(client).to have_received(:perform_request)
1426
- .with('GET', '_search', { opaque_id: 'opaque_id' }, nil, {})
1426
+ .with('GET', '_search', { opaque_id: 'opaque_id' }, nil, {})
1427
1427
  end
1428
1428
  end
1429
1429
  end
@@ -1462,7 +1462,28 @@ describe Elasticsearch::Transport::Client do
1462
1462
  allow(client).to receive(:perform_request) { OpenStruct.new(body: '') }
1463
1463
  expect { client.search(headers: headers) }.not_to raise_error
1464
1464
  expect(client).to have_received(:perform_request)
1465
- .with('GET', '_search', {}, nil, headers)
1465
+ .with('GET', '_search', {}, nil, headers)
1466
+ end
1467
+ end
1468
+
1469
+ context 'when a header is set on an endpoint request and on initialization' do
1470
+ let!(:client) do
1471
+ described_class.new(
1472
+ host: hosts,
1473
+ transport_options: { headers: instance_headers }
1474
+ )
1475
+ end
1476
+ let(:instance_headers) { { set_in_instantiation: 'header value' } }
1477
+ let(:param_headers) {{'user-agent' => 'My Ruby Tests', 'set-on-method-call' => 'header value'}}
1478
+
1479
+ it 'performs the request with the header' do
1480
+ expected_headers = client.transport.connections.connections.first.connection.headers.merge(param_headers)
1481
+
1482
+ expect_any_instance_of(Faraday::Connection)
1483
+ .to receive(:run_request)
1484
+ .with(:get, "http://#{hosts[0]}/_search", nil, expected_headers) { OpenStruct.new(body: '')}
1485
+
1486
+ client.search(headers: param_headers)
1466
1487
  end
1467
1488
  end
1468
1489
  end
@@ -1684,7 +1705,7 @@ describe Elasticsearch::Transport::Client do
1684
1705
  context 'when using the HTTPClient adapter' do
1685
1706
 
1686
1707
  let(:client) do
1687
- described_class.new(hosts: ELASTICSEARCH_HOSTS, compression: true, adapter: :httpclient)
1708
+ described_class.new(hosts: ELASTICSEARCH_HOSTS, compression: true, adapter: :httpclient, enable_meta_header: false)
1688
1709
  end
1689
1710
 
1690
1711
  it 'compresses the request and decompresses the response' do
@@ -0,0 +1,265 @@
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 Elasticsearch::Transport::Client do
21
+ context 'meta-header' do
22
+ let(:subject) { client.transport.connections.first.connection.headers }
23
+ let(:client) { described_class.new }
24
+ let(:regexp) { /^[a-z]{1,}=[a-z0-9.\-]{1,}(?:,[a-z]{1,}=[a-z0-9._\-]+)*$/ }
25
+ let(:adapter) { :net_http }
26
+ let(:adapter_code) { "nh=#{defined?(Net::HTTP::VERSION) ? Net::HTTP::VERSION : Net::HTTP::HTTPVersion}" }
27
+ let(:meta_header) do
28
+ if jruby?
29
+ "es=#{meta_version},rb=#{RUBY_VERSION},t=#{Elasticsearch::Transport::VERSION},jv=#{ENV_JAVA['java.version']},jr=#{JRUBY_VERSION},fd=#{Faraday::VERSION},#{adapter_code}"
30
+ else
31
+ "es=#{meta_version},rb=#{RUBY_VERSION},t=#{Elasticsearch::Transport::VERSION},fd=#{Faraday::VERSION},#{adapter_code}"
32
+ end
33
+ end
34
+
35
+ context 'client_meta_version' do
36
+ let(:version) { ['7.1.0-alpha', '7.11.0.pre.1', '8.0.0-beta', '8.0.0.beta.2']}
37
+
38
+ it 'converts the version to X.X.Xp' do
39
+ expect(client.send(:client_meta_version, '7.0.0-alpha')).to eq('7.0.0p')
40
+ expect(client.send(:client_meta_version, '7.11.0.pre.1')).to eq('7.11.0p')
41
+ expect(client.send(:client_meta_version, '8.1.0-beta')).to eq('8.1.0p')
42
+ expect(client.send(:client_meta_version, '8.0.0.beta.2')).to eq('8.0.0p')
43
+ expect(client.send(:client_meta_version, '12.16.4.pre')).to eq('12.16.4p')
44
+ end
45
+ end
46
+
47
+ # We are testing this method in the previous block, so now using it inside the test to make the
48
+ # Elasticsearch version in the meta header string dynamic
49
+ def meta_version
50
+ client.send(:client_meta_version, Elasticsearch::VERSION)
51
+ end
52
+
53
+ context 'single use of meta header' do
54
+ let(:client) do
55
+ described_class.new(adapter: adapter).tap do |klient|
56
+ allow(klient).to receive(:__build_connections)
57
+ end
58
+ end
59
+
60
+ it 'x-elastic-client-header value matches regexp' do
61
+ expect(subject['x-elastic-client-meta']).to match(regexp)
62
+ expect(subject).to include('x-elastic-client-meta' => meta_header)
63
+ end
64
+ end
65
+
66
+ context 'when using user-agent headers' do
67
+ let(:client) do
68
+ transport_options = { headers: { user_agent: 'My Ruby App' } }
69
+ described_class.new(transport_options: transport_options, adapter: adapter).tap do |klient|
70
+ allow(klient).to receive(:__build_connections)
71
+ end
72
+ end
73
+
74
+ it 'is friendly to previously set headers' do
75
+ expect(subject).to include(user_agent: 'My Ruby App')
76
+ expect(subject['x-elastic-client-meta']).to match(regexp)
77
+ expect(subject).to include('x-elastic-client-meta' => meta_header)
78
+ end
79
+ end
80
+
81
+ context 'when using API Key' do
82
+ let(:client) do
83
+ described_class.new(api_key: 'an_api_key', adapter: adapter)
84
+ end
85
+
86
+ let(:authorization_header) do
87
+ client.transport.connections.first.connection.headers['Authorization']
88
+ end
89
+
90
+ it 'adds the ApiKey header to the connection' do
91
+ expect(authorization_header).to eq('ApiKey an_api_key')
92
+ expect(subject['x-elastic-client-meta']).to match(regexp)
93
+ expect(subject).to include('x-elastic-client-meta' => meta_header)
94
+ end
95
+ end
96
+
97
+ context 'adapters' do
98
+ let(:meta_header) do
99
+ if jruby?
100
+ "es=#{meta_version},rb=#{RUBY_VERSION},t=#{Elasticsearch::Transport::VERSION},jv=#{ENV_JAVA['java.version']},jr=#{JRUBY_VERSION},fd=#{Faraday::VERSION}"
101
+ else
102
+ "es=#{meta_version},rb=#{RUBY_VERSION},t=#{Elasticsearch::Transport::VERSION},fd=#{Faraday::VERSION}"
103
+ end
104
+ end
105
+ let(:client) { described_class.new(adapter: adapter) }
106
+ let(:headers) { client.transport.connections.first.connection.headers }
107
+
108
+ context 'using net/http/persistent' do
109
+ let(:adapter) { :net_http_persistent }
110
+
111
+ it 'sets adapter in the meta header version to 0 when not loaded' do
112
+ fork {
113
+ expect(headers['x-elastic-client-meta']).to match(regexp)
114
+ meta = "#{meta_header},np=0"
115
+ expect(headers).to include('x-elastic-client-meta' => meta)
116
+ }
117
+ end unless jruby?
118
+
119
+ it 'sets adapter in the meta header' do
120
+ require 'net/http/persistent'
121
+ expect(headers['x-elastic-client-meta']).to match(regexp)
122
+ meta = "#{meta_header},np=#{Net::HTTP::Persistent::VERSION}"
123
+ expect(headers).to include('x-elastic-client-meta' => meta)
124
+ end
125
+ end
126
+
127
+ context 'using httpclient' do
128
+ let(:adapter) { :httpclient }
129
+
130
+ it 'sets adapter in the meta header version to 0 when not loaded' do
131
+ fork {
132
+ expect(headers['x-elastic-client-meta']).to match(regexp)
133
+ meta = "#{meta_header},hc=0"
134
+ expect(headers).to include('x-elastic-client-meta' => meta)
135
+ }
136
+ end unless jruby?
137
+
138
+ it 'sets adapter in the meta header' do
139
+ require 'httpclient'
140
+ expect(headers['x-elastic-client-meta']).to match(regexp)
141
+ meta = "#{meta_header},hc=#{HTTPClient::VERSION}"
142
+ expect(headers).to include('x-elastic-client-meta' => meta)
143
+ end
144
+ end
145
+
146
+ context 'using typhoeus' do
147
+ let(:adapter) { :typhoeus }
148
+
149
+ it 'sets adapter in the meta header version to 0 when not loaded' do
150
+ fork {
151
+ expect(headers['x-elastic-client-meta']).to match(regexp)
152
+ meta = "#{meta_header},ty=0"
153
+ expect(headers).to include('x-elastic-client-meta' => meta)
154
+ }
155
+ end unless jruby?
156
+
157
+ it 'sets adapter in the meta header' do
158
+ require 'typhoeus'
159
+ expect(headers['x-elastic-client-meta']).to match(regexp)
160
+ meta = "#{meta_header},ty=#{Typhoeus::VERSION}"
161
+ expect(headers).to include('x-elastic-client-meta' => meta)
162
+ end
163
+ end
164
+
165
+ unless jruby?
166
+ let(:adapter) { :patron }
167
+
168
+ context 'using patron without requiring it' do
169
+ it 'sets adapter in the meta header version to 0 when not loaded' do
170
+ fork {
171
+ expect(headers['x-elastic-client-meta']).to match(regexp)
172
+ meta = "#{meta_header},pt=0"
173
+ expect(headers).to include('x-elastic-client-meta' => meta)
174
+ }
175
+ end
176
+ end
177
+
178
+ context 'using patron' do
179
+ it 'sets adapter in the meta header' do
180
+ require 'patron'
181
+ expect(headers['x-elastic-client-meta']).to match(regexp)
182
+ meta = "#{meta_header},pt=#{Patron::VERSION}"
183
+ expect(headers).to include('x-elastic-client-meta' => meta)
184
+ end
185
+ end
186
+ end
187
+
188
+ context 'using other' do
189
+ let(:adapter) { :some_other_adapter }
190
+
191
+ it 'sets adapter in the meta header without requiring' do
192
+ Faraday::Adapter.register_middleware some_other_adapter: Faraday::Adapter::NetHttpPersistent
193
+ expect(headers['x-elastic-client-meta']).to match(regexp)
194
+ expect(headers).to include('x-elastic-client-meta' => meta_header)
195
+ end
196
+
197
+ it 'sets adapter in the meta header' do
198
+ require 'net/http/persistent'
199
+ Faraday::Adapter.register_middleware some_other_adapter: Faraday::Adapter::NetHttpPersistent
200
+ expect(headers['x-elastic-client-meta']).to match(regexp)
201
+ expect(headers).to include('x-elastic-client-meta' => meta_header)
202
+ end
203
+ end
204
+ end
205
+
206
+ if defined?(JRUBY_VERSION)
207
+ context 'when using manticore' do
208
+ let(:client) do
209
+ Elasticsearch::Client.new(transport_class: Elasticsearch::Transport::Transport::HTTP::Manticore)
210
+ end
211
+ let(:subject) { client.transport.connections.first.connection.instance_variable_get("@options")[:headers]}
212
+
213
+ it 'sets manticore in the metaheader' do
214
+ expect(subject['x-elastic-client-meta']).to match(regexp)
215
+ expect(subject['x-elastic-client-meta']).to match(/mc=[0-9.]+/)
216
+ end
217
+ end
218
+ else
219
+ context 'when using curb' do
220
+ let(:client) do
221
+ Elasticsearch::Client.new(transport_class: Elasticsearch::Transport::Transport::HTTP::Curb)
222
+ end
223
+
224
+ it 'sets curb in the metaheader' do
225
+ expect(subject['x-elastic-client-meta']).to match(regexp)
226
+ expect(subject['x-elastic-client-meta']).to match(/cl=[0-9.]+/)
227
+ end
228
+ end
229
+ end
230
+
231
+ context 'when using custom transport implementation' do
232
+ class MyTransport
233
+ include Elasticsearch::Transport::Transport::Base
234
+ def initialize(args); end
235
+ end
236
+ let(:client) { Elasticsearch::Client.new(transport_class: MyTransport) }
237
+ let(:subject){ client.instance_variable_get("@arguments")[:transport_options][:headers] }
238
+ let(:meta_header) do
239
+ if jruby?
240
+ "es=#{meta_version},rb=#{RUBY_VERSION},t=#{Elasticsearch::Transport::VERSION},jv=#{ENV_JAVA['java.version']},jr=#{JRUBY_VERSION}"
241
+ else
242
+ "es=#{meta_version},rb=#{RUBY_VERSION},t=#{Elasticsearch::Transport::VERSION}"
243
+ end
244
+ end
245
+
246
+ it 'doesnae set any info about the implementation in the metaheader' do
247
+ expect(subject['x-elastic-client-meta']).to match(regexp)
248
+ expect(subject).to include('x-elastic-client-meta' => meta_header)
249
+ end
250
+ end
251
+
252
+ context 'when using a different service version' do
253
+ before do
254
+ stub_const("Elastic::META_HEADER_SERVICE_VERSION", [:ent, '8.0.0'])
255
+ end
256
+
257
+ let(:client) { Elasticsearch::Client.new }
258
+
259
+ it 'sets the service version in the metaheader' do
260
+ expect(subject['x-elastic-client-meta']).to match(regexp)
261
+ expect(subject['x-elastic-client-meta']).to start_with('ent=8.0.0')
262
+ end
263
+ end
264
+ end
265
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: elasticsearch-transport
3
3
  version: !ruby/object:Gem::Version
4
- version: 7.10.1
4
+ version: 7.12.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Karel Minarik
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-01-07 00:00:00.000000000 Z
11
+ date: 2021-03-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: multi_json
@@ -373,6 +373,7 @@ files:
373
373
  - lib/elasticsearch-transport.rb
374
374
  - lib/elasticsearch/transport.rb
375
375
  - lib/elasticsearch/transport/client.rb
376
+ - lib/elasticsearch/transport/meta_header.rb
376
377
  - lib/elasticsearch/transport/redacted.rb
377
378
  - lib/elasticsearch/transport/transport/base.rb
378
379
  - lib/elasticsearch/transport/transport/connections/collection.rb
@@ -391,6 +392,7 @@ files:
391
392
  - spec/elasticsearch/connections/selector_spec.rb
392
393
  - spec/elasticsearch/transport/base_spec.rb
393
394
  - spec/elasticsearch/transport/client_spec.rb
395
+ - spec/elasticsearch/transport/meta_header_spec.rb
394
396
  - spec/elasticsearch/transport/sniffer_spec.rb
395
397
  - spec/spec_helper.rb
396
398
  - test/integration/transport_test.rb
@@ -436,6 +438,7 @@ test_files:
436
438
  - spec/elasticsearch/connections/selector_spec.rb
437
439
  - spec/elasticsearch/transport/base_spec.rb
438
440
  - spec/elasticsearch/transport/client_spec.rb
441
+ - spec/elasticsearch/transport/meta_header_spec.rb
439
442
  - spec/elasticsearch/transport/sniffer_spec.rb
440
443
  - spec/spec_helper.rb
441
444
  - test/integration/transport_test.rb