fixed a few spaces
[catagits/Web-Simple.git] / t / post.t
1 use strict;
2 use warnings FATAL => 'all';
3
4 use Test::More qw(no_plan);
5
6 {
7   use Web::Simple 'PostTest';
8   package PostTest;
9   sub dispatch_request {
10     sub (%:foo=&:bar~) {
11       $_[1]->{bar} ||= 'EMPTY';
12       [ 200,
13         [ "Content-type" => "text/plain" ],
14         [ join(' ',@{$_[1]}{qw(foo bar)}) ]
15       ]
16     },
17     sub (*baz=) {
18       [ 200,
19         [ "Content-type" => "text/plain" ],
20         [ $_[1]->reason || $_[1]->filename ],
21       ]
22     },
23   }
24 }
25
26 use Plack::Test;
27 use HTTP::Request::Common qw(GET POST);
28
29 my $app = PostTest->new;
30
31 sub run_request {
32   my $request = shift;
33   return test_psgi $app->to_psgi_app, sub { shift->($request) };
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');
61
62 my $upload = run_request(
63   POST 'http://localhost'
64     => Content_Type => 'form-data'
65     => Content => [
66       foo => 'FOO',
67       bar => 'BAR'
68     ]
69 );
70
71 cmp_ok($upload->code, '==', 200, '200 with multipart');
72
73 is($upload->content, 'FOO BAR', 'both params returned');
74
75 my $upload_wrongtype = run_request(
76   POST 'http://localhost'
77     => [ baz => 'fleem' ]
78 );
79
80 is(
81   $upload_wrongtype->content,
82   'field baz exists with value fleem but body was not multipart/form-data',
83   'error points out wrong body type'
84 );
85
86 my $upload_notupload = run_request(
87   POST 'http://localhost'
88     => Content_Type => 'form-data'
89     => Content => [ baz => 'fleem' ]
90 );
91
92 is(
93   $upload_notupload->content,
94   'field baz exists with value fleem but was not an upload',
95   'error points out field was not an upload'
96 );
97
98 my $upload_isupload = run_request(
99   POST 'http://localhost'
100     => Content_Type => 'form-data'
101     => Content => [
102       baz => [
103         undef, 'TESTFILE',
104         Content => 'test content', 'Content-Type' => 'text/plain'
105       ],
106     ]
107 );
108
109 is(
110   $upload_isupload->content,
111   'TESTFILE',
112   'Actual upload returns filename ok'
113 );