|
| 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 |
0 commit comments