Date of last release in Changes files
[catagits/HTTP-Body.git] / t / 10mixparamcontent.t
CommitLineData
2f14a496 1use utf8;
2use warnings;
3use strict;
4
5use Test::More;
af65f6d3 6use Test::Deep;
2f14a496 7use HTTP::Body;
8use HTTP::Request::Common;
9use Encode;
2f14a496 10use File::Spec::Functions;
11use File::Temp qw/ tempdir /;
12
2fdfe663 13SKIP: {
14 eval { require HTTP::Message::PSGI };
2f14a496 15
2fdfe663 16 skip "Plack not installed", 13 if $@;
2f14a496 17
2fdfe663 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');
2f14a496 23
2fdfe663 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 ];
2f14a496 44
2f14a496 45
2fdfe663 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);
2f14a496 51
2fdfe663 52 binmode $fh, ':raw';
2f14a496 53
2fdfe663 54 while ( $fh->read( my $buffer, 1024 ) ) {
55 $body->add($buffer);
56 }
2f14a496 57
2fdfe663 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;
af65f6d3 62
2fdfe663 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,
af65f6d3 80 headers => {
81 'Content-Disposition' => re(qr{^form-data\b}),
2fdfe663 82 'Content-Type' => 'text/plain; charset=UTF-8',
af65f6d3 83 },
84 done => 1,
2fdfe663 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};
2f14a496 109
110done_testing;