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