elasticsearch-transport 7.9.0.pre → 7.11.0.pre.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,203 @@
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(:regexp) { /^[a-z]{1,}=[a-z0-9.\-]{1,}(?:,[a-z]{1,}=[a-z0-9._\-]+)*$/ }
24
+ let(:adapter) { :net_http }
25
+ let(:adapter_code) { "nh=#{defined?(Net::HTTP::VERSION) ? Net::HTTP::VERSION : Net::HTTP::HTTPVersion}" }
26
+ let(:meta_header) do
27
+ if jruby?
28
+ "es=#{Elasticsearch::VERSION},rb=#{RUBY_VERSION},t=#{Elasticsearch::Transport::VERSION},jv=#{ENV_JAVA['java.version']},jr=#{JRUBY_VERSION},fd=#{Faraday::VERSION},#{adapter_code}"
29
+ else
30
+ "es=#{Elasticsearch::VERSION},rb=#{RUBY_VERSION},t=#{Elasticsearch::Transport::VERSION},fd=#{Faraday::VERSION},#{adapter_code}"
31
+ end
32
+ end
33
+
34
+ context 'single use of meta header' do
35
+ let(:client) do
36
+ described_class.new(adapter: adapter).tap do |klient|
37
+ allow(klient).to receive(:__build_connections)
38
+ end
39
+ end
40
+
41
+ it 'x-elastic-client-header value matches regexp' do
42
+ expect(subject['x-elastic-client-meta']).to match(regexp)
43
+ expect(subject).to include('x-elastic-client-meta' => meta_header)
44
+ end
45
+ end
46
+
47
+ context 'when using user-agent headers' do
48
+ let(:client) do
49
+ transport_options = { headers: { user_agent: 'My Ruby App' } }
50
+ described_class.new(transport_options: transport_options, adapter: adapter).tap do |klient|
51
+ allow(klient).to receive(:__build_connections)
52
+ end
53
+ end
54
+
55
+ it 'is friendly to previously set headers' do
56
+ expect(subject).to include(user_agent: 'My Ruby App')
57
+ expect(subject['x-elastic-client-meta']).to match(regexp)
58
+ expect(subject).to include('x-elastic-client-meta' => meta_header)
59
+ end
60
+ end
61
+
62
+ context 'when using API Key' do
63
+ let(:client) do
64
+ described_class.new(api_key: 'an_api_key', adapter: adapter)
65
+ end
66
+
67
+ let(:authorization_header) do
68
+ client.transport.connections.first.connection.headers['Authorization']
69
+ end
70
+
71
+ it 'adds the ApiKey header to the connection' do
72
+ expect(authorization_header).to eq('ApiKey an_api_key')
73
+ expect(subject['x-elastic-client-meta']).to match(regexp)
74
+ expect(subject).to include('x-elastic-client-meta' => meta_header)
75
+ end
76
+ end
77
+
78
+ context 'adapters' do
79
+ let(:meta_header) do
80
+ if jruby?
81
+ "es=#{Elasticsearch::VERSION},rb=#{RUBY_VERSION},t=#{Elasticsearch::Transport::VERSION},jv=#{ENV_JAVA['java.version']},jr=#{JRUBY_VERSION},fd=#{Faraday::VERSION}"
82
+ else
83
+ "es=#{Elasticsearch::VERSION},rb=#{RUBY_VERSION},t=#{Elasticsearch::Transport::VERSION},fd=#{Faraday::VERSION}"
84
+ end
85
+ end
86
+ let(:client) { described_class.new(adapter: adapter) }
87
+ let(:headers) { client.transport.connections.first.connection.headers }
88
+
89
+ context 'using net/http/persistent' do
90
+ let(:adapter) { :net_http_persistent }
91
+
92
+ it 'sets adapter in the meta header' do
93
+ require 'net/http/persistent'
94
+ expect(headers['x-elastic-client-meta']).to match(regexp)
95
+ meta = "#{meta_header},np=#{Net::HTTP::Persistent::VERSION}"
96
+ expect(headers).to include('x-elastic-client-meta' => meta)
97
+ end
98
+ end
99
+
100
+ context 'using httpclient' do
101
+ let(:adapter) { :httpclient }
102
+
103
+ it 'sets adapter in the meta header' do
104
+ require 'httpclient'
105
+ expect(headers['x-elastic-client-meta']).to match(regexp)
106
+ meta = "#{meta_header},hc=#{HTTPClient::VERSION}"
107
+ expect(headers).to include('x-elastic-client-meta' => meta)
108
+ end
109
+ end
110
+
111
+ context 'using typhoeus' do
112
+ let(:adapter) { :typhoeus }
113
+
114
+ it 'sets adapter in the meta header' do
115
+ require 'typhoeus'
116
+ expect(headers['x-elastic-client-meta']).to match(regexp)
117
+ meta = "#{meta_header},ty=#{Typhoeus::VERSION}"
118
+ expect(headers).to include('x-elastic-client-meta' => meta)
119
+ end
120
+ end
121
+
122
+ unless defined?(JRUBY_VERSION)
123
+ let(:adapter) { :patron }
124
+
125
+ context 'using patron' do
126
+ it 'sets adapter in the meta header' do
127
+ require 'patron'
128
+ expect(headers['x-elastic-client-meta']).to match(regexp)
129
+ meta = "#{meta_header},pt=#{Patron::VERSION}"
130
+ expect(headers).to include('x-elastic-client-meta' => meta)
131
+ end
132
+ end
133
+ end
134
+ end
135
+
136
+ if defined?(JRUBY_VERSION)
137
+ context 'when using manticore' do
138
+ let(:client) do
139
+ Elasticsearch::Client.new(transport_class: Elasticsearch::Transport::Transport::HTTP::Manticore)
140
+ end
141
+ let(:subject) { client.transport.connections.first.connection.instance_variable_get("@options")[:headers]}
142
+
143
+ it 'sets manticore in the metaheader' do
144
+ expect(subject['x-elastic-client-meta']).to match(regexp)
145
+ expect(subject['x-elastic-client-meta']).to match(/mc=[0-9.]+/)
146
+ end
147
+ end
148
+ else
149
+ context 'when using curb' do
150
+ let(:client) do
151
+ Elasticsearch::Client.new(transport_class: Elasticsearch::Transport::Transport::HTTP::Curb)
152
+ end
153
+
154
+ it 'sets curb in the metaheader' do
155
+ expect(subject['x-elastic-client-meta']).to match(regexp)
156
+ expect(subject['x-elastic-client-meta']).to match(/cl=[0-9.]+/)
157
+ end
158
+ end
159
+ end
160
+
161
+ context 'when using custom transport implementation' do
162
+ class MyTransport
163
+ include Elasticsearch::Transport::Transport::Base
164
+ def initialize(args); end
165
+ end
166
+ let(:client) { Elasticsearch::Client.new(transport_class: MyTransport) }
167
+ let(:subject){ client.instance_variable_get("@arguments")[:transport_options][:headers] }
168
+ let(:meta_header) do
169
+ if jruby?
170
+ "es=#{Elasticsearch::VERSION},rb=#{RUBY_VERSION},t=#{Elasticsearch::Transport::VERSION},jv=#{ENV_JAVA['java.version']},jr=#{JRUBY_VERSION}"
171
+ else
172
+ "es=#{Elasticsearch::VERSION},rb=#{RUBY_VERSION},t=#{Elasticsearch::Transport::VERSION}"
173
+ end
174
+ end
175
+
176
+ it 'doesnae set any info about the implementation in the metaheader' do
177
+ expect(subject['x-elastic-client-meta']).to match(regexp)
178
+ expect(subject).to include('x-elastic-client-meta' => meta_header)
179
+ end
180
+ end
181
+
182
+ context 'when using a different service version' do
183
+ before do
184
+ module Elastic
185
+ META_HEADER_SERVICE_VERSION = [:ent, '8.0.0']
186
+ end
187
+ end
188
+
189
+ after do
190
+ module Elastic
191
+ META_HEADER_SERVICE_VERSION = [:es, Elasticsearch::VERSION]
192
+ end
193
+ end
194
+
195
+ let(:client) { Elasticsearch::Client.new }
196
+
197
+ it 'sets the service version in the metaheader' do
198
+ expect(subject['x-elastic-client-meta']).to match(regexp)
199
+ expect(subject['x-elastic-client-meta']).to start_with('ent=8.0.0')
200
+ end
201
+ end
202
+ end
203
+ end
@@ -57,6 +57,11 @@ class Elasticsearch::Transport::Transport::Connections::ConnectionTest < Minites
57
57
  assert_equal 'http://localhost:9200/foo/_search?foo=bar', c.full_url('_search', {:foo => 'bar'})
