Commit | Line | Data |
2f14a496 |
1 | use utf8; |
2 | use warnings; |
3 | use strict; |
4 | |
5 | use Test::More; |
af65f6d3 |
6 | use Test::Deep; |
2f14a496 |
7 | use HTTP::Body; |
8 | use HTTP::Request::Common; |
9 | use Encode; |
2f14a496 |
10 | use File::Spec::Functions; |
11 | use File::Temp qw/ tempdir /; |
12 | |
2fdfe663 |
13 | SKIP: { |
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', |
cc75c886 |
31 | 'Content' => $string_in_utf8, |
32 | ], |
2fdfe663 |
33 | arg2 => [ |
34 | undef, '', |
35 | 'Content-Type' =>'text/plain; charset=SHIFT_JIS', |
cc75c886 |
36 | 'Content' => $string_in_shiftjis, |
37 | ], |
2fdfe663 |
38 | arg2 => [ |
39 | undef, '', |
40 | 'Content-Type' =>'text/plain; charset=SHIFT_JIS', |
cc75c886 |
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 | ], |
2fdfe663 |
48 | file => [ |
49 | "$path", Encode::encode_utf8('♥ttachment.txt'), 'Content-Type' =>'text/html; charset=UTF-8' |
50 | ], |
51 | ]; |
2f14a496 |
52 | |
2f14a496 |
53 | |
2fdfe663 |
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); |
2f14a496 |
59 | |
2fdfe663 |
60 | binmode $fh, ':raw'; |
2f14a496 |
61 | |
2fdfe663 |
62 | while ( $fh->read( my $buffer, 1024 ) ) { |
63 | $body->add($buffer); |
64 | } |
2f14a496 |
65 | |
2fdfe663 |
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; |
af65f6d3 |
70 | |
2fdfe663 |
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, |
af65f6d3 |
88 | headers => { |
89 | 'Content-Disposition' => re(qr{^form-data\b}), |
2fdfe663 |
90 | 'Content-Type' => 'text/plain; charset=UTF-8', |
af65f6d3 |
91 | }, |
92 | done => 1, |
2fdfe663 |
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 | |
cc75c886 |
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 | |
2fdfe663 |
120 | }; |
2f14a496 |
121 | |
122 | done_testing; |