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