Commit | Line | Data |
53d47b78 |
1 | use strict; |
2 | use warnings FATAL => 'all'; |
3 | |
05aafc1a |
4 | use Test::More qw(no_plan); |
b91b7bc9 |
5 | use 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 | |
27 | use HTTP::Request::Common qw(GET POST); |
28 | |
29 | my $app = PostTest->new; |
1bba6f88 |
30 | sub run_request { $app->run_test_request(@_); } |
53d47b78 |
31 | |
9f3d2dd9 |
32 | my $get = run_request(GET 'http://localhost/'); |
53d47b78 |
33 | |
34 | cmp_ok($get->code, '==', 404, '404 on GET'); |
35 | |
9f3d2dd9 |
36 | my $no_body = run_request(POST 'http://localhost/'); |
53d47b78 |
37 | |
38 | cmp_ok($no_body->code, '==', 404, '404 with empty body'); |
39 | |
9f3d2dd9 |
40 | my $no_foo = run_request(POST 'http://localhost/' => [ bar => 'BAR' ]); |
53d47b78 |
41 | |
42 | cmp_ok($no_foo->code, '==', 404, '404 with no foo param'); |
43 | |
9f3d2dd9 |
44 | my $no_bar = run_request(POST 'http://localhost/' => [ foo => 'FOO' ]); |
53d47b78 |
45 | |
46 | cmp_ok($no_bar->code, '==', 200, '200 with only foo param'); |
47 | |
48 | is($no_bar->content, 'FOO EMPTY', 'bar defaulted'); |
49 | |
50 | my $both = run_request( |
9f3d2dd9 |
51 | POST 'http://localhost/' => [ foo => 'FOO', bar => 'BAR' ] |
53d47b78 |
52 | ); |
53 | |
54 | cmp_ok($both->code, '==', 200, '200 with both params'); |
55 | |
56 | is($both->content, 'FOO BAR', 'both params returned'); |
05aafc1a |
57 | |
58 | my $upload = run_request( |
59 | POST 'http://localhost' |
60 | => Content_Type => 'form-data' |
61 | => Content => [ |
62 | foo => 'FOO', |
63 | bar => 'BAR' |
64 | ] |
65 | ); |
66 | |
67 | cmp_ok($upload->code, '==', 200, '200 with multipart'); |
68 | |
69 | is($upload->content, 'FOO BAR', 'both params returned'); |
70 | |
71 | my $upload_wrongtype = run_request( |
9f3d2dd9 |
72 | POST 'http://localhost' |
05aafc1a |
73 | => [ baz => 'fleem' ] |
74 | ); |
75 | |
76 | is( |
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 | |
82 | my $upload_notupload = run_request( |
83 | POST 'http://localhost' |
84 | => Content_Type => 'form-data' |
85 | => Content => [ baz => 'fleem' ] |
86 | ); |
87 | |
88 | is( |
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 | |
94 | my $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 | |
105 | is( |
106 | $upload_isupload->content, |
107 | 'TESTFILE', |
108 | 'Actual upload returns filename ok' |
109 | ); |
b91b7bc9 |
110 | |
111 | my $utf8_req = POST "http://localhost" |
112 | => Content_Type => "form-data" |
113 | => Content => [ foo => "FOOü", bar => "BAR" ]; |
114 | my $utf8_expect = "FOOü BAR"; |
115 | |
116 | my $utf8 = run_request($utf8_req); |
117 | is($utf8->content, decode_utf8($utf8_expect), "params utf8-decoded by default"); |
118 | |
119 | my $utf8_bytes = PostTest->new( param_encoding => undef ) |
120 | ->run_test_request($utf8_req); |
121 | is($utf8_bytes->content, $utf8_expect, "disabling the param encoding works"); |
122 | |
123 | my $iso_req = POST "http://localhost" |
124 | => Content_Type => "form-data" |
125 | => Content => [ foo => "FOO\x{FC}", bar => "BAR" ]; |
126 | my $iso = PostTest->new( param_encoding => "iso-8859-15" ) |
127 | ->run_test_request($iso_req); |
128 | is($iso->content, "FOO\x{FC} BAR", "changing the param encoding works"); |
129 | |
130 | 1; |