Minimal pod, and removed req on YAML - tests will skip all if not found
[catagits/HTTP-Body.git] / t / 04multipart.t
1 #!perl
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7
8 eval { require YAML; import YAML 'LoadFile'; };
9 if ($@) {
10   eval { require YAML::Syck; import YAML::Syck 'LoadFile'; }
11 }
12
13 plan skip_all => 'Tests need YAML or YAML::Syck' if $@;
14
15 plan tests => 55;
16
17 use Cwd;
18 use HTTP::Body;
19 use File::Spec::Functions;
20 use IO::File;
21
22 my $path = catdir( getcwd(), 't', 'data', 'multipart' );
23
24 for ( my $i = 1; $i <= 11; $i++ ) {
25
26     my $test    = sprintf( "%.3d", $i );
27     my $headers = LoadFile( catfile( $path, "$test-headers.yml" ) );
28     my $results = LoadFile( catfile( $path, "$test-results.yml" ) );
29     my $content = IO::File->new( catfile( $path, "$test-content.dat" ) );
30     my $body    = HTTP::Body->new( $headers->{'Content-Type'}, $headers->{'Content-Length'} );
31
32     binmode $content, ':raw';
33
34     while ( $content->read( my $buffer, 1024 ) ) {
35         $body->add($buffer);
36     }
37     
38     for my $field ( keys %{ $body->upload } ) {
39
40         my $value = $body->upload->{$field};
41
42         for ( ( ref($value) eq 'ARRAY' ) ? @{$value} : $value ) {
43             delete $_->{tempname};
44         }
45     }
46
47     is_deeply( $body->body, $results->{body}, "$test MultiPart body" );
48     is_deeply( $body->param, $results->{param}, "$test MultiPart param" );
49     is_deeply( $body->upload, $results->{upload}, "$test MultiPart upload" );
50     cmp_ok( $body->state, 'eq', 'done', "$test MultiPart state" );
51     cmp_ok( $body->length, '==', $headers->{'Content-Length'}, "$test MultiPart length" );
52 }