amend post.t to work with new body parsing rules
[catagits/Web-Simple.git] / t / post.t
1 use strict;
2 use warnings FATAL => 'all';
3
4 use 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;
13   dispatch {
14     sub (%:foo=&:bar~) {
15       $_[1]->{bar} ||= 'EMPTY';
16       [ 200,
17         [ "Content-type" => "text/plain" ],
18         [ join(' ',@{$_[1]}{qw(foo bar)}) ]
19       ]
20     },
21   }
22 }
23
24 use HTTP::Request::Common qw(GET POST);
25
26 my $app = PostTest->new;
27
28 sub 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
36 my $get = run_request(GET 'http://localhost/');
37
38 cmp_ok($get->code, '==', 404, '404 on GET');
39
40 my $no_body = run_request(POST 'http://localhost/');
41
42 cmp_ok($no_body->code, '==', 404, '404 with empty body');
43
44 my $no_foo = run_request(POST 'http://localhost/' => [ bar => 'BAR' ]);
45
46 cmp_ok($no_foo->code, '==', 404, '404 with no foo param');
47
48 my $no_bar = run_request(POST 'http://localhost/' => [ foo => 'FOO' ]);
49
50 cmp_ok($no_bar->code, '==', 200, '200 with only foo param');
51
52 is($no_bar->content, 'FOO EMPTY', 'bar defaulted');
53
54 my $both = run_request(
55   POST 'http://localhost/' => [ foo => 'FOO', bar => 'BAR' ]
56 );
57
58 cmp_ok($both->code, '==', 200, '200 with both params');
59
60 is($both->content, 'FOO BAR', 'both params returned');