1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
#!/usr/bin/env perl
use strict;
use warnings;
use FindBin;
use Hijk;
use Test::More;
use Test::Exception;
my $port = 10000 + int rand(5000);
my $pid = fork;
die "Fail to fork then start a plack server" unless defined $pid;
if ($pid == 0) {
require Plack::Runner;
my $runner = Plack::Runner->new;
$runner->parse_options("--port", $port, "$FindBin::Bin/bin/it-takes-time.psgi");
$runner->run;
exit;
}
sleep 10; # hopfully this is enough to launch that psgi.
my %args = (
host => "localhost",
port => $port,
query_string => "t=5",
method => "GET",
);
subtest "expect connection failure (mismatching port number)" => sub {
dies_ok {
my $port_wrong = $port;
while ($port_wrong == $port) {
$port_wrong = int( 15001+rand()*3000 );
}
diag "Connecting to a wrong port: $port";
my $res = Hijk::request({%args, port => $port_wrong, timeout => 10});
} 'We connect to wrong port so, as expected, the connection cannot be established.';
diag "Dying message: $@";
};
subtest "expect read timeout" => sub {
lives_ok {
my $res = Hijk::request({%args, timeout => 1});
ok exists $res->{error}, '$res->{error} should exist becasue a read timeout is expected.';
is $res->{error}, Hijk::Error::READ_TIMEOUT, '$res->{error} == Hijk::Error::READ_TIMEOUT';
};
};
subtest "do not expect timeout" => sub {
lives_ok {
my $res = Hijk::request({%args, timeout => 10});
} 'local plack send back something within 10s';
};
END { kill INT => $pid if $pid }
done_testing;
|