more comprehensive test for part_data
[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;
10use HTTP::Message::PSGI ();
11use File::Spec::Functions;
12use File::Temp qw/ tempdir /;
13
14
af65f6d3 15my $string_for_utf8 = 'test ♥';
16my $string_in_utf8 = Encode::encode('UTF-8',$string_for_utf8);
17my $string_for_shiftjis = 'test テスト';
18my $string_in_shiftjis = Encode::encode('SHIFT_JIS',$string_for_shiftjis);
2f14a496 19my $path = File::Spec->catfile('t', 'utf8.txt');
20
21ok 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',
af65f6d3 28 'Content' => $string_in_utf8, ],
2f14a496 29 arg2 => [
30 undef, '',
31 'Content-Type' =>'text/plain; charset=SHIFT_JIS',
af65f6d3 32 'Content' => $string_in_shiftjis, ],
2f14a496 33 arg2 => [
34 undef, '',
35 'Content-Type' =>'text/plain; charset=SHIFT_JIS',
af65f6d3 36 'Content' => $string_in_shiftjis, ],
2f14a496 37 file => [
38 "$path", Encode::encode_utf8('♥ttachment.txt'), 'Content-Type' =>'text/html; charset=UTF-8'
39 ],
40 ];
41
42
43ok my $env = HTTP::Message::PSGI::req_to_psgi($req);
44ok my $fh = $env->{'psgi.input'};
45ok my $body = HTTP::Body->new( $req->header('Content-Type'), $req->header('Content-Length') );
46ok my $tempdir = tempdir( 'XXXXXXX', CLEANUP => 1, DIR => File::Spec->tmpdir() );
47$body->tmpdir($tempdir);
48
49binmode $fh, ':raw';
50
51while ( $fh->read( my $buffer, 1024 ) ) {
52 $body->add($buffer);
53}
54
55is $body->param->{'arg0'}, 'helloworld';
af65f6d3 56is $body->param->{'arg1'}, $string_in_utf8;
57is $body->param->{'arg2'}[0], $string_in_shiftjis;
58is $body->param->{'arg2'}[1], $string_in_shiftjis;
2f14a496 59
af65f6d3 60cmp_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);
73cmp_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
88cmp_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);
2f14a496 104
105done_testing;