Skip to content

Commit

Permalink
use URI::Generic to avoid edge-cases with ports
Browse files Browse the repository at this point in the history
  • Loading branch information
yaauie authored and estolfo committed May 7, 2019
1 parent 0f8ad9d commit 9212136
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
9 changes: 4 additions & 5 deletions lib/elasticsearch/transport/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -194,17 +194,16 @@ def __parse_host(host)
host_parts = case host
when String
if host =~ /^[a-z]+\:\/\//
uri = URI.parse(host)

# Handle when port is not specified
port = uri.port unless uri.port == uri.default_port
# Construct a new `URI::Generic` directly from the array returned by URI::split.
# This avoids `URI::HTTP` and `URI::HTTPS`, which supply default ports.
uri = URI::Generic.new(*URI.split(host))

{ :scheme => uri.scheme,
:user => uri.user,
:password => uri.password,
:host => uri.host,
:path => uri.path,
:port => port }
:port => uri.port }
else
host, port = host.split(':')
{ :host => host,
Expand Down
20 changes: 20 additions & 0 deletions spec/elasticsearch/transport/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,26 @@
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
Expand Down

0 comments on commit 9212136

Please sign in to comment.