Juno
view release on metacpan or search on metacpan
t/check/http.t view on Meta::CPAN
use warnings;
use Test::More;
use AnyEvent;
use Juno::Check::HTTP;
{
local $@ = undef;
eval 'use Test::TCP';
$@ and plan skip_all => 'Test::TCP is required for this test';
}
{
local $@ = undef;
eval 'use AnyEvent::HTTPD';
$@ and plan skip_all => 'AnyEvent::HTTPD is required for this test';
}
plan tests => 29;
my $requests = 0;
my $goodbody = '<html><head><body>OK</body></head></html>';
my $badbody = 'Fail';
my $port = Test::TCP::empty_port();
my $httpd = AnyEvent::HTTPD->new( port => $port );
$httpd->reg_cb (
'/' => sub {
my ( $httpd, $req ) = @_;
# if we're on the 3rd request, let's fail it
# this is after two successful ones that call:
# before, result, success
if ( ++$requests >= 3 ) {
$req->respond(
[ 400, 'failed', { 'Content-Type' => 'text/html' }, $badbody ]
);
# stop here
return;
}
$req->respond( {
content => [
'text/html',
$goodbody,
],
} );
},
);
my %match = ();
sub result_check {
my ( $type, $check, $host, $got_body, $headers ) = @_;
my $exp_body = $goodbody;
my %results = (
'cache-control' => 'max-age=0',
'connection' => 'Keep-Alive',
'content-type' => 'text/html',
'HTTPVersion' => '1.0',
);
if ( $type eq 'fail' ) {
$exp_body = $badbody;
%results = (
%results,
'content-length' => 4,
'Reason' => 'failed',
'Status' => 400,
'URL' => "http://$host/",
),
} else {
%results = (
%results,
'content-length' => 41,
'Reason' => 'ok',
'Status' => 200,
'URL' => "http://$host/",
),
}
isa_ok( $check, 'Juno::Check::HTTP' );
is( $got_body, $exp_body, 'Got body' );
# possibly remove dates from headers because it can't be static
delete $headers->{'date'};
delete $headers->{'expires'};
is_deeply( $headers, \%results, 'Got correct headers' );
};
my $cv = AnyEvent->condvar;
my $check = Juno::Check::HTTP->new(
interval => 0.1,
hosts => [ "localhost:$port", "127.0.0.1:$port" ],
headers => { 'Num' => 30, 'String' => 'hello' },
# called twice for each host = x4
# for good or bad
on_before => sub {
my ( $checker, $host ) = @_;
isa_ok( $checker, 'Juno::Check::HTTP' );
$match{'before'}{$host}++;
$cv->end;
},
# called once for each host = x2
on_success => sub {
my ( $checker, $host ) = @_;
$match{'success'}{$host}++;
result_check( 'success', @_ );
$cv->end;
},
# called twice for each host = x4
# for good or bad
on_result => sub {
my ( $checker, $host ) = @_;
$match{'result'}{$host}++;
( run in 1.387 second using v1.01-cache-2.11-cpan-d7f47b0818f )