spec/elasticsearch/transport/client_spec.rb in elasticsearch-transport-7.0.0 vs spec/elasticsearch/transport/client_spec.rb in elasticsearch-transport-7.1.0

- old
+ new

@@ -66,15 +66,30 @@ let(:client) do described_class.new(adapter: :typhoeus) end - it 'uses Faraday' do + it 'uses Faraday with the adapter' do expect(adapter).to include(Faraday::Adapter::Typhoeus) end end + context 'when the adapter is specified as a string key' do + + let(:adapter) do + client.transport.connections.all.first.connection.builder.handlers + end + + let(:client) do + described_class.new('adapter' => :typhoeus) + end + + it 'uses Faraday with the adapter' do + expect(adapter).to include(Faraday::Adapter::Typhoeus) + end + end + context 'when the adapter can be detected', unless: jruby? do around do |example| require 'patron'; load 'patron.rb' example.run @@ -124,10 +139,34 @@ expect(hosts[0][:host]).to eq('myhost') expect(hosts[0][:protocol]).to eq('http') expect(hosts[0][:port]).to be(9200) end + context 'when IPv6 format is used' do + + around do |example| + original_setting = Faraday.ignore_env_proxy + Faraday.ignore_env_proxy = true + example.run + Faraday.ignore_env_proxy = original_setting + end + + let(:host) do + 'https://[2090:db8:85a3:9811::1f]:8080' + end + + it 'extracts the host' do + expect(hosts[0][:host]).to eq('[2090:db8:85a3:9811::1f]') + expect(hosts[0][:scheme]).to eq('https') + expect(hosts[0][:port]).to be(8080) + end + + it 'creates the correct full url' do + expect(client.transport.__full_url(client.transport.hosts[0])).to eq('https://[2090:db8:85a3:9811::1f]:8080') + end + end + context 'when a path is specified' do let(:host) do 'https://myhost:8080/api' end @@ -205,10 +244,57 @@ expect(hosts[0][:host]).to eq('myhost') expect(hosts[0][:scheme]).to eq('https') expect(hosts[0][:port]).to be(9200) end + context 'when IPv6 format is used' do + + around do |example| + original_setting = Faraday.ignore_env_proxy + Faraday.ignore_env_proxy = true + example.run + Faraday.ignore_env_proxy = original_setting + end + + let(:host) do + { host: '[2090:db8:85a3:9811::1f]', scheme: 'https', port: '443' } + end + + it 'extracts the host' do + expect(hosts[0][:host]).to eq('[2090:db8:85a3:9811::1f]') + expect(hosts[0][:scheme]).to eq('https') + expect(hosts[0][:port]).to be(443) + end + + it 'creates the correct full url' do + expect(client.transport.__full_url(client.transport.hosts[0])).to eq('https://[2090:db8:85a3:9811::1f]:443') + end + end + + context 'when the host is localhost as a IPv6 address' do + + around do |example| + original_setting = Faraday.ignore_env_proxy + Faraday.ignore_env_proxy = true + example.run + Faraday.ignore_env_proxy = original_setting + end + + let(:host) do + { host: '[::1]' } + end + + it 'extracts the host' do + expect(hosts[0][:host]).to eq('[::1]') + expect(hosts[0][:port]).to be(9200) + end + + it 'creates the correct full url' do + expect(client.transport.__full_url(client.transport.hosts[0])).to eq('http://[::1]:9200') + end + end + context 'when the port is specified as a String' do let(:host) do { host: 'myhost', scheme: 'https', port: '443' } end @@ -286,10 +372,83 @@ expect(hosts[0][:protocol]).to eq('http') expect(hosts[0][:port]).to be(9200) end end + context 'when there is one host with a protocol and no port' do + + let(:host) do + ['http://myhost'] + end + + it 'extracts the host' do + expect(hosts[0][:host]).to eq('myhost') + expect(hosts[0][:protocol]).to eq('http') + expect(hosts[0][:port]).to be(9200) + end + end + + context 'when there is one host with a protocol and no port' do + + let(:host) do + ['http://myhost'] + end + + it 'extracts the host' do + expect(hosts[0][:host]).to eq('myhost') + expect(hosts[0][:protocol]).to eq('http') + expect(hosts[0][:port]).to be(9200) + end + end + + context 'when there is one host with a protocol and the default http port explicitly provided' do + let(:host) do + ['http://myhost:80'] + end + + it 'respects the explicit port' do + expect(hosts[0][:port]).to be(80) + end + end + + context 'when there is one host with a protocol and the default https port explicitly provided' do + let(:host) do + ['https://myhost:443'] + end + + it 'respects the explicit port' do + expect(hosts[0][:port]).to be(443) + end + end + + context 'when there is one host with a scheme, protocol and no port' do + + let(:host) do + ['https://myhost'] + end + + it 'extracts the host' do + expect(hosts[0][:host]).to eq('myhost') + expect(hosts[0][:protocol]).to eq('https') + expect(hosts[0][:port]).to be(9200) + end + end + + context 'when there is one host with a scheme, protocol, path, and no port' do + + let(:host) do + ['http://myhost/foo/bar'] + end + + it 'extracts the host' do + expect(hosts[0][:host]).to eq('myhost') + expect(hosts[0][:protocol]).to eq('http') + expect(hosts[0][:port]).to be(9200) + expect(hosts[0][:path]).to eq("/foo/bar") + end + end + context 'when there is more than one host' do let(:host) do ['host1', 'host2'] end @@ -351,11 +510,11 @@ end context 'when hosts are specified with the \'host\' key' do let(:client) do - described_class.new(hosts: ['host1', 'host2', 'host3', 'host4'], randomize_hosts: true) + described_class.new(host: ['host1', 'host2', 'host3', 'host4'], randomize_hosts: true) end let(:hosts) do client.transport.hosts end @@ -363,62 +522,103 @@ it 'sets the hosts in random order' do expect(hosts.all? { |host| client.transport.hosts.include?(host) }).to be(true) end end - context 'when hosts are specified with the \'host\' key' do + context 'when hosts are specified with the \'host\' key as a String' do let(:client) do - described_class.new(host: host) + described_class.new('host' => ['host1', 'host2', 'host3', 'host4'], 'randomize_hosts' => true) end let(:hosts) do client.transport.hosts end - it_behaves_like 'a client that extracts hosts' + it 'sets the hosts in random order' do + expect(hosts.all? { |host| client.transport.hosts.include?(host) }).to be(true) + end end context 'when hosts are specified with the \'hosts\' key' do let(:client) do - described_class.new(host: host) + described_class.new(hosts: host) end let(:hosts) do client.transport.hosts end it_behaves_like 'a client that extracts hosts' end + context 'when hosts are specified with the \'hosts\' key as a String' do + + let(:client) do + described_class.new('hosts' => host) + end + + let(:hosts) do + client.transport.hosts + end + + it_behaves_like 'a client that extracts hosts' + end + context 'when hosts are specified with the \'url\' key' do let(:client) do - described_class.new(host: host) + described_class.new(url: host) end let(:hosts) do client.transport.hosts end it_behaves_like 'a client that extracts hosts' end + context 'when hosts are specified with the \'url\' key as a String' do + + let(:client) do + described_class.new('url' => host) + end + + let(:hosts) do + client.transport.hosts + end + + it_behaves_like 'a client that extracts hosts' + end + context 'when hosts are specified with the \'urls\' key' do let(:client) do - described_class.new(host: host) + described_class.new(urls: host) end let(:hosts) do client.transport.hosts end it_behaves_like 'a client that extracts hosts' end + context 'when hosts are specified with the \'urls\' key as a String' do + + let(:client) do + described_class.new('urls' => host) + end + + let(:hosts) do + client.transport.hosts + end + + it_behaves_like 'a client that extracts hosts' + end + context 'when the URL is set in the ELASTICSEARCH_URL environment variable' do context 'when there is only one host specified' do around do |example| @@ -462,10 +662,21 @@ it 'sets the scheme' do expect(client.transport.connections[0].full_url('')).to match(/https/) end end + context 'when scheme is specified as a String key' do + + let(:client) do + described_class.new('scheme' => 'https') + end + + it 'sets the scheme' do + expect(client.transport.connections[0].full_url('')).to match(/https/) + end + end + context 'when user and password are specified' do let(:client) do described_class.new(user: 'USERNAME', password: 'PASSWORD') end @@ -488,10 +699,36 @@ expect(client.transport.connections[0].full_url('')).to match(/foobar/) end end end + context 'when user and password are specified as String keys' do + + let(:client) do + described_class.new('user' => 'USERNAME', 'password' => 'PASSWORD') + end + + it 'sets the user and password' do + expect(client.transport.connections[0].full_url('')).to match(/USERNAME/) + expect(client.transport.connections[0].full_url('')).to match(/PASSWORD/) + end + + context 'when the connections are reloaded' do + + before do + allow(client.transport.sniffer).to receive(:hosts).and_return([{ host: 'foobar', port: 4567, id: 'foobar4567' }]) + client.transport.reload_connections! + end + + it 'sets keeps user and password' do + expect(client.transport.connections[0].full_url('')).to match(/USERNAME/) + expect(client.transport.connections[0].full_url('')).to match(/PASSWORD/) + expect(client.transport.connections[0].full_url('')).to match(/foobar/) + end + end + end + context 'when port is specified' do let(:client) do described_class.new(host: 'node1', port: 1234) end @@ -572,10 +809,21 @@ it 'sets the options on the transport' do expect(client.transport.options[:transport_options][:request]).to eq(timeout: 120) end end + + context 'when \'request_timeout\' is defined as a String key' do + + let(:client) do + described_class.new('request_timeout' => 120) + end + + it 'sets the options on the transport' do + expect(client.transport.options[:transport_options][:request]).to eq(timeout: 120) + end + end end describe '#perform_request' do let(:transport_instance) do @@ -924,6 +1172,6 @@ expect(connections_after).to be >= (connections_before) end end end end -end \ No newline at end of file +end