88a09a477f797b65b885c2a781e69474938a5d80
[catagits/HTTP-Body.git] / t / 10mixparamcontent.t
1 use utf8;
2 use warnings;
3 use strict;
4
5 use Test::More;
6 use HTTP::Body;
7 use HTTP::Request::Common;
8 use Encode;
9 use HTTP::Message::PSGI ();
10 use File::Spec::Functions;
11 use File::Temp qw/ tempdir /;
12
13
14 my $utf8 = 'test ♥';
15 my $shiftjs = 'test テスト';
16 my $path = File::Spec->catfile('t', 'utf8.txt');
17
18 ok my $req = POST '/root/echo_arg',
19   Content_Type => 'form-data',
20     Content =>  [
21       arg0 => 'helloworld',
22       arg1 => [
23         undef, '',
24         'Content-Type' =>'text/plain; charset=UTF-8',
25         'Content' => Encode::encode('UTF-8', $utf8)],
26       arg2 => [
27         undef, '',
28         'Content-Type' =>'text/plain; charset=SHIFT_JIS',
29         'Content' => Encode::encode('SHIFT_JIS', $shiftjs)],
30       arg2 => [
31         undef, '',
32         'Content-Type' =>'text/plain; charset=SHIFT_JIS',
33         'Content' => Encode::encode('SHIFT_JIS', $shiftjs)],
34       file => [
35         "$path", Encode::encode_utf8('♥ttachment.txt'), 'Content-Type' =>'text/html; charset=UTF-8'
36       ],
37     ];
38
39
40 ok my $env = HTTP::Message::PSGI::req_to_psgi($req);
41 ok my $fh = $env->{'psgi.input'};
42 ok my $body = HTTP::Body->new( $req->header('Content-Type'), $req->header('Content-Length') );
43 ok my $tempdir = tempdir( 'XXXXXXX', CLEANUP => 1, DIR => File::Spec->tmpdir() );
44 $body->tmpdir($tempdir);
45
46 binmode $fh, ':raw';
47
48 while ( $fh->read( my $buffer, 1024 ) ) {
49   $body->add($buffer);
50 }
51
52 is $body->param->{'arg0'}, 'helloworld';
53 is Encode::decode('UTF-8', $body->param->{'arg1'}), $utf8;
54 is Encode::decode('SHIFT_JIS', $body->param->{'arg2'}[0]), $shiftjs;
55
56 is $body->part_data->{'arg0'}->{data}, 'helloworld';
57 is Encode::decode('UTF-8', $body->part_data->{'arg1'}->{data}), $utf8;
58 is Encode::decode('SHIFT_JIS', $body->part_data->{'arg2'}[0]->{data}), $shiftjs;
59
60 done_testing;