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