Skip to content

Commit 178a343

Browse files
miyucyestolfo
authored andcommitted
Use URI#default_port instead of Regxp
1 parent c0dd048 commit 178a343

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ def __parse_host(host)
197197
uri = URI.parse(host)
198198

199199
# Handle when port is not specified
200-
if host =~ /^#{uri.scheme}:\/\/#{uri.host}$/
200+
if uri.port == uri.default_port
201201
port = nil
202202
else
203203
port = uri.port

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

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,46 @@
301301
end
302302
end
303303

304+
context 'when there is one host with a protocol and no port' do
305+
306+
let(:host) do
307+
['http://myhost']
308+
end
309+
310+
it 'extracts the host' do
311+
expect(hosts[0][:host]).to eq('myhost')
312+
expect(hosts[0][:protocol]).to eq('http')
313+
expect(hosts[0][:port]).to be(9200)
314+
end
315+
end
316+
317+
context 'when there is one host with a scheme, protocol and no port' do
318+
319+
let(:host) do
320+
['https://myhost']
321+
end
322+
323+
it 'extracts the host' do
324+
expect(hosts[0][:host]).to eq('myhost')
325+
expect(hosts[0][:protocol]).to eq('https')
326+
expect(hosts[0][:port]).to be(9200)
327+
end
328+
end
329+
330+
context 'when there is one host with a scheme, protocol and no port' do
331+
332+
let(:host) do
333+
['http://myhost/foo/bar']
334+
end
335+
336+
it 'extracts the host' do
337+
expect(hosts[0][:host]).to eq('myhost')
338+
expect(hosts[0][:protocol]).to eq('http')
339+
expect(hosts[0][:port]).to be(9200)
340+
expect(hosts[0][:path]).to eq("/foo/bar")
341+
end
342+
end
343+
304344
context 'when there is more than one host' do
305345

306346
let(:host) do
@@ -939,4 +979,4 @@
939979
end
940980
end
941981
end
942-
end
982+
end

0 commit comments

Comments
 (0)