HTTP::Body, patch from Daniel Ruoso to add XForms support
[catagits/HTTP-Body.git] / t / 07xforms.t
CommitLineData
5940e4c7 1#!perl
2
3use strict;
4use warnings;
5
6use Test::More tests => 12;
7
8use Cwd;
9use HTTP::Body;
10use File::Spec::Functions;
11use IO::File;
12use YAML;
13
14my $path = catdir( getcwd(), 't', 'data', 'xforms' );
15
16for ( my $i = 1; $i <= 2; $i++ ) {
17
18 my $test = sprintf( "%.3d", $i );
19 my $headers = YAML::LoadFile( catfile( $path, "$test-headers.yml" ) );
20 my $results = YAML::LoadFile( catfile( $path, "$test-results.yml" ) );
21 my $content = IO::File->new( catfile( $path, "$test-content.dat" ) );
22 my $body = HTTP::Body->new( $headers->{'Content-Type'}, $headers->{'Content-Length'} );
23
24 binmode $content, ':raw';
25
26 while ( $content->read( my $buffer, 1024 ) ) {
27 $body->add($buffer);
28 }
29
30 # Save tempnames for later deletion
31 my @temps;
32
33 for my $field ( keys %{ $body->upload } ) {
34
35 my $value = $body->upload->{$field};
36
37 for ( ( ref($value) eq 'ARRAY' ) ? @{$value} : $value ) {
38 push @temps, delete $_->{tempname};
39 }
40 }
41
42 is_deeply( $body->body, $results->{body}, "$test XForms body" );
43 is_deeply( $body->param, $results->{param}, "$test XForms param" );
44 is_deeply( $body->upload, $results->{upload}, "$test XForms upload" );
45 if ( $body->isa('HTTP::Body::XFormsMultipart') ) {
46 cmp_ok( $body->start, 'eq', $results->{start}, "$test XForms start" );
47 }
48 else {
49 ok( 1, "$test XForms start" );
50 }
51 cmp_ok( $body->state, 'eq', 'done', "$test XForms state" );
52 cmp_ok( $body->length, '==', $headers->{'Content-Length'}, "$test XForms length" );
53
54 # Clean up temp files created
55 unlink map { $_ } grep { defined $_ && -e $_ } @temps;
56}