amend post.t to work with new body parsing rules
[catagits/Web-Simple.git] / t / post.t
CommitLineData
53d47b78 1use strict;
2use warnings FATAL => 'all';
3
4use Test::More (
5 eval { require HTTP::Request::AsCGI }
6 ? 'no_plan'
7 : (skip_all => 'No HTTP::Request::AsCGI')
8);
9
10{
11 use Web::Simple 'PostTest';
12 package PostTest;
92e23550 13 dispatch {
5705ec1e 14 sub (%:foo=&:bar~) {
53d47b78 15 $_[1]->{bar} ||= 'EMPTY';
16 [ 200,
17 [ "Content-type" => "text/plain" ],
18 [ join(' ',@{$_[1]}{qw(foo bar)}) ]
19 ]
20 },
92e23550 21 }
53d47b78 22}
23
24use HTTP::Request::Common qw(GET POST);
25
26my $app = PostTest->new;
27
28sub run_request {
29 my $request = shift;
30 my $c = HTTP::Request::AsCGI->new($request)->setup;
31 $app->run;
32 $c->restore;
33 return $c->response;
34}
35
36my $get = run_request(GET 'http://localhost/');
37
38cmp_ok($get->code, '==', 404, '404 on GET');
39
40my $no_body = run_request(POST 'http://localhost/');
41
42cmp_ok($no_body->code, '==', 404, '404 with empty body');
43
44my $no_foo = run_request(POST 'http://localhost/' => [ bar => 'BAR' ]);
45
46cmp_ok($no_foo->code, '==', 404, '404 with no foo param');
47
48my $no_bar = run_request(POST 'http://localhost/' => [ foo => 'FOO' ]);
49
50cmp_ok($no_bar->code, '==', 200, '200 with only foo param');
51
52is($no_bar->content, 'FOO EMPTY', 'bar defaulted');
53
54my $both = run_request(
55 POST 'http://localhost/' => [ foo => 'FOO', bar => 'BAR' ]
56);
57
58cmp_ok($both->code, '==', 200, '200 with both params');
59
60is($both->content, 'FOO BAR', 'both params returned');