Commit | Line | Data |
a9df1200 |
1 | #!perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
0a66fd23 |
6 | use Test::More tests => 60; |
a9df1200 |
7 | |
8 | use Cwd; |
9 | use HTTP::Body; |
10 | use File::Spec::Functions; |
11 | use IO::File; |
12 | use YAML; |
13 | |
14 | my $path = catdir( getcwd(), 't', 'data', 'multipart' ); |
15 | |
0a66fd23 |
16 | for ( my $i = 1; $i <= 12; $i++ ) { |
a9df1200 |
17 | |
18 | my $test = sprintf( "%.3d", $i ); |
19 | my $headers = YAML::LoadFile( catfile( $path, "$test-headers.yml" ) ); |
20 | my $results = YAML::LoadFile( catfile( $path, "$test-results.yml" ) ); |
21 | my $content = IO::File->new( catfile( $path, "$test-content.dat" ) ); |
22 | my $body = HTTP::Body->new( $headers->{'Content-Type'}, $headers->{'Content-Length'} ); |
23 | |
24 | binmode $content, ':raw'; |
25 | |
26 | while ( $content->read( my $buffer, 1024 ) ) { |
27 | $body->add($buffer); |
28 | } |
29 | |
1ced50e0 |
30 | # Save tempnames for later deletion |
31 | my @temps; |
32 | |
a9df1200 |
33 | for my $field ( keys %{ $body->upload } ) { |
34 | |
35 | my $value = $body->upload->{$field}; |
36 | |
37 | for ( ( ref($value) eq 'ARRAY' ) ? @{$value} : $value ) { |
1ced50e0 |
38 | push @temps, delete $_->{tempname}; |
a9df1200 |
39 | } |
40 | } |
41 | |
42 | is_deeply( $body->body, $results->{body}, "$test MultiPart body" ); |
43 | is_deeply( $body->param, $results->{param}, "$test MultiPart param" ); |
44 | is_deeply( $body->upload, $results->{upload}, "$test MultiPart upload" ); |
45 | cmp_ok( $body->state, 'eq', 'done', "$test MultiPart state" ); |
0a66fd23 |
46 | cmp_ok( $body->length, '==', $body->content_length, "$test MultiPart length" ); |
1ced50e0 |
47 | |
48 | # Clean up temp files created |
49 | unlink map { $_ } grep { -e $_ } @temps; |
a9df1200 |
50 | } |