Skip to content

Commit d9337a9

Browse files
committed
[CLIENT] Use default port when host and protocol are specified but no port
1 parent f1b54f9 commit d9337a9

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,12 +195,20 @@ def __parse_host(host)
195195
when String
196196
if host =~ /^[a-z]+\:\/\//
197197
uri = URI.parse(host)
198+
199+
# Handle when port is not specified
200+
if host =~ /^#{uri.scheme}:\/\/#{uri.host}$/
201+
port = nil
202+
else
203+
port = uri.port
204+
end
205+
198206
{ :scheme => uri.scheme,
199207
:user => uri.user,
200208
:password => uri.password,
201209
:host => uri.host,
202210
:path => uri.path,
203-
:port => uri.port }
211+
:port => port }
204212
else
205213
host, port = host.split(':')
206214
{ :host => host,

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,19 @@
288288
end
289289
end
290290

291+
context 'when there is one host with a protocol and no port' do
292+
293+
let(:host) do
294+
['http://myhost']
295+
end
296+
297+
it 'extracts the host' do
298+
expect(hosts[0][:host]).to eq('myhost')
299+
expect(hosts[0][:protocol]).to eq('http')
300+
expect(hosts[0][:port]).to be(9200)
301+
end
302+
end
303+
291304
context 'when there is more than one host' do
292305

293306
let(:host) do

0 commit comments

Comments
 (0)