minor doc updates
[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 },
92e23550 23 }
53d47b78 24}
25
26use HTTP::Request::Common qw(GET POST);
27
28my $app = PostTest->new;
1bba6f88 29sub run_request { $app->run_test_request(@_); }
53d47b78 30
9f3d2dd9 31my $get = run_request(GET 'http://localhost/');
53d47b78 32
33cmp_ok($get->code, '==', 404, '404 on GET');
34
9f3d2dd9 35my $no_body = run_request(POST 'http://localhost/');
53d47b78 36
37cmp_ok($no_body->code, '==', 404, '404 with empty body');
38
9f3d2dd9 39my $no_foo = run_request(POST 'http://localhost/' => [ bar => 'BAR' ]);
53d47b78 40
41cmp_ok($no_foo->code, '==', 404, '404 with no foo param');
42
9f3d2dd9 43my $no_bar = run_request(POST 'http://localhost/' => [ foo => 'FOO' ]);
53d47b78 44
45cmp_ok($no_bar->code, '==', 200, '200 with only foo param');
46
47is($no_bar->content, 'FOO EMPTY', 'bar defaulted');
48
49my $both = run_request(
9f3d2dd9 50 POST 'http://localhost/' => [ foo => 'FOO', bar => 'BAR' ]
53d47b78 51);
52
53cmp_ok($both->code, '==', 200, '200 with both params');
54
55is($both->content, 'FOO BAR', 'both params returned');
05aafc1a 56
57my $upload = run_request(
58 POST 'http://localhost'
59 => Content_Type => 'form-data'
60 => Content => [
61 foo => 'FOO',
62 bar => 'BAR'
63 ]
64);
65
66cmp_ok($upload->code, '==', 200, '200 with multipart');
67
68is($upload->content, 'FOO BAR', 'both params returned');
69
70my $upload_wrongtype = run_request(
9f3d2dd9 71 POST 'http://localhost'
05aafc1a 72 => [ baz => 'fleem' ]
73);
74
75is(
76 $upload_wrongtype->content,
77 'field baz exists with value fleem but body was not multipart/form-data',
78 'error points out wrong body type'
79);
80
81my $upload_notupload = run_request(
82 POST 'http://localhost'
83 => Content_Type => 'form-data'
84 => Content => [ baz => 'fleem' ]
85);
86
87is(
88 $upload_notupload->content,
89 'field baz exists with value fleem but was not an upload',
90 'error points out field was not an upload'
91);
92
93my $upload_isupload = run_request(
94 POST 'http://localhost'
95 => Content_Type => 'form-data'
96 => Content => [
97 baz => [
98 undef, 'TESTFILE',
99 Content => 'test content', 'Content-Type' => 'text/plain'
100 ],
101 ]
102);
103
104is(
105 $upload_isupload->content,
106 'TESTFILE',
107 'Actual upload returns filename ok'
108);