Fixed test which failed if Plack wasn't installed
[catagits/HTTP-Body.git] / t / 10mixparamcontent.t
1 use utf8;
2 use warnings;
3 use strict;
4
5 use Test::More;
6 use Test::Deep;
7 use HTTP::Body;
8 use HTTP::Request::Common;
9 use Encode;
10 use HTTP::Message::PSGI ();
11 use File::Spec::Functions;
12 use File::Temp qw/ tempdir /;
13
14 SKIP: {
15     eval { require HTTP::Message::PSGI };
16
17     skip "Plack not installed", 13 if $@;
18
19     my $string_for_utf8 = 'test ♥';
20     my $string_in_utf8 = Encode::encode('UTF-8',$string_for_utf8);
21     my $string_for_shiftjis = 'test テスト';
22     my $string_in_shiftjis = Encode::encode('SHIFT_JIS',$string_for_shiftjis);
23     my $path = File::Spec->catfile('t', 'utf8.txt');
24
25     ok my $req = POST '/root/echo_arg',
26       Content_Type => 'form-data',
27         Content =>  [
28           arg0 => 'helloworld',
29           arg1 => [
30             undef, '',
31             'Content-Type' =>'text/plain; charset=UTF-8',
32             'Content' => $string_in_utf8, ],
33           arg2 => [
34             undef, '',
35             'Content-Type' =>'text/plain; charset=SHIFT_JIS',
36             'Content' => $string_in_shiftjis, ],
37           arg2 => [
38             undef, '',
39             'Content-Type' =>'text/plain; charset=SHIFT_JIS',
40             'Content' => $string_in_shiftjis, ],
41           file => [
42             "$path", Encode::encode_utf8('♥ttachment.txt'), 'Content-Type' =>'text/html; charset=UTF-8'
43           ],
44         ];
45
46
47     ok my $env = HTTP::Message::PSGI::req_to_psgi($req);
48     ok my $fh = $env->{'psgi.input'};
49     ok my $body = HTTP::Body->new( $req->header('Content-Type'), $req->header('Content-Length') );
50     ok my $tempdir = tempdir( 'XXXXXXX', CLEANUP => 1, DIR => File::Spec->tmpdir() );
51     $body->tmpdir($tempdir);
52
53     binmode $fh, ':raw';
54
55     while ( $fh->read( my $buffer, 1024 ) ) {
56       $body->add($buffer);
57     }
58
59     is $body->param->{'arg0'}, 'helloworld';
60     is $body->param->{'arg1'}, $string_in_utf8;
61     is $body->param->{'arg2'}[0], $string_in_shiftjis;
62     is $body->param->{'arg2'}[1], $string_in_shiftjis;
63
64     cmp_deeply(
65         $body->part_data->{'arg0'},
66         {
67             data => 'helloworld',
68             headers => {
69                 'Content-Disposition' => re(qr{^form-data\b}),
70             },
71             done => 1,
72             name => 'arg0',
73             size => 10,
74         },
75         'arg0 part data correct',
76     );
77     cmp_deeply(
78         $body->part_data->{'arg1'},
79         {
80             data => $string_in_utf8,
81             headers => {
82                 'Content-Disposition' => re(qr{^form-data\b}),
83                 'Content-Type' => 'text/plain; charset=UTF-8',
84             },
85             done => 1,
86             name => 'arg1',
87             size => length($string_in_utf8),
88         },
89         'arg1 part data correct',
90     );
91
92     cmp_deeply(
93         $body->part_data->{'arg2'},
94         [
95             ({
96                 data => $string_in_shiftjis,
97                 headers => {
98                     'Content-Disposition' => re(qr{^form-data\b}),
99                     'Content-Type' => 'text/plain; charset=SHIFT_JIS',
100                 },
101                 done => 1,
102                 name => 'arg2',
103                 size => length($string_in_shiftjis),
104             }) x 2,
105         ],
106         'arg2 part data correct',
107     );
108
109 };
110
111 done_testing;