Skip to content

Commit e1c9fa8

Browse files
committed
Take into account the localhost exception in address specs
1 parent 7d7fc3e commit e1c9fa8

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

spec/mongo/address_spec.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,13 +214,22 @@
214214
address.host
215215
end
216216

217+
let(:addr_info) do
218+
family = (host == 'localhost') ? ::Socket::AF_INET : ::Socket::AF_UNSPEC
219+
::Socket.getaddrinfo(host, nil, family, ::Socket::SOCK_STREAM)
220+
end
221+
222+
let(:socket_address_or_host) do
223+
(host == 'localhost') ? addr_info.first[3] : host
224+
end
225+
217226
context 'when providing a DNS entry that resolves to both IPv6 and IPv4' do
218227

219228
before do
220229
address.instance_variable_set(:@resolver, nil)
221230
allow(::Socket).to receive(:getaddrinfo).and_return(
222231
[ ["AF_INET6", 0, '::1', '::1', ::Socket::AF_INET6, 1, 6],
223-
["AF_INET", 0, host, host, ::Socket::AF_INET, 1, 6]]
232+
["AF_INET", 0, socket_address_or_host, socket_address_or_host, ::Socket::AF_INET, 1, 6]]
224233
)
225234
end
226235

@@ -237,7 +246,7 @@
237246
end
238247

239248
it 'uses the host, not the IP address' do
240-
expect(address.socket(0.0).host).to eq(host)
249+
expect(address.socket(0.0).host).to eq(socket_address_or_host)
241250
end
242251

243252
let(:socket) do

0 commit comments

Comments
 (0)