Add version 1.11 of HTTP::Body with new param_order functionality
[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 => 14;
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->param_order, $results->{param_order} ? $results->{param_order} : [], "$test XForms param_order" );
48     is_deeply( $body->upload, $results->{upload}, "$test XForms upload" );
49     if ( $body->isa('HTTP::Body::XFormsMultipart') ) {
50         cmp_ok( $body->start, 'eq', $results->{start}, "$test XForms start" );
51     }
52     else {
53         ok( 1, "$test XForms start" );
54     }
55     cmp_ok( $body->state, 'eq', 'done', "$test XForms state" );
56     cmp_ok( $body->length, '==', $headers->{'Content-Length'}, "$test XForms length" );
57     
58     # Clean up temp files created
59     unlink map { $_ } grep { defined $_ && -e $_ } @temps;
60 }