Add version 1.11 of HTTP::Body with new param_order functionality
[catagits/HTTP-Body.git] / t / 04multipart.t
1 #!perl
2
3 use strict;
4 use warnings;
5
6 use FindBin;
7 use lib "$FindBin::Bin/lib";
8
9 use Test::More tests => 153;
10 use Test::Deep;
11
12 use Cwd;
13 use HTTP::Body;
14 use File::Spec::Functions;
15 use IO::File;
16 use PAML;
17 use File::Temp qw/ tempdir /;
18
19 my $path = catdir( getcwd(), 't', 'data', 'multipart' );
20
21 for ( my $i = 1; $i <= 13; $i++ ) {
22
23     my $test    = sprintf( "%.3d", $i );
24     my $headers = PAML::LoadFile( catfile( $path, "$test-headers.pml" ) );
25     my $results = PAML::LoadFile( catfile( $path, "$test-results.pml" ) );
26     my $content = IO::File->new( catfile( $path, "$test-content.dat" ) );
27     my $body    = HTTP::Body->new( $headers->{'Content-Type'}, $headers->{'Content-Length'} );
28     my $tempdir = tempdir( 'XXXXXXX', CLEANUP => 1, DIR => File::Spec->tmpdir() );
29     $body->tmpdir($tempdir);
30
31     my $regex_tempdir = quotemeta($tempdir);
32
33     binmode $content, ':raw';
34
35     while ( $content->read( my $buffer, 1024 ) ) {
36         $body->add($buffer);
37     }
38     
39     # Tests >= 10 use auto-cleanup
40     if ( $i >= 10 ) {
41         $body->cleanup(1);
42     }
43     
44     # Save tempnames for later deletion
45     my @temps;
46     
47     for my $field ( keys %{ $body->upload } ) {
48
49         my $value = $body->upload->{$field};
50
51         for ( ( ref($value) eq 'ARRAY' ) ? @{$value} : $value ) {
52             like($_->{tempname}, qr{$regex_tempdir}, "has tmpdir $tempdir");
53             push @temps, $_->{tempname};
54         }
55         
56         # Tell Test::Deep to ignore tempname values
57         if ( ref $value eq 'ARRAY' ) {
58             for ( @{ $results->{upload}->{$field} } ) {
59                 $_->{tempname} = ignore();
60             }
61         }
62         else {
63             $results->{upload}->{$field}->{tempname} = ignore();
64         }
65     }
66         
67     cmp_deeply( $body->body, $results->{body}, "$test MultiPart body" );
68     cmp_deeply( $body->param, $results->{param}, "$test MultiPart param" );
69     cmp_deeply( $body->param_order, $results->{param_order} ? $results->{param_order} : [], "$test MultiPart param_order" );
70     cmp_deeply( $body->upload, $results->{upload}, "$test MultiPart upload" )
71         if $results->{upload};
72     cmp_ok( $body->state, 'eq', 'done', "$test MultiPart state" );
73     cmp_ok( $body->length, '==', $body->content_length, "$test MultiPart length" );
74     
75     if ( $i < 10 ) {
76         # Clean up temp files created
77         unlink map { $_ } grep { -e $_ } @temps;
78     }
79     
80     undef $body;
81     
82     # Ensure temp files were deleted
83     for my $temp ( @temps ) {
84         ok( !-e $temp, "Temp file $temp was deleted" );
85     }
86
87