File: 10mixparamcontent.t

package info (click to toggle)
libhttp-body-perl 1.23-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 532 kB
  • sloc: perl: 980; makefile: 2
file content (122 lines) | stat: -rw-r--r-- 3,597 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
use utf8;
use warnings;
use strict;

use Test::More;
use Test::Deep;
use HTTP::Body;
use HTTP::Request::Common;
use Encode;
use File::Spec::Functions;
use File::Temp qw/ tempdir /;

SKIP: {
    eval { require HTTP::Message::PSGI };

    skip "Plack not installed", 13 if $@;

    my $string_for_utf8 = 'test ♥';
    my $string_in_utf8 = Encode::encode('UTF-8',$string_for_utf8);
    my $string_for_shiftjis = 'test テスト';
    my $string_in_shiftjis = Encode::encode('SHIFT_JIS',$string_for_shiftjis);
    my $path = File::Spec->catfile('t', 'utf8.txt');

    ok my $req = POST '/root/echo_arg',
      Content_Type => 'form-data',
        Content =>  [
          arg0 => 'helloworld',
          arg1 => [
            undef, '',
            'Content-Type' =>'text/plain; charset=UTF-8',
            'Content' => $string_in_utf8,
          ],
          arg2 => [
            undef, '',
            'Content-Type' =>'text/plain; charset=SHIFT_JIS',
            'Content' => $string_in_shiftjis,
          ],
          arg2 => [
            undef, '',
            'Content-Type' =>'text/plain; charset=SHIFT_JIS',
            'Content' => $string_in_shiftjis,
          ],
          arg3 => [
            "$path", Encode::encode_utf8('♥ttachment.txt'),
            'Content-Type' => 'text/plain; charset=UTF-8',
            'Content' => $string_in_utf8,
          ],
          file => [
            "$path", Encode::encode_utf8('♥ttachment.txt'), 'Content-Type' =>'text/html; charset=UTF-8'
          ],
        ];


    ok my $env = HTTP::Message::PSGI::req_to_psgi($req);
    ok my $fh = $env->{'psgi.input'};
    ok my $body = HTTP::Body->new( $req->header('Content-Type'), $req->header('Content-Length') );
    ok my $tempdir = tempdir( 'XXXXXXX', CLEANUP => 1, DIR => File::Spec->tmpdir() );
    $body->tmpdir($tempdir);

    binmode $fh, ':raw';

    while ( $fh->read( my $buffer, 1024 ) ) {
      $body->add($buffer);
    }

    is $body->param->{'arg0'}, 'helloworld';
    is $body->param->{'arg1'}, $string_in_utf8;
    is $body->param->{'arg2'}[0], $string_in_shiftjis;
    is $body->param->{'arg2'}[1], $string_in_shiftjis;

    cmp_deeply(
        $body->part_data->{'arg0'},
        {
            data => 'helloworld',
            headers => {
                'Content-Disposition' => re(qr{^form-data\b}),
            },
            done => 1,
            name => 'arg0',
            size => 10,
        },
        'arg0 part data correct',
    );
    cmp_deeply(
        $body->part_data->{'arg1'},
        {
            data => $string_in_utf8,
            headers => {
                'Content-Disposition' => re(qr{^form-data\b}),
                'Content-Type' => 'text/plain; charset=UTF-8',
            },
            done => 1,
            name => 'arg1',
            size => length($string_in_utf8),
        },
        'arg1 part data correct',
    );

    cmp_deeply(
        $body->part_data->{'arg2'},
        [
            ({
                data => $string_in_shiftjis,
                headers => {
                    'Content-Disposition' => re(qr{^form-data\b}),
                    'Content-Type' => 'text/plain; charset=SHIFT_JIS',
                },
                done => 1,
                name => 'arg2',
                size => length($string_in_shiftjis),
            }) x 2,
        ],
        'arg2 part data correct',
    );

    my $filename = $body->upload->{'arg3'} ? ($body->upload->{'arg3'}->{tempname}||"") : "";

    ok($filename =~ qr/\Q$HTTP::Body::MultiPart::file_temp_suffix\E$/, 'arg3 temp file extension correct');

};

done_testing;