Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 17 additions & 14 deletions elasticsearch-transport/lib/elasticsearch/transport/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,22 +143,25 @@ def __extract_hosts(hosts_config, options={})
end

result = hosts.map do |host|
case host
when String
if host =~ /^[a-z]+\:\/\//
uri = URI.parse(host)
{ :scheme => uri.scheme, :user => uri.user, :password => uri.password, :host => uri.host, :path => uri.path, :port => uri.port.to_s }
host_parts = case host
when String
if host =~ /^[a-z]+\:\/\//
uri = URI.parse(host)
{ :scheme => uri.scheme, :user => uri.user, :password => uri.password, :host => uri.host, :path => uri.path, :port => uri.port }
else
host, port = host.split(':')
{ :host => host, :port => port }
end
when URI
{ :scheme => host.scheme, :user => host.user, :password => host.password, :host => host.host, :path => host.path, :port => host.port }
when Hash
host
else
host, port = host.split(':')
{ :host => host, :port => port }
raise ArgumentError, "Please pass host as a String, URI or Hash -- #{host.class} given."
end
when URI
{ :scheme => host.scheme, :user => host.user, :password => host.password, :host => host.host, :path => host.path, :port => host.port.to_s }
when Hash
host
else
raise ArgumentError, "Please pass host as a String, URI or Hash -- #{host.class} given."
end

host_parts[:port] = host_parts[:port].to_i unless host_parts[:port].nil?
host_parts
end

result.shuffle! if options[:randomize_hosts]
Expand Down
23 changes: 17 additions & 6 deletions elasticsearch-transport/test/unit/client_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,17 @@ def initialize(*); end
hosts = @client.__extract_hosts( { :host => 'myhost', :scheme => 'https' } )
assert_equal 'myhost', hosts[0][:host]
assert_equal 'https', hosts[0][:scheme]
assert_nil hosts[0][:port]
end

should "extract from hash with a port passed as a string" do
hosts = @client.__extract_hosts( { :host => 'myhost', :scheme => 'https', :port => '443' } )
assert_equal 443, hosts[0][:port]
end

should "extract from hash with a port passed as an integer" do
hosts = @client.__extract_hosts( { :host => 'myhost', :scheme => 'https', :port => 443 } )
assert_equal 443, hosts[0][:port]
end

should "extract from Hashie::Mash" do
Expand Down Expand Up @@ -154,10 +165,10 @@ def initialize(*); end
assert_equal 2, hosts.size

assert_equal 'host1', hosts[0][:host]
assert_equal '1000', hosts[0][:port]
assert_equal 1000, hosts[0][:port]

assert_equal 'host2', hosts[1][:host]
assert_equal '2000', hosts[1][:port]
assert_equal 2000, hosts[1][:port]
end

should "extract path" do
Expand All @@ -171,7 +182,7 @@ def initialize(*); end

assert_equal 'https', hosts[0][:scheme]
assert_equal 'myhost', hosts[0][:host]
assert_equal '8080', hosts[0][:port]
assert_equal 8080, hosts[0][:port]
end

should "extract credentials" do
Expand All @@ -181,14 +192,14 @@ def initialize(*); end
assert_equal 'USERNAME', hosts[0][:user]
assert_equal 'PASSWORD', hosts[0][:password]
assert_equal 'myhost', hosts[0][:host]
assert_equal '8080', hosts[0][:port]
assert_equal 8080, hosts[0][:port]
end

should "pass hashes over" do
hosts = @client.__extract_hosts [{:host => 'myhost', :port => '1000', :foo => 'bar'}]

assert_equal 'myhost', hosts[0][:host]
assert_equal '1000', hosts[0][:port]
assert_equal 1000, hosts[0][:port]
assert_equal 'bar', hosts[0][:foo]
end

Expand All @@ -200,7 +211,7 @@ def initialize(*); end
assert_equal 'USERNAME', hosts[0][:user]
assert_equal 'PASSWORD', hosts[0][:password]
assert_equal 'myhost', hosts[0][:host]
assert_equal '4430', hosts[0][:port]
assert_equal 4430, hosts[0][:port]
end

should "split comma-separated URLs" do
Expand Down