Changing default behavior of upload handling to stop taking over the upload extension...
[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 File::Spec::Functions;
11 use File::Temp qw/ tempdir /;
12
13 SKIP: {
14     eval { require HTTP::Message::PSGI };
15
16     skip "Plack not installed", 13 if $@;
17
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');
23
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           ],
33           arg2 => [
34             undef, '',
35             'Content-Type' =>'text/plain; charset=SHIFT_JIS',
36             'Content' => $string_in_shiftjis,
37           ],
38           arg2 => [
39             undef, '',
40             'Content-Type' =>'text/plain; charset=SHIFT_JIS',
41             'Content' => $string_in_shiftjis,
42           ],
43           arg3 => [
44             "$path", Encode::encode_utf8('♥ttachment.txt'),
45             'Content-Type' => 'text/plain; charset=UTF-8',
46             'Content' => $string_in_utf8,
47           ],
48           file => [
49             "$path", Encode::encode_utf8('♥ttachment.txt'), 'Content-Type' =>'text/html; charset=UTF-8'
50           ],
51         ];
52
53
54     ok my $env = HTTP::Message::PSGI::req_to_psgi($req);
55     ok my $fh = $env->{'psgi.input'};
56     ok my $body = HTTP::Body->new( $req->header('Content-Type'), $req->header('Content-Length') );
57     ok my $tempdir = tempdir( 'XXXXXXX', CLEANUP => 1, DIR => File::Spec->tmpdir() );
58     $body->tmpdir($tempdir);
59
60     binmode $fh, ':raw';
61
62     while ( $fh->read( my $buffer, 1024 ) ) {
63       $body->add($buffer);
64     }
65
66     is $body->param->{'arg0'}, 'helloworld';
67     is $body->param->{'arg1'}, $string_in_utf8;
68     is $body->param->{'arg2'}[0], $string_in_shiftjis;
69     is $body->param->{'arg2'}[1], $string_in_shiftjis;
70
71     cmp_deeply(
72         $body->part_data->{'arg0'},
73         {
74             data => 'helloworld',
75             headers => {
76                 'Content-Disposition' => re(qr{^form-data\b}),
77             },
78             done => 1,
79             name => 'arg0',
80             size => 10,
81         },
82         'arg0 part data correct',
83     );
84     cmp_deeply(
85         $body->part_data->{'arg1'},
86         {
87             data => $string_in_utf8,
88             headers => {
89                 'Content-Disposition' => re(qr{^form-data\b}),
90                 'Content-Type' => 'text/plain; charset=UTF-8',
91             },
92             done => 1,
93             name => 'arg1',
94             size => length($string_in_utf8),
95         },
96         'arg1 part data correct',
97     );
98
99     cmp_deeply(
100         $body->part_data->{'arg2'},
101         [
102             ({
103                 data => $string_in_shiftjis,
104                 headers => {
105                     'Content-Disposition' => re(qr{^form-data\b}),
106                     'Content-Type' => 'text/plain; charset=SHIFT_JIS',
107                 },
108                 done => 1,
109                 name => 'arg2',
110                 size => length($string_in_shiftjis),
111             }) x 2,
112         ],
113         'arg2 part data correct',
114     );
115
116     my $filename = $body->upload->{'arg3'} ? ($body->upload->{'arg3'}->{tempname}||"") : "";
117
118     ok($filename =~ qr/\Q$HTTP::Body::MultiPart::file_temp_suffix\E$/, 'arg3 temp file extension correct');
119
120 };
121
122 done_testing;