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