Make sure we do not include a directory separator in the temp file suffix.
[catagits/HTTP-Body.git] / t / 07xforms.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 => 12;
10
11 use Cwd;
12 use HTTP::Body;
13 use File::Spec::Functions;
14 use IO::File;
15 use PAML;
16
17 my $path = catdir( getcwd(), 't', 'data', 'xforms' );
18
19 for ( my $i = 1; $i <= 2; $i++ ) {
20
21     my $test    = sprintf( "%.3d", $i );
22     my $headers = PAML::LoadFile( catfile( $path, "$test-headers.pml" ) );
23     my $results = PAML::LoadFile( catfile( $path, "$test-results.pml" ) );
24     my $content = IO::File->new( catfile( $path, "$test-content.dat" ) );
25     my $body    = HTTP::Body->new( $headers->{'Content-Type'}, $headers->{'Content-Length'} );
26
27     binmode $content, ':raw';
28
29     while ( $content->read( my $buffer, 1024 ) ) {
30         $body->add($buffer);
31     }
32     
33     # Save tempnames for later deletion
34     my @temps;
35     
36     for my $field ( keys %{ $body->upload } ) {
37
38         my $value = $body->upload->{$field};
39
40         for ( ( ref($value) eq 'ARRAY' ) ? @{$value} : $value ) {
41             push @temps, delete $_->{tempname};
42         }
43     }
44
45     is_deeply( $body->body, $results->{body}, "$test XForms body" );
46     is_deeply( $body->param, $results->{param}, "$test XForms param" );
47     is_deeply( $body->upload, $results->{upload}, "$test XForms upload" );
48     if ( $body->isa('HTTP::Body::XFormsMultipart') ) {
49         cmp_ok( $body->start, 'eq', $results->{start}, "$test XForms start" );
50     }
51     else {
52         ok( 1, "$test XForms start" );
53     }
54     cmp_ok( $body->state, 'eq', 'done', "$test XForms state" );
55     cmp_ok( $body->length, '==', $headers->{'Content-Length'}, "$test XForms length" );
56     
57     # Clean up temp files created
58     unlink map { $_ } grep { defined $_ && -e $_ } @temps;
59 }