1b27ebd67668a59ddafdd9b72d4e8980ea1e1664
[catagits/HTTP-Body.git] / t / 04multipart.t
1 #!perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 98;
7
8 use Cwd;
9 use HTTP::Body;
10 use File::Spec::Functions;
11 use IO::File;
12 use YAML;
13 use File::Temp qw/ tempdir /;
14
15 my $path = catdir( getcwd(), 't', 'data', 'multipart' );
16
17 for ( my $i = 1; $i <= 12; $i++ ) {
18
19     my $test    = sprintf( "%.3d", $i );
20     my $headers = YAML::LoadFile( catfile( $path, "$test-headers.yml" ) );
21     my $results = YAML::LoadFile( catfile( $path, "$test-results.yml" ) );
22     my $content = IO::File->new( catfile( $path, "$test-content.dat" ) );
23     my $body    = HTTP::Body->new( $headers->{'Content-Type'}, $headers->{'Content-Length'} );
24     my $tempdir = tempdir( 'XXXXXXX', CLEANUP => 1, DIR => File::Spec->tmpdir() );
25     $body->tmpdir($tempdir);
26
27     my $regex_tempdir = quotemeta($tempdir);
28
29     binmode $content, ':raw';
30
31     while ( $content->read( my $buffer, 1024 ) ) {
32         $body->add($buffer);
33     }
34     
35     # Save tempnames for later deletion
36     my @temps;
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             like($_->{tempname}, qr{$regex_tempdir}, "has tmpdir $tempdir");
44             push @temps, delete $_->{tempname};
45         }
46     }
47
48     is_deeply( $body->body, $results->{body}, "$test MultiPart body" );
49     is_deeply( $body->param, $results->{param}, "$test MultiPart param" );
50     is_deeply( $body->upload, $results->{upload}, "$test MultiPart upload" );
51     cmp_ok( $body->state, 'eq', 'done', "$test MultiPart state" );
52     cmp_ok( $body->length, '==', $body->content_length, "$test MultiPart length" );
53     
54     # Clean up temp files created
55     unlink map { $_ } grep { -e $_ } @temps;
56 }