Skip to content

Commit

Permalink
Fix name resolution when no port is given and add test for empty and …
Browse files Browse the repository at this point in the history
…NULL port
  • Loading branch information
larskanis committed Nov 15, 2022
1 parent 68d40c5 commit dbff785
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/pg/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,7 @@ def new(*args)
# This requires PostgreSQL-10+, so no DNS resolving is done on earlier versions.
ihosts = iopts[:host].split(",", -1)
iports = iopts[:port].split(",", -1)
iports = [nil] if iports.size == 0
iports = iports * ihosts.size if iports.size == 1
raise PG::ConnectionBad, "could not match #{iports.size} port numbers to #{ihosts.size} hosts" if iports.size != ihosts.size

Expand Down
25 changes: 25 additions & 0 deletions spec/pg/connection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,31 @@
expect( @conn.options ).to eq( "" )
end

it "connects without port and then retrieves the default port" do
gate = Helpers::TcpGateSwitcher.new(
external_host: 'localhost',
external_port: ENV['PGPORT'].to_i,
internal_host: "127.0.0.1",
internal_port: 5432,
debug: ENV['PG_DEBUG']=='1')

PG.connect(host: "localhost",
port: "",
dbname: "test") do |conn|
expect( conn.port ).to eq( 5432 )
end

PG.connect(hostaddr: "127.0.0.1",
port: nil,
dbname: "test") do |conn|
expect( conn.port ).to eq( 5432 )
end

gate.finish
rescue Errno::EADDRINUSE => err
skip err.to_s
end

it "can retrieve hostaddr for the established connection", :postgresql_12 do
expect( @conn.hostaddr ).to match( /^127\.0\.0\.1$|^::1$/ )
end
Expand Down

0 comments on commit dbff785

Please sign in to comment.