HTTP::Body 1.04, patch from jgoulah for tmpdir() accessor
[catagits/HTTP-Body.git] / t / 04multipart.t
CommitLineData
a9df1200 1#!perl
2
3use strict;
4use warnings;
5
3debb7c0 6use Test::More tests => 98;
a9df1200 7
8use Cwd;
9use HTTP::Body;
10use File::Spec::Functions;
11use IO::File;
12use YAML;
3debb7c0 13use File::Temp qw/ tempdir /;
a9df1200 14
15my $path = catdir( getcwd(), 't', 'data', 'multipart' );
16
0a66fd23 17for ( my $i = 1; $i <= 12; $i++ ) {
a9df1200 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'} );
3debb7c0 24 my $tempdir = tempdir( 'XXXXXXX', CLEANUP => 1, DIR => File::Spec->tmpdir() );
25 $body->tmpdir($tempdir);
26
27 my $regex_tempdir = quotemeta($tempdir);
a9df1200 28
29 binmode $content, ':raw';
30
31 while ( $content->read( my $buffer, 1024 ) ) {
32 $body->add($buffer);
33 }
34
1ced50e0 35 # Save tempnames for later deletion
36 my @temps;
37
a9df1200 38 for my $field ( keys %{ $body->upload } ) {
39
40 my $value = $body->upload->{$field};
41
42 for ( ( ref($value) eq 'ARRAY' ) ? @{$value} : $value ) {
3debb7c0 43 like($_->{tempname}, qr{$regex_tempdir}, "has tmpdir $tempdir");
1ced50e0 44 push @temps, delete $_->{tempname};
a9df1200 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" );
0a66fd23 52 cmp_ok( $body->length, '==', $body->content_length, "$test MultiPart length" );
1ced50e0 53
54 # Clean up temp files created
55 unlink map { $_ } grep { -e $_ } @temps;
a9df1200 56}