test POST splat
[catagits/Web-Simple.git] / t / post.t
CommitLineData
53d47b78 1use strict;
2use warnings FATAL => 'all';
3
05aafc1a 4use Test::More qw(no_plan);
53d47b78 5
6{
7 use Web::Simple 'PostTest';
8 package PostTest;
60647485 9 sub dispatch_request {
5705ec1e 10 sub (%:foo=&:bar~) {
53d47b78 11 $_[1]->{bar} ||= 'EMPTY';
12 [ 200,
13 [ "Content-type" => "text/plain" ],
14 [ join(' ',@{$_[1]}{qw(foo bar)}) ]
15 ]
16 },
05aafc1a 17 sub (*baz=) {
18 [ 200,
19 [ "Content-type" => "text/plain" ],
20 [ $_[1]->reason || $_[1]->filename ],
21 ]
22 },
24ecd3bd 23 sub (POST + %* + %biff=) {
24 $_[1]->{bar} ||= 'EMPTY';
25 [ 200,
26 [ "Content-type" => "text/plain" ],
27 [ join(' ',@{$_[1]}{qw(biff bong)}) ]
28 ]
29 },
92e23550 30 }
53d47b78 31}
32
33use HTTP::Request::Common qw(GET POST);
34
35my $app = PostTest->new;
1bba6f88 36sub run_request { $app->run_test_request(@_); }
53d47b78 37
9f3d2dd9 38my $get = run_request(GET 'http://localhost/');
53d47b78 39
40cmp_ok($get->code, '==', 404, '404 on GET');
41
9f3d2dd9 42my $no_body = run_request(POST 'http://localhost/');
53d47b78 43
44cmp_ok($no_body->code, '==', 404, '404 with empty body');
45
9f3d2dd9 46my $no_foo = run_request(POST 'http://localhost/' => [ bar => 'BAR' ]);
53d47b78 47
48cmp_ok($no_foo->code, '==', 404, '404 with no foo param');
49
9f3d2dd9 50my $no_bar = run_request(POST 'http://localhost/' => [ foo => 'FOO' ]);
53d47b78 51
52cmp_ok($no_bar->code, '==', 200, '200 with only foo param');
53
54is($no_bar->content, 'FOO EMPTY', 'bar defaulted');
55
56my $both = run_request(
9f3d2dd9 57 POST 'http://localhost/' => [ foo => 'FOO', bar => 'BAR' ]
53d47b78 58);
59
60cmp_ok($both->code, '==', 200, '200 with both params');
61
62is($both->content, 'FOO BAR', 'both params returned');
05aafc1a 63
64my $upload = run_request(
65 POST 'http://localhost'
66 => Content_Type => 'form-data'
67 => Content => [
68 foo => 'FOO',
69 bar => 'BAR'
70 ]
71);
72
73cmp_ok($upload->code, '==', 200, '200 with multipart');
74
75is($upload->content, 'FOO BAR', 'both params returned');
76
24ecd3bd 77my $upload_splat = run_request(
78 POST 'http://localhost'
79 => Content_Type => 'form-data'
80 => Content => [
81 biff => 'frew',
82 bong => 'fru'
83 ]
84);
85
86cmp_ok($upload_splat->code, '==', 200, '200 with multipart');
87
88is($upload_splat->content, 'frew fru', 'both params returned');
89
05aafc1a 90my $upload_wrongtype = run_request(
9f3d2dd9 91 POST 'http://localhost'
05aafc1a 92 => [ baz => 'fleem' ]
93);
94
95is(
96 $upload_wrongtype->content,
97 'field baz exists with value fleem but body was not multipart/form-data',
98 'error points out wrong body type'
99);
100
101my $upload_notupload = run_request(
102 POST 'http://localhost'
103 => Content_Type => 'form-data'
104 => Content => [ baz => 'fleem' ]
105);
106
107is(
108 $upload_notupload->content,
109 'field baz exists with value fleem but was not an upload',
110 'error points out field was not an upload'
111);
112
113my $upload_isupload = run_request(
114 POST 'http://localhost'
115 => Content_Type => 'form-data'
116 => Content => [
117 baz => [
118 undef, 'TESTFILE',
119 Content => 'test content', 'Content-Type' => 'text/plain'
120 ],
121 ]
122);
123
124is(
125 $upload_isupload->content,
126 'TESTFILE',
127 'Actual upload returns filename ok'
128);