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