|
19 | 19 |
|
20 | 20 | describe Elasticsearch::Transport::Client do
|
21 | 21 | context 'meta-header' do
|
22 |
| - let(:client) do |
23 |
| - described_class.new.tap do |klient| |
24 |
| - allow(klient).to receive(:__build_connections) |
25 |
| - end |
26 |
| - end |
27 | 22 | let(:subject) { client.transport.connections.first.connection.headers }
|
28 | 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}" } |
29 | 26 | let(:meta_header) do
|
30 | 27 | if RUBY_ENGINE == 'jruby'
|
31 |
| - "es=#{Elasticsearch::VERSION},rb=#{RUBY_VERSION},t=#{Elasticsearch::Transport::VERSION},jr=#{JRUBY_VERSION}" |
| 28 | + "es=#{Elasticsearch::VERSION},rb=#{RUBY_VERSION},t=#{Elasticsearch::Transport::VERSION},jr=#{JRUBY_VERSION},fd=#{Faraday::VERSION},#{adapter_code}" |
32 | 29 | else
|
33 |
| - "es=#{Elasticsearch::VERSION},rb=#{RUBY_VERSION},t=#{Elasticsearch::Transport::VERSION}" |
| 30 | + "es=#{Elasticsearch::VERSION},rb=#{RUBY_VERSION},t=#{Elasticsearch::Transport::VERSION},fd=#{Faraday::VERSION},#{adapter_code}" |
34 | 31 | end
|
35 | 32 | end
|
36 | 33 |
|
37 | 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 | + |
38 | 41 | it 'x-elastic-client-header value matches regexp' do
|
39 | 42 | expect(subject['x-elastic-client-meta']).to match(regexp)
|
40 | 43 | expect(subject).to include('x-elastic-client-meta' => meta_header)
|
|
44 | 47 | context 'when using user-agent headers' do
|
45 | 48 | let(:client) do
|
46 | 49 | transport_options = { headers: { user_agent: 'My Ruby App' } }
|
47 |
| - described_class.new(transport_options: transport_options).tap do |klient| |
| 50 | + described_class.new(transport_options: transport_options, adapter: adapter).tap do |klient| |
48 | 51 | allow(klient).to receive(:__build_connections)
|
49 | 52 | end
|
50 | 53 | end
|
51 | 54 |
|
52 | 55 | it 'is friendly to previously set headers' do
|
53 | 56 | expect(subject).to include(user_agent: 'My Ruby App')
|
| 57 | + expect(subject['x-elastic-client-meta']).to match(regexp) |
54 | 58 | expect(subject).to include('x-elastic-client-meta' => meta_header)
|
55 | 59 | end
|
56 | 60 | end
|
57 | 61 |
|
58 | 62 | context 'when using API Key' do
|
59 | 63 | let(:client) do
|
60 |
| - described_class.new(api_key: 'an_api_key') |
| 64 | + described_class.new(api_key: 'an_api_key', adapter: adapter) |
61 | 65 | end
|
62 | 66 |
|
63 | 67 | let(:authorization_header) do
|
64 | 68 | client.transport.connections.first.connection.headers['Authorization']
|
65 | 69 | end
|
66 | 70 |
|
67 |
| - it 'Adds the ApiKey header to the connection' do |
| 71 | + it 'adds the ApiKey header to the connection' do |
68 | 72 | expect(authorization_header).to eq('ApiKey an_api_key')
|
69 | 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 RUBY_ENGINE == 'jruby' |
| 81 | + "es=#{Elasticsearch::VERSION},rb=#{RUBY_VERSION},t=#{Elasticsearch::Transport::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 RUBY_ENGINE == 'jruby' |
| 170 | + "es=#{Elasticsearch::VERSION},rb=#{RUBY_VERSION},t=#{Elasticsearch::Transport::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) |
70 | 179 | end
|
71 | 180 | end
|
72 | 181 | end
|
|
0 commit comments