1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
|
# Licensed to Elasticsearch B.V. under one or more contributor
# license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright
# ownership. Elasticsearch B.V. licenses this file to you under
# the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
require 'spec_helper'
require 'elasticsearch'
describe Elasticsearch::Transport::Client do
context 'meta-header' do
let(:subject) { client.transport.connections.first.connection.headers }
let(:client) { described_class.new }
let(:regexp) { /^[a-z]{1,}=[a-z0-9.\-]{1,}(?:,[a-z]{1,}=[a-z0-9._\-]+)*$/ }
let(:adapter) { :net_http }
let(:adapter_code) { "nh=#{defined?(Net::HTTP::VERSION) ? Net::HTTP::VERSION : Net::HTTP::HTTPVersion}" }
let(:meta_header) do
if jruby?
"es=#{meta_version},rb=#{RUBY_VERSION},t=#{Elasticsearch::Transport::VERSION},jv=#{ENV_JAVA['java.version']},jr=#{JRUBY_VERSION},fd=#{Faraday::VERSION},#{adapter_code}"
else
"es=#{meta_version},rb=#{RUBY_VERSION},t=#{Elasticsearch::Transport::VERSION},fd=#{Faraday::VERSION},#{adapter_code}"
end
end
context 'client_meta_version' do
let(:version) { ['7.1.0-alpha', '7.11.0.pre.1', '8.0.0-beta', '8.0.0.beta.2']}
it 'converts the version to X.X.Xp' do
expect(client.send(:client_meta_version, '7.0.0-alpha')).to eq('7.0.0p')
expect(client.send(:client_meta_version, '7.11.0.pre.1')).to eq('7.11.0p')
expect(client.send(:client_meta_version, '8.1.0-beta')).to eq('8.1.0p')
expect(client.send(:client_meta_version, '8.0.0.beta.2')).to eq('8.0.0p')
expect(client.send(:client_meta_version, '12.16.4.pre')).to eq('12.16.4p')
end
end
# We are testing this method in the previous block, so now using it inside the test to make the
# Elasticsearch version in the meta header string dynamic
def meta_version
client.send(:client_meta_version, Elasticsearch::VERSION)
end
context 'single use of meta header' do
let(:client) do
described_class.new(adapter: adapter).tap do |klient|
allow(klient).to receive(:__build_connections)
end
end
it 'x-elastic-client-header value matches regexp' do
expect(subject['x-elastic-client-meta']).to match(regexp)
expect(subject).to include('x-elastic-client-meta' => meta_header)
end
end
context 'when using user-agent headers' do
let(:client) do
transport_options = { headers: { user_agent: 'My Ruby App' } }
described_class.new(transport_options: transport_options, adapter: adapter).tap do |klient|
allow(klient).to receive(:__build_connections)
end
end
it 'is friendly to previously set headers' do
expect(subject).to include(user_agent: 'My Ruby App')
expect(subject['x-elastic-client-meta']).to match(regexp)
expect(subject).to include('x-elastic-client-meta' => meta_header)
end
end
context 'when using API Key' do
let(:client) do
described_class.new(api_key: 'an_api_key', adapter: adapter)
end
let(:authorization_header) do
client.transport.connections.first.connection.headers['Authorization']
end
it 'adds the ApiKey header to the connection' do
expect(authorization_header).to eq('ApiKey an_api_key')
expect(subject['x-elastic-client-meta']).to match(regexp)
expect(subject).to include('x-elastic-client-meta' => meta_header)
end
end
context 'adapters' do
let(:meta_header) do
if jruby?
"es=#{meta_version},rb=#{RUBY_VERSION},t=#{Elasticsearch::Transport::VERSION},jv=#{ENV_JAVA['java.version']},jr=#{JRUBY_VERSION},fd=#{Faraday::VERSION}"
else
"es=#{meta_version},rb=#{RUBY_VERSION},t=#{Elasticsearch::Transport::VERSION},fd=#{Faraday::VERSION}"
end
end
let(:client) { described_class.new(adapter: adapter) }
let(:headers) { client.transport.connections.first.connection.headers }
context 'using net/http/persistent' do
let(:adapter) { :net_http_persistent }
it 'sets adapter in the meta header version to 0 when not loaded' do
was_required = defined?(Net::HTTP::Persistent)
if was_required
@klass = Net::HTTP::Persistent.clone
Net::HTTP.send(:remove_const, :Persistent)
end
expect(headers['x-elastic-client-meta']).to match(regexp)
meta = "#{meta_header},np=0"
expect(headers).to include('x-elastic-client-meta' => meta)
Net::HTTP::Persistent = @klass if was_required
end unless jruby?
it 'sets adapter in the meta header' do
require 'net/http/persistent'
expect(headers['x-elastic-client-meta']).to match(regexp)
meta = "#{meta_header},np=#{Net::HTTP::Persistent::VERSION}"
expect(headers).to include('x-elastic-client-meta' => meta)
end
end
context 'using httpclient' do
let(:adapter) { :httpclient }
it 'sets adapter in the meta header version to 0 when not loaded' do
was_required = defined?(HTTPClient)
if was_required
@klass = HTTPClient.clone
Object.send(:remove_const, :HTTPClient)
end
expect(headers['x-elastic-client-meta']).to match(regexp)
meta = "#{meta_header},hc=0"
expect(headers).to include('x-elastic-client-meta' => meta)
HTTPClient = @klass if was_required
end unless jruby?
it 'sets adapter in the meta header' do
require 'httpclient'
expect(headers['x-elastic-client-meta']).to match(regexp)
meta = "#{meta_header},hc=#{HTTPClient::VERSION}"
expect(headers).to include('x-elastic-client-meta' => meta)
end
end
context 'using typhoeus' do
let(:adapter) { :typhoeus }
it 'sets adapter in the meta header version to 0 when not loaded' do
was_required = defined?(Typhoeus)
if was_required
@klass = Typhoeus.clone
Object.send(:remove_const, :Typhoeus)
end
expect(headers['x-elastic-client-meta']).to match(regexp)
meta = "#{meta_header},ty=0"
expect(headers).to include('x-elastic-client-meta' => meta)
Typhoeus = @klass if was_required
end unless jruby?
it 'sets adapter in the meta header' do
require 'typhoeus'
expect(headers['x-elastic-client-meta']).to match(regexp)
meta = "#{meta_header},ty=#{Typhoeus::VERSION}"
expect(headers).to include('x-elastic-client-meta' => meta)
end
end
unless jruby?
let(:adapter) { :patron }
context 'using patron without requiring it' do
it 'sets adapter in the meta header version to 0 when not loaded' do
was_required = defined?(Patron)
if was_required
@klass = Patron.clone
Object.send(:remove_const, :Patron)
end
expect(headers['x-elastic-client-meta']).to match(regexp)
meta = "#{meta_header},pt=0"
expect(headers).to include('x-elastic-client-meta' => meta)
Patron = @klass if was_required
end
end
context 'using patron' do
it 'sets adapter in the meta header' do
require 'patron'
expect(headers['x-elastic-client-meta']).to match(regexp)
meta = "#{meta_header},pt=#{Patron::VERSION}"
expect(headers).to include('x-elastic-client-meta' => meta)
end
end
end
context 'using other' do
let(:adapter) { :some_other_adapter }
it 'sets adapter in the meta header without requiring' do
Faraday::Adapter.register_middleware some_other_adapter: Faraday::Adapter::NetHttpPersistent
expect(headers['x-elastic-client-meta']).to match(regexp)
expect(headers).to include('x-elastic-client-meta' => meta_header)
end
it 'sets adapter in the meta header' do
require 'net/http/persistent'
Faraday::Adapter.register_middleware some_other_adapter: Faraday::Adapter::NetHttpPersistent
expect(headers['x-elastic-client-meta']).to match(regexp)
expect(headers).to include('x-elastic-client-meta' => meta_header)
end
end
end
if defined?(JRUBY_VERSION)
context 'when using manticore' do
let(:client) do
described_class.new(transport_class: Elasticsearch::Transport::Transport::HTTP::Manticore).tap do |client|
client.instance_variable_set('@verified', true)
end
end
let(:subject) { client.transport.connections.first.connection.instance_variable_get("@options")[:headers]}
it 'sets manticore in the metaheader' do
expect(subject['x-elastic-client-meta']).to match(regexp)
expect(subject['x-elastic-client-meta']).to match(/mc=[0-9.]+/)
end
end
else
context 'when using curb' do
let(:client) do
described_class.new(transport_class: Elasticsearch::Transport::Transport::HTTP::Curb).tap do |client|
client.instance_variable_set('@verified', true)
end
end
it 'sets curb in the metaheader' do
expect(subject['x-elastic-client-meta']).to match(regexp)
expect(subject['x-elastic-client-meta']).to match(/cl=[0-9.]+/)
end
end
end
context 'when using custom transport implementation' do
let(:transport_class) do
Class.new do
def initialize(args)
end
end
end
let(:client) { Elasticsearch::Transport::Client.new(transport_class: transport_class) }
let(:subject) { client.instance_variable_get('@arguments')[:transport_options][:headers] }
let(:meta_header) do
if jruby?
"es=#{meta_version},rb=#{RUBY_VERSION},t=#{Elasticsearch::Transport::VERSION},jv=#{ENV_JAVA['java.version']},jr=#{JRUBY_VERSION}"
else
"es=#{meta_version},rb=#{RUBY_VERSION},t=#{Elasticsearch::Transport::VERSION}"
end
end
it 'doesnae set any info about the implementation in the metaheader' do
expect(subject['x-elastic-client-meta']).to match(regexp)
expect(subject).to include('x-elastic-client-meta' => meta_header)
end
end
context 'when using a different service version' do
before do
stub_const('Elastic::ELASTICSEARCH_SERVICE_VERSION', [:ent, '8.0.0'])
end
let(:client) do
described_class.new.tap do |client|
client.instance_variable_set('@verified', true)
end
end
it 'sets the service version in the metaheader' do
expect(subject['x-elastic-client-meta']).to match(regexp)
expect(subject['x-elastic-client-meta']).to start_with('ent=8.0.0')
end
end
end
end
|