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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
|
#!/usr/bin/perl
# vim: ts=2 sw=2 filetype=perl expandtab
use warnings;
use strict;
BEGIN {
unless (-f 'run_network_tests') {
print "1..0 # skip - Network access (and permission) required to run this test\n";
exit;
}
eval "use HTTP::Request";
if ($@) {
print "1..0 # skip - HTTP::Request needed to test POE::Filter::HTTPD\n";
exit;
}
}
use Test::More tests => 3;
my $port;
use POE qw(
Component::Client::TCP
Component::Server::TCP
Filter::HTTPD
);
#
# handler
#
POE::Component::Server::TCP->new(
Alias => 's0',
Address => '127.0.0.1',
Port => 0,
ClientFilter => 'POE::Filter::HTTPD',
Started => sub {
use Socket qw(sockaddr_in);
$port = (
sockaddr_in($_[HEAP]->{listener}->getsockname())
)[0];
},
ClientInput => sub {
my ( $kernel, $heap, $request ) = @_[ KERNEL, HEAP, ARG0 ];
isa_ok( $request, 'HTTP::Message', $request);
ok( $request->uri() eq '/foo/bar', 'Double striped' );
POE::Kernel->yield('shutdown');
},
);
POE::Component::Client::TCP->new (
Alias => 'c0',
RemoteAddress => '127.0.0.1',
RemotePort => $port,
ServerInput => sub { fail("client c0 got input from server: $_[ARG0]"); },
# Silence errors.
ServerError => sub { undef },
);
POE::Component::Client::TCP->new (
Alias => 'c1',
RemoteAddress => '127.0.0.1',
RemotePort => $port,
Connected => sub {
ok 1, 'client connected';
$_[HEAP]->{server}->put( "GET //foo/bar 1.0\015\012\015\012");
},
Disconnected => sub {
# Shutdown step 2: Kill the server and all remaining connections
note "client c1 disconnected";
POE::Kernel->signal( s0 => 'KILL' );
},
ServerInput => sub { fail("client c1 got input from server: $_[ARG0]"); },
# Silence errors.
ServerError => sub { undef },
);
$poe_kernel->run();
exit 0;
|