Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add a test for client disconnection
  • Loading branch information
athomason committed Sep 18, 2011
1 parent 505bb86 commit 845ceda
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions t/disconnect.t
@@ -0,0 +1,65 @@
use strict;
use warnings;
use Test::More qw(no_diag);
use Test::TCP;
use IO::Socket::INET;
use Plack::Loader;
use Plack::Request;
use HTTP::Response;

my $app = sub {
my $env = shift;
my $req = Plack::Request->new($env);
my $params = $req->parameters;

return [
200,
[ 'Content-Type' => 'text/plain', ],
[ $params->{q} ],
];
};

# test that client disconnection doesn't trigger 400 response

test_tcp(
client => sub {
my $port = shift;

# empty request
my $sock = IO::Socket::INET->new(
Proto => 'tcp',
PeerAddr => '127.0.0.1',
PeerPort => $port,
) or die "Cannot open client socket: $!";
$sock->shutdown(1);

my $data = join('', <$sock>);
$sock->close;

is($data, '', 'got empty response to empty request')
or note explain $data;

# incomplete headers
$sock = IO::Socket::INET->new(
Proto => 'tcp',
PeerAddr => '127.0.0.1',
PeerPort => $port,
) or die "Cannot open client socket: $!";

print $sock "GET / HTTP/1.0"; # no CRLF
$sock->shutdown(1);

$data = join('', <$sock>);
$sock->close;

is($data, '', 'got empty response to incomplete header request')
or note explain $data;
},
server => sub {
my $port = shift;
my $server = Plack::Loader->load('Twiggy', port => $port, host => '127.0.0.1');
$server->run($app);
},
);

done_testing();

0 comments on commit 845ceda

Please sign in to comment.