File: 030_basic.t

package info (click to toggle)
libhttp-multipartparser-perl 0.02-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 340 kB
  • sloc: perl: 1,923; makefile: 2
file content (65 lines) | stat: -rw-r--r-- 1,407 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
#!perl

use strict;
use warnings;

use FindBin;
use lib "$FindBin::Bin/lib";

use HTTP::MultiPartParser qw[];
use PAML                   qw[];
use Cwd                    qw[getcwd];
use File::Spec::Functions  qw[catdir catfile];

use Test::More;
use Test::Deep;

my $base = catdir(getcwd(), 't', 'data');

foreach my $number ('001'..'012') {
    my $test = PAML::LoadFile(catfile($base, "$number-test.pml"));
    my $exp  = PAML::LoadFile(catfile($base, "$number-exp.pml"));
    my $path = catfile($base, "$number-content.dat");

    my @got;
    my %part;

    my $on_header = sub {
        my ($header) = @_;
        $part{header} = $header;
    };

    my $on_body = sub {
        my ($chunk, $final) = @_;

        $part{body} .= $chunk;
        if ($final) {
            push @got, { %part };
            %part = ();
        }
    };
    
    my $parser = HTTP::MultiPartParser->new(
        boundary  => $test->{boundary},
        on_header => $on_header,
        on_body   => $on_body,
    );

    open(my $fh, '<:raw', $path)
      or die qq/Could not open: '$path': '$!'/;

    while () {
        my $n = read($fh, my $buffer, 1024);
        unless ($n) {
            die qq/Could not read from fh: '$!'/
              unless defined $n;
            last;
        }
        $parser->parse($buffer);
    }
    $parser->finish;
    cmp_deeply(\@got, $exp, "$number-content.dat");
}

done_testing();