File: multipart.t

package info (click to toggle)
libhttp-entity-parser-perl 0.25-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 476 kB
  • sloc: perl: 394; makefile: 2
file content (111 lines) | stat: -rw-r--r-- 2,716 bytes parent folder | download | duplicates (3)
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
use strict;
use warnings;
use Test::More;
use HTTP::Entity::Parser::MultiPart;
use Hash::MultiValue;
use HTTP::Headers;
require "./t/Util.pm";
t::Util->import();
use File::Basename;

my $content = qq{------BOUNDARY
Content-Disposition: form-data; name="hoge"
Content-Type: text/plain

fuga
------BOUNDARY
Content-Disposition: form-data; name="hoge"
Content-Type: text/plain

hige
------BOUNDARY
Content-Disposition: form-data; name="nobuko"
Content-Type: text/plain

iwaki
------BOUNDARY
Content-Disposition: form-data; name="test_upload_file"; filename="yappo.txt"
Content-Type: text/plain

SHOGUN
------BOUNDARY
Content-Disposition: form-data; name="test_upload_file"; filename="yappo2.txt"
Content-Type: text/plain

SHOGUN2
------BOUNDARY
Content-Disposition: form-data; name="test_upload_file3"; filename="yappo3.txt"
Content-Type: text/plain

SHOGUN3
------BOUNDARY
Content-Disposition: form-data; name="test_upload_file4"; filename="yappo4.txt"
Content-Type: text/plain

SHOGUN4
------BOUNDARY
Content-Disposition: form-data; name="test_upload_file4"; filename="yappo5.txt"
Content-Type: text/plain

SHOGUN4
------BOUNDARY
Content-Disposition: form-data; name="test_upload_file6"; filename="yappo6.txt"
Foo: bar
  baz
X: Y:Z
Content-Type: text/plain

SHOGUN6
------BOUNDARY--
};
$content =~ s/\r\n/\n/g;
$content =~ s/\n/\r\n/g;

my $tmpdir;

{
    my $env = {
        CONTENT_LENGTH => length($content),
        CONTENT_TYPE   => 'multipart/form-data; boundary=----BOUNDARY',
    };

    # read from file.
    my $parser = HTTP::Entity::Parser::MultiPart->new($env);
    $parser->add($_) for split //, $content;
    my ($params, $uploads) = $parser->finalize();

    is_deeply( Hash::MultiValue->new(@$params)->as_hashref_multi, {   
        hoge => ['fuga', 'hige'],
        nobuko => ['iwaki'],
    });

    $uploads = Hash::MultiValue->new(@$uploads);

    my @test_upload_file = $uploads->get_all('test_upload_file');
    is 0+@test_upload_file, 2;
    is slurp($test_upload_file[0]->{tempname}), 'SHOGUN';
    is slurp($test_upload_file[1]->{tempname}), 'SHOGUN2';

    {
        my $test_upload_file3 = $uploads->{'test_upload_file3'};
        is slurp($test_upload_file3->{tempname}), 'SHOGUN3';

        my @test_upload_file6 = $uploads->{'test_upload_file6'};
        is slurp($test_upload_file6[0]->{tempname}), 'SHOGUN6';
        my $header = HTTP::Headers->new(@{$test_upload_file6[0]->{headers}});
        is $header->header('Content-Type'), 'text/plain';
        is $header->content_type, 'text/plain';
        is $header->header('X'), 'Y:Z';
        is $header->header('Foo'), 'bar baz';
    }

    $tmpdir = dirname($test_upload_file[0]->{tempname});
}

ok(! -d $tmpdir);

done_testing();