fix test and code, HT: TBSliver
[catagits/Web-Simple.git] / t / json.t
CommitLineData
7f283bce 1use strict;
2use warnings FATAL => 'all';
3
4use 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 sub (POST + %* + %biff=) {
24 $_[1]->{bar} ||= 'EMPTY';
25 [ 200,
26 [ "Content-type" => "text/plain" ],
27 [ join(' ',@{$_[1]}{qw(biff bong)}) ]
28 ]
29 },
8b48a8e4 30 sub (GET + ?foo=) {
31 [ 200, [ 'Content-type' => 'text/plain' ], [ $_[1] ] ];
32 },
7f283bce 33 }
34}
35
36use HTTP::Request::Common qw(GET POST);
37
38my $app = PostTest->new;
39sub run_request { $app->run_test_request(@_); }
40
41my $get = run_request(GET 'http://localhost/');
42
8b48a8e4 43cmp_ok($get->code, '==', 404, '404 on plain GET');
7f283bce 44
45my $no_body = run_request(POST 'http://localhost/');
46
47cmp_ok($no_body->code, '==', 404, '404 with empty body');
48
49my $no_foo = run_request(POST 'http://localhost/' => [ bar => 'BAR' ]);
50
51cmp_ok($no_foo->code, '==', 404, '404 with no foo param');
52
53my $no_bar = run_request(POST 'http://localhost/' => [ foo => 'FOO' ]);
54
55cmp_ok($no_bar->code, '==', 200, '200 with only foo param');
56
57is($no_bar->content, 'FOO EMPTY', 'bar defaulted');
58
59my $both = run_request(
60 POST 'http://localhost/' => [ foo => 'FOO', bar => 'BAR' ]
61);
62
63cmp_ok($both->code, '==', 200, '200 with both params');
64
65is($both->content, 'FOO BAR', 'both params returned');
66
67my $upload = run_request(
68 POST 'http://localhost'
69 => Content_Type => 'form-data'
70 => Content => [
71 foo => 'FOO',
72 bar => 'BAR'
73 ]
74);
75
76cmp_ok($upload->code, '==', 200, '200 with multipart');
77
78is($upload->content, 'FOO BAR', 'both params returned');
79
80my $upload_splat = run_request(
81 POST 'http://localhost'
82 => Content_Type => 'form-data'
83 => Content => [
84 biff => 'frew',
85 bong => 'fru'
86 ]
87);
88
89cmp_ok($upload_splat->code, '==', 200, '200 with multipart');
90
91is($upload_splat->content, 'frew fru', 'both params returned');
92
93my $upload_wrongtype = run_request(
94 POST 'http://localhost'
95 => [ baz => 'fleem' ]
96);
97
98is(
99 $upload_wrongtype->content,
100 'field baz exists with value fleem but body was not multipart/form-data',
101 'error points out wrong body type'
102);
103
104my $upload_notupload = run_request(
105 POST 'http://localhost'
106 => Content_Type => 'form-data'
107 => Content => [ baz => 'fleem' ]
108);
109
110is(
111 $upload_notupload->content,
112 'field baz exists with value fleem but was not an upload',
113 'error points out field was not an upload'
114);
115
116my $upload_isupload = run_request(
117 POST 'http://localhost'
118 => Content_Type => 'form-data'
119 => Content => [
120 baz => [
121 undef, 'TESTFILE',
122 Content => 'test content', 'Content-Type' => 'text/plain'
123 ],
124 ]
125);
126
127is(
128 $upload_isupload->content,
129 'TESTFILE',
130 'Actual upload returns filename ok'
131);
8b48a8e4 132
133my $query_simple = run_request(GET 'http://localhost?foo=bar');
134
135is($query_simple->content, 'bar', 'simple query string ok');
136
137my $query_json = run_request(GET 'http://localhost?j={"foo": "bar"}');
138
1da2e3c5 139is($query_json->content, 'bar', 'json query string ok');