58
58
  end
59
59
 
60
+ should "return right full url with path when path starts with /" do
61
+ c = Connection.new :host => { :protocol => 'http', :host => 'localhost', :port => '9200', :path => '/foo' }
62
+ assert_equal 'http://localhost:9200/foo/_search?foo=bar', c.full_url('/_search', {:foo => 'bar'})
63
+ end
64
+
60
65
  should "have a string representation" do
61
66
  c = Connection.new :host => 'x'
62
67
  assert_match /host: x/, c.to_s
@@ -56,7 +56,7 @@ else
56
56
 
57
57
  should "set body for GET request" do
58
58
  @transport.connections.first.connection.expects(:get).
59
- with('http://127.0.0.1:8080//', {:body => '{"foo":"bar"}',
59
+ with('http://127.0.0.1:8080/', {:body => '{"foo":"bar"}',
60
60
  :headers => {"Content-Type" => "application/json",
61
61
  "User-Agent" => @transport.send(:user_agent_header)}}).returns(stub_everything)
62
62
  @transport.perform_request 'GET', '/', {}, '{"foo":"bar"}'
@@ -64,7 +64,7 @@ else
64
64
 
65
65
  should "set body for PUT request" do
66
66
  @transport.connections.first.connection.expects(:put).
67
- with('http://127.0.0.1:8080//', {:body => '{"foo":"bar"}',
67
+ with('http://127.0.0.1:8080/', {:body => '{"foo":"bar"}',
68
68
  :headers => {"Content-Type" => "application/json",
69
69
  "User-Agent" => @transport.send(:user_agent_header)}}).returns(stub_everything)
70
70
  @transport.perform_request 'PUT', '/', {}, {:foo => 'bar'}
@@ -72,7 +72,7 @@ else
72
72
 
73
73
  should "serialize the request body" do
74
74
  @transport.connections.first.connection.expects(:post).
75
- with('http://127.0.0.1:8080//', {:body => '{"foo":"bar"}',
75
+ with('http://127.0.0.1:8080/', {:body => '{"foo":"bar"}',
76
76
  :headers => {"Content-Type" => "application/json",
77
77
  "User-Agent" => @transport.send(:user_agent_header)}}).returns(stub_everything)
78
78
  @transport.perform_request 'POST', '/', {}, {'foo' => 'bar'}
@@ -80,7 +80,7 @@ else
80
80
 
81
81
  should "set custom headers for PUT request" do
82
82
  @transport.connections.first.connection.expects(:put).
83
- with('http://127.0.0.1:8080//', {:body => '{"foo":"bar"}',
83
+ with('http://127.0.0.1:8080/', {:body => '{"foo":"bar"}',
84
84
  :headers => {"Content-Type" => "application/json",
85
85
  "User-Agent" => @transport.send(:user_agent_header)}})
86
86
  .returns(stub_everything)
@@ -89,7 +89,7 @@ else
89
89
 
90
90
  should "not serialize a String request body" do
91
91
  @transport.connections.first.connection.expects(:post).
92
- with('http://127.0.0.1:8080//', {:body => '{"foo":"bar"}',
92
+ with('http://127.0.0.1:8080/', {:body => '{"foo":"bar"}',
93
93
  :headers => {"Content-Type" => "application/json",
94
94
  "User-Agent" => @transport.send(:user_agent_header)}}).returns(stub_everything)
95
95
  @transport.serializer.expects(:dump).never
@@ -103,7 +103,7 @@ else
103
103
 
104
104
  transport = Manticore.new :hosts => [ { :host => 'localhost', :port => 8080 } ], :options => options
105
105
 
106
- transport.connections.first.connection.stub("http://localhost:8080//", :body => "\"\"", :headers => {"Content-Type" => "application/x-ndjson",
106
+ transport.connections.first.connection.stub("http://localhost:8080/", :body => "\"\"", :headers => {"Content-Type" => "application/x-ndjson",
107
107
  "User-Agent" => @transport.send(:user_agent_header)}, :code => 200 )
108
108
 
109
109
  response = transport.perform_request 'GET', '/', {}
@@ -124,15 +124,15 @@ else
124
124
  end
125
125
 
126
126
  should "handle HTTP methods" do
127
- @transport.connections.first.connection.expects(:delete).with('http://127.0.0.1:8080//', { headers: {"Content-Type" => "application/json",
127
+ @transport.connections.first.connection.expects(:delete).with('http://127.0.0.1:8080/', { headers: {"Content-Type" => "application/json",
128
128
  "User-Agent" => @transport.send(:user_agent_header)}}).returns(stub_everything)
129
- @transport.connections.first.connection.expects(:head).with('http://127.0.0.1:8080//', { headers: {"Content-Type" => "application/json",
129
+ @transport.connections.first.connection.expects(:head).with('http://127.0.0.1:8080/', { headers: {"Content-Type" => "application/json",
130
130
  "User-Agent" => @transport.send(:user_agent_header)}}).returns(stub_everything)
131
- @transport.connections.first.connection.expects(:get).with('http://127.0.0.1:8080//', { headers: {"Content-Type" => "application/json",
131
+ @transport.connections.first.connection.expects(:get).with('http://127.0.0.1:8080/', { headers: {"Content-Type" => "application/json",
132
132
  "User-Agent" => @transport.send(:user_agent_header)}}).returns(stub_everything)
133
- @transport.connections.first.connection.expects(:put).with('http://127.0.0.1:8080//', { headers: {"Content-Type" => "application/json",
133
+ @transport.connections.first.connection.expects(:put).with('http://127.0.0.1:8080/', { headers: {"Content-Type" => "application/json",
134
134
  "User-Agent" => @transport.send(:user_agent_header)}}).returns(stub_everything)
135
- @transport.connections.first.connection.expects(:post).with('http://127.0.0.1:8080//', { headers: {"Content-Type" => "application/json",
135
+ @transport.connections.first.connection.expects(:post).with('http://127.0.0.1:8080/', { headers: {"Content-Type" => "application/json",
136
136
  "User-Agent" => @transport.send(:user_agent_header)}}).returns(stub_everything)
137
137
 
138
138
  %w| HEAD GET PUT POST DELETE |.each { |method| @transport.perform_request method, '/' }
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.9.0.pre
4
+ version: 7.11.0.pre.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Karel Minarik
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-07-17 00:00:00.000000000 Z
11
+ date: 2021-01-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: multi_json
@@ -355,7 +355,7 @@ dependencies:
355
355
  description: 'Ruby client for Elasticsearch. See the `elasticsearch` gem for full
356
356
  integration.
357
357
 
358
- '
358
+ '
359
359
  email:
360
360
  - karel.minarik@elasticsearch.org
361
361
  executables: []
@@ -391,6 +391,7 @@ files:
391
391
  - spec/elasticsearch/connections/selector_spec.rb
392
392
  - spec/elasticsearch/transport/base_spec.rb
393
393
  - spec/elasticsearch/transport/client_spec.rb
394
+ - spec/elasticsearch/transport/meta_header_spec.rb
394
395
  - spec/elasticsearch/transport/sniffer_spec.rb
395
396
  - spec/spec_helper.rb
396
397
  - test/integration/transport_test.rb
@@ -436,6 +437,7 @@ test_files:
436
437
  - spec/elasticsearch/connections/selector_spec.rb
437
438
  - spec/elasticsearch/transport/base_spec.rb
438
439
  - spec/elasticsearch/transport/client_spec.rb
440
+ - spec/elasticsearch/transport/meta_header_spec.rb
439
441
  - spec/elasticsearch/transport/sniffer_spec.rb
440
442
  - spec/spec_helper.rb
441
443
  - test/integration/transport_test.rb