forked from miyagawa/Twiggy
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request miyagawa#22 from athomason/845cedab
Add test for pull request 20
- Loading branch information
Showing
1 changed file
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); |