Skip to content

Commit 49bd535

Browse files
committed
[CLIENT] Support options specified with String keys
1 parent 1fdcb66 commit 49bd535

File tree

2 files changed

+114
-10
lines changed

2 files changed

+114
-10
lines changed

elasticsearch-transport/lib/elasticsearch/transport/client.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ class Client
105105
# @yield [faraday] Access and configure the `Faraday::Connection` instance directly with a block
106106
#
107107
def initialize(arguments={}, &block)
108-
@options = arguments
109-
@arguments = arguments
108+
@options = arguments.inject({}){|args,(k,v)| args[k.to_sym] = v; args}
109+
@arguments = @options
110110
@arguments[:logger] ||= @arguments[:log] ? DEFAULT_LOGGER.call() : nil
111111
@arguments[:tracer] ||= @arguments[:trace] ? DEFAULT_TRACER.call() : nil
112112
@arguments[:reload_connections] ||= false

elasticsearch-transport/spec/elasticsearch/transport/client_spec.rb

Lines changed: 112 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,22 @@
6868
described_class.new(adapter: :typhoeus)
6969
end
7070

71-
it 'uses Faraday' do
71+
it 'uses Faraday with the adapter' do
72+
expect(adapter).to include(Faraday::Adapter::Typhoeus)
73+
end
74+
end
75+
76+
context 'when the adapter is specified as a string key' do
77+
78+
let(:adapter) do
79+
client.transport.connections.all.first.connection.builder.handlers
80+
end
81+
82+
let(:client) do
83+
described_class.new('adapter' => :typhoeus)
84+
end
85+
86+
it 'uses Faraday with the adapter' do
7287
expect(adapter).to include(Faraday::Adapter::Typhoeus)
7388
end
7489
end
@@ -426,7 +441,7 @@
426441
context 'when hosts are specified with the \'host\' key' do
427442

428443
let(:client) do
429-
described_class.new(hosts: ['host1', 'host2', 'host3', 'host4'], randomize_hosts: true)
444+
described_class.new(host: ['host1', 'host2', 'host3', 'host4'], randomize_hosts: true)
430445
end
431446

432447
let(:hosts) do
@@ -438,23 +453,38 @@
438453
end
439454
end
440455

441-
context 'when hosts are specified with the \'host\' key' do
456+
context 'when hosts are specified with the \'host\' key as a String' do
442457

443458
let(:client) do
444-
described_class.new(host: host)
459+
described_class.new('host' => ['host1', 'host2', 'host3', 'host4'], 'randomize_hosts' => true)
445460
end
446461

447462
let(:hosts) do
448463
client.transport.hosts
449464
end
450465

451-
it_behaves_like 'a client that extracts hosts'
466+
it 'sets the hosts in random order' do
467+
expect(hosts.all? { |host| client.transport.hosts.include?(host) }).to be(true)
468+
end
452469
end
453470

454471
context 'when hosts are specified with the \'hosts\' key' do
455472

456473
let(:client) do
457-
described_class.new(host: host)
474+
described_class.new(hosts: host)
475+
end
476+
477+
let(:hosts) do
478+
client.transport.hosts
479+
end
480+
481+
it_behaves_like 'a client that extracts hosts'
482+
end
483+
484+
context 'when hosts are specified with the \'hosts\' key as a String' do
485+
486+
let(:client) do
487+
described_class.new('hosts' => host)
458488
end
459489

460490
let(:hosts) do
@@ -467,7 +497,20 @@
467497
context 'when hosts are specified with the \'url\' key' do
468498

469499
let(:client) do
470-
described_class.new(host: host)
500+
described_class.new(url: host)
501+
end
502+
503+
let(:hosts) do
504+
client.transport.hosts
505+
end
506+
507+
it_behaves_like 'a client that extracts hosts'
508+
end
509+
510+
context 'when hosts are specified with the \'url\' key as a String' do
511+
512+
let(:client) do
513+
described_class.new('url' => host)
471514
end
472515

473516
let(:hosts) do
@@ -480,7 +523,20 @@
480523
context 'when hosts are specified with the \'urls\' key' do
481524

482525
let(:client) do
483-
described_class.new(host: host)
526+
described_class.new(urls: host)
527+
end
528+
529+
let(:hosts) do
530+
client.transport.hosts
531+
end
532+
533+
it_behaves_like 'a client that extracts hosts'
534+
end
535+
536+
context 'when hosts are specified with the \'urls\' key as a String' do
537+
538+
let(:client) do
539+
described_class.new('urls' => host)
484540
end
485541

486542
let(:hosts) do
@@ -537,6 +593,17 @@
537593
end
538594
end
539595

596+
context 'when scheme is specified as a String key' do
597+
598+
let(:client) do
599+
described_class.new('scheme' => 'https')
600+
end
601+
602+
it 'sets the scheme' do
603+
expect(client.transport.connections[0].full_url('')).to match(/https/)
604+
end
605+
end
606+
540607
context 'when user and password are specified' do
541608

542609
let(:client) do
@@ -563,6 +630,32 @@
563630
end
564631
end
565632

633+
context 'when user and password are specified as String keys' do
634+
635+
let(:client) do
636+
described_class.new('user' => 'USERNAME', 'password' => 'PASSWORD')
637+
end
638+
639+
it 'sets the user and password' do
640+
expect(client.transport.connections[0].full_url('')).to match(/USERNAME/)
641+
expect(client.transport.connections[0].full_url('')).to match(/PASSWORD/)
642+
end
643+
644+
context 'when the connections are reloaded' do
645+
646+
before do
647+
allow(client.transport.sniffer).to receive(:hosts).and_return([{ host: 'foobar', port: 4567, id: 'foobar4567' }])
648+
client.transport.reload_connections!
649+
end
650+
651+
it 'sets keeps user and password' do
652+
expect(client.transport.connections[0].full_url('')).to match(/USERNAME/)
653+
expect(client.transport.connections[0].full_url('')).to match(/PASSWORD/)
654+
expect(client.transport.connections[0].full_url('')).to match(/foobar/)
655+
end
656+
end
657+
end
658+
566659
context 'when port is specified' do
567660

568661
let(:client) do
@@ -647,6 +740,17 @@
647740
expect(client.transport.options[:transport_options][:request]).to eq(timeout: 120)
648741
end
649742
end
743+
744+
context 'when \'request_timeout\' is defined as a String key' do
745+
746+
let(:client) do
747+
described_class.new('request_timeout' => 120)
748+
end
749+
750+
it 'sets the options on the transport' do
751+
expect(client.transport.options[:transport_options][:request]).to eq(timeout: 120)
752+
end
753+
end
650754
end
651755

652756
describe '#perform_request' do

0 commit comments

Comments
 (0)