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