Skip to content

Commit 3935f86

Browse files
haargoalders
authored andcommitted
test using local address
Try to resolve localhost and use it for the daemon. Fall back to 127.0.0.1, or just not providing a local address.
1 parent e66151b commit 3935f86

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

t/lib/TestServer.pm

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,31 @@ sub import {
111111
sub run {
112112
my $self = shift;
113113

114+
my $listen_host;
115+
116+
require Socket;
117+
require IO::Socket::IP;
118+
my ($err, @res) = Socket::getaddrinfo("localhost", "http", {
119+
protocol => Socket::IPPROTO_TCP(),
120+
} );
121+
122+
my @local_hosts = map +(Socket::getnameinfo($_->{addr}, Socket::NI_NUMERICHOST()))[1], @res;
123+
push @local_hosts, '127.0.0.1';
124+
125+
for my $host (@local_hosts) {
126+
my $try = IO::Socket::IP->new(LocalAddr => $host, Listen => 1);
127+
if ($try) {
128+
$listen_host = $host;
129+
$try->close;
130+
last;
131+
}
132+
}
133+
114134
require HTTP::Daemon;
115-
my $d = HTTP::Daemon->new(Timeout => 10);
135+
my $d = HTTP::Daemon->new(
136+
Timeout => 10,
137+
$listen_host ? ( LocalHost => $listen_host ) : (),
138+
);
116139

117140
print "HTTP::Daemon running at <URL:", $d->url, ">\n";
118141
open STDOUT, '>', File::Spec->devnull;

0 commit comments

Comments
 (0)