diff --git a/elasticsearch-transport/lib/elasticsearch/transport/client.rb b/elasticsearch-transport/lib/elasticsearch/transport/client.rb index 0f820d837a..99bff1a76f 100644 --- a/elasticsearch-transport/lib/elasticsearch/transport/client.rb +++ b/elasticsearch-transport/lib/elasticsearch/transport/client.rb @@ -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] diff --git a/elasticsearch-transport/test/unit/client_test.rb b/elasticsearch-transport/test/unit/client_test.rb index c25e7ffea2..74797a20f3 100644 --- a/elasticsearch-transport/test/unit/client_test.rb +++ b/elasticsearch-transport/test/unit/client_test.rb @@ -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 @@ -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 @@ -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 @@ -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 @@ -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