-
Notifications
You must be signed in to change notification settings - Fork 214
/
post.t
57 lines (54 loc) · 1.51 KB
/
post.t
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
use strict;
use warnings;
use LWP::UserAgent;
use Plack::Runner;
use Test::More;
use Test::TCP;
test_tcp(
server => sub {
my $port = shift;
my $runner = Plack::Runner->new;
$runner->parse_options("--port" => $port, "-E", "dev", "-s", "HTTP::Server::PSGI");
$runner->run(
sub {
my $env = shift;
my $buf = '';
while (length($buf) != $env->{CONTENT_LENGTH}) {
my $rlen = $env->{'psgi.input'}->read(
$buf,
$env->{CONTENT_LENGTH} - length($buf),
length($buf),
);
last unless $rlen > 0;
}
return [
200,
[ 'Content-Type' => 'text/plain' ],
[ $buf ],
];
},
);
},
client => sub {
my $port = shift;
note 'send a broken request';
my $sock = IO::Socket::INET->new(
PeerAddr => "127.0.0.1:$port",
Proto => 'tcp',
) or die "failed to connect to server:$!";
$sock->print(<< "EOT");
POST / HTTP/1.0\r
Content-Length: 6\r
\r
EOT
undef $sock;
note 'send next request';
my $ua = LWP::UserAgent->new;
$ua->timeout(10);
my $res = $ua->post("http://127.0.0.1:$port/", { a => 1 });
ok $res->is_success;
is $res->code, 200;
is $res->content, 'a=1';
},
);
done_testing;