spec/elasticsearch/transport/client_spec.rb in elasticsearch-transport-7.5.0 vs spec/elasticsearch/transport/client_spec.rb in elasticsearch-transport-7.6.0.pre

- old
+ new

@@ -47,20 +47,62 @@ it 'uses localhost by default' do expect(client.transport.hosts[0][:host]).to eq('localhost') end context 'when a User-Agent header is specified as client option' do - let(:client) do described_class.new(transport_options: { headers: { 'User-Agent' => 'testing' } }) end it 'sets the specified User-Agent header' do expect(client.transport.connections.first.connection.headers['User-Agent']).to eq('testing') end end + context 'when an encoded api_key is provided' do + let(:client) do + described_class.new(api_key: 'an_api_key') + 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') + end + end + + context 'when an un-encoded api_key is provided' do + let(:client) do + described_class.new(api_key: { id: 'my_id', api_key: 'my_api_key' }) + 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 #{Base64.strict_encode64('my_id:my_api_key')}") + end + end + + context 'when basic auth and api_key are provided' do + let(:client) do + described_class.new( + api_key: { id: 'my_id', api_key: 'my_api_key' }, + host: 'http://elastic:password@localhost:9200' + ) + end + let(:authorization_header) do + client.transport.connections.first.connection.headers['Authorization'] + end + + it 'removes basic auth credentials' do + expect(authorization_header).not_to match(/^Basic/) + expect(authorization_header).to match(/^ApiKey/) + end + end + context 'when a user-agent header is specified as client option in lower-case' do let(:client) do described_class.new(transport_options: { headers: { 'user-agent' => 'testing' } }) end @@ -174,49 +216,47 @@ end end end describe 'adapter' do - context 'when no adapter is specified' do - let(:adapter) do - client.transport.connections.all.first.connection.builder.handlers + client.transport.connections.all.first.connection.builder.adapter end it 'uses Faraday NetHttp' do - expect(adapter).to include(Faraday::Adapter::NetHttp) + expect(adapter).to eq Faraday::Adapter::NetHttp end end context 'when the adapter is specified' do let(:adapter) do - client.transport.connections.all.first.connection.builder.handlers + client.transport.connections.all.first.connection.builder.adapter end let(:client) do - described_class.new(adapter: :typhoeus) + described_class.new(adapter: :patron) end it 'uses Faraday with the adapter' do - expect(adapter).to include(Faraday::Adapter::Typhoeus) + expect(adapter).to eq Faraday::Adapter::Patron end end context 'when the adapter is specified as a string key' do let(:adapter) do - client.transport.connections.all.first.connection.builder.handlers + client.transport.connections.all.first.connection.builder.adapter end let(:client) do - described_class.new('adapter' => :typhoeus) + described_class.new('adapter' => :patron) end it 'uses Faraday with the adapter' do - expect(adapter).to include(Faraday::Adapter::Typhoeus) + expect(adapter).to eq Faraday::Adapter::Patron end end context 'when the adapter can be detected', unless: jruby? do @@ -224,33 +264,37 @@ require 'patron'; load 'patron.rb' example.run end let(:adapter) do - client.transport.connections.all.first.connection.builder.handlers + client.transport.connections.all.first.connection.builder.adapter end it 'uses the detected adapter' do - expect(adapter).to include(Faraday::Adapter::Patron) + expect(adapter).to eq Faraday::Adapter::Patron end end context 'when the Faraday adapter is configured' do let(:client) do described_class.new do |faraday| - faraday.adapter :typhoeus + faraday.adapter :patron faraday.response :logger end end + let(:adapter) do + client.transport.connections.all.first.connection.builder.adapter + end + let(:handlers) do client.transport.connections.all.first.connection.builder.handlers end it 'sets the adapter' do - expect(handlers).to include(Faraday::Adapter::Typhoeus) + expect(adapter).to eq Faraday::Adapter::Patron end it 'sets the logger' do expect(handlers).to include(Faraday::Response::Logger) end @@ -1053,14 +1097,47 @@ it 'sets the option' do expect(request).to be(true) end end + + context 'when x-opaque-id is set' do + let(:client) { described_class.new(host: hosts) } + + it 'uses x-opaque-id on a request' do + expect(client.perform_request('GET', '/', { opaque_id: '12345' }).headers['x-opaque-id']).to eq('12345') + end + end + + context 'when an x-opaque-id prefix is set on initialization' do + let(:prefix) { 'elastic_cloud' } + let(:client) do + described_class.new(host: hosts, opaque_id_prefix: prefix) + end + + it 'uses x-opaque-id on a request' do + expect(client.perform_request('GET', '/', { opaque_id: '12345' }).headers['x-opaque-id']).to eq("#{prefix}12345") + end + + context 'when using an API call' do + let(:client) { described_class.new(host: hosts) } + + it 'doesnae raise an ArgumentError' do + expect { client.search(opaque_id: 'no_error') }.not_to raise_error + end + + it 'uses X-Opaque-Id in the header' do + allow(client).to receive(:perform_request) { OpenStruct.new(body: '') } + expect { client.search(opaque_id: 'opaque_id') }.not_to raise_error + expect(client).to have_received(:perform_request) + .with('GET', '_search', { opaque_id: 'opaque_id' }, nil) + end + end + end end context 'when the client connects to Elasticsearch' do - let(:logger) do Logger.new(STDERR).tap do |logger| logger.formatter = proc do |severity, datetime, progname, msg| color = case severity when /INFO/ then :green @@ -1134,27 +1211,26 @@ expect(response.body).to match(/---\n/) expect(response.headers['content-type']).to eq('application/yaml') end context 'when the Faraday adapter is set in the block' do - let(:client) do Elasticsearch::Client.new(host: ELASTICSEARCH_HOSTS.first, logger: logger) do |client| client.adapter(:net_http_persistent) end end - let(:connection_handler) do - client.transport.connections.first.connection.builder.handlers.first + let(:handler_name) do + client.transport.connections.first.connection.builder.adapter.name end let(:response) do client.perform_request('GET', '_cluster/health') end it 'sets the adapter' do - expect(connection_handler.name).to eq('Faraday::Adapter::NetHttpPersistent') + expect(handler_name).to eq('Faraday::Adapter::NetHttpPersistent') end it 'uses the adapter to connect' do expect(response.status).to eq(200) end @@ -1200,11 +1276,11 @@ it 'retries only the specified number of times' do expect(client.perform_request('GET', '_nodes/_local')) expect { client.perform_request('GET', '_nodes/_local') - }.to raise_exception(Faraday::Error::ConnectionFailed) + }.to raise_exception(Faraday::ConnectionFailed) end end context 'when reload_on_failure is true' do @@ -1484,15 +1560,15 @@ let(:options) do { adapter: :patron } end - let(:connection_handler) do - client.transport.connections.first.connection.builder.handlers.first + let(:adapter) do + client.transport.connections.first.connection.builder.adapter end it 'uses the patron connection handler' do - expect(connection_handler).to eq('Faraday::Adapter::Patron') + expect(adapter).to eq('Faraday::Adapter::Patron') end it 'keeps connections open' do response = client.perform_request('GET', '_nodes/stats/http') connections_before = response.body['nodes'].values.find { |n| n['name'] == node_names.first }['http']['total_opened']