X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F04multipart.t;h=074ca1068f8e9b2bf5e2687530d15c5444cea81c;hb=HEAD;hp=1783429b7d7b5655c2f35b597d9c0bcd4f6c9c16;hpb=1ced50e0624bcb0e1c37780cac004841f5804c52;p=catagits%2FHTTP-Body.git diff --git a/t/04multipart.t b/t/04multipart.t index 1783429..074ca10 100644 --- a/t/04multipart.t +++ b/t/04multipart.t @@ -3,23 +3,32 @@ use strict; use warnings; -use Test::More tests => 60; +use FindBin; +use lib "$FindBin::Bin/lib"; + +use Test::More tests => 153; +use Test::Deep; use Cwd; use HTTP::Body; use File::Spec::Functions; use IO::File; -use YAML; +use PAML; +use File::Temp qw/ tempdir /; my $path = catdir( getcwd(), 't', 'data', 'multipart' ); -for ( my $i = 1; $i <= 12; $i++ ) { +for ( my $i = 1; $i <= 13; $i++ ) { my $test = sprintf( "%.3d", $i ); - my $headers = YAML::LoadFile( catfile( $path, "$test-headers.yml" ) ); - my $results = YAML::LoadFile( catfile( $path, "$test-results.yml" ) ); + my $headers = PAML::LoadFile( catfile( $path, "$test-headers.pml" ) ); + my $results = PAML::LoadFile( catfile( $path, "$test-results.pml" ) ); my $content = IO::File->new( catfile( $path, "$test-content.dat" ) ); my $body = HTTP::Body->new( $headers->{'Content-Type'}, $headers->{'Content-Length'} ); + my $tempdir = tempdir( 'XXXXXXX', CLEANUP => 1, DIR => File::Spec->tmpdir() ); + $body->tmpdir($tempdir); + + my $regex_tempdir = quotemeta($tempdir); binmode $content, ':raw'; @@ -27,6 +36,11 @@ for ( my $i = 1; $i <= 12; $i++ ) { $body->add($buffer); } + # Tests >= 10 use auto-cleanup + if ( $i >= 10 ) { + $body->cleanup(1); + } + # Save tempnames for later deletion my @temps; @@ -35,16 +49,39 @@ for ( my $i = 1; $i <= 12; $i++ ) { my $value = $body->upload->{$field}; for ( ( ref($value) eq 'ARRAY' ) ? @{$value} : $value ) { - push @temps, delete $_->{tempname}; + like($_->{tempname}, qr{$regex_tempdir}, "has tmpdir $tempdir"); + push @temps, $_->{tempname}; + } + + # Tell Test::Deep to ignore tempname values + if ( ref $value eq 'ARRAY' ) { + for ( @{ $results->{upload}->{$field} } ) { + $_->{tempname} = ignore(); + } + } + else { + $results->{upload}->{$field}->{tempname} = ignore(); } } - - is_deeply( $body->body, $results->{body}, "$test MultiPart body" ); - is_deeply( $body->param, $results->{param}, "$test MultiPart param" ); - is_deeply( $body->upload, $results->{upload}, "$test MultiPart upload" ); + + cmp_deeply( $body->body, $results->{body}, "$test MultiPart body" ); + cmp_deeply( $body->param, $results->{param}, "$test MultiPart param" ); + cmp_deeply( $body->param_order, $results->{param_order} ? $results->{param_order} : [], "$test MultiPart param_order" ); + cmp_deeply( $body->upload, $results->{upload}, "$test MultiPart upload" ) + if $results->{upload}; cmp_ok( $body->state, 'eq', 'done', "$test MultiPart state" ); cmp_ok( $body->length, '==', $body->content_length, "$test MultiPart length" ); - # Clean up temp files created - unlink map { $_ } grep { -e $_ } @temps; -} + if ( $i < 10 ) { + # Clean up temp files created + unlink map { $_ } grep { -e $_ } @temps; + } + + undef $body; + + # Ensure temp files were deleted + for my $temp ( @temps ) { + ok( !-e $temp, "Temp file $temp was deleted" ); + } + +}