Test Content-Length
[catagits/HTTP-Body.git] / t / 01use.t
1 use Test::More 'no_plan';
2
3 use strict;
4 use FindBin;
5 use_ok 'HTTP::Body';
6 use YAML 'LoadFile';
7 use Path::Class;
8 use Data::Dumper;
9
10 for my $format (qw/multipart urlencoded/) {
11
12     for my $match ( glob file( $FindBin::Bin, 'data', $format, '*.dat' ) ) {
13         my $file = file($match);
14         my $name = $file->basename;
15         $name =~ /^(\d+)-.*/;
16         my $num     = $1;
17         my $headers = LoadFile( file( $FindBin::Bin, 'data', $format, "$num-headers.yml" ) );
18         my $results = LoadFile( file( $FindBin::Bin, 'data', $format, "$num-results.yml" ) );
19         my $content = $file->open('<');
20         my $body    = HTTP::Body->new( $headers->{'Content-Type'}, $headers->{'Content-Length'} );
21
22         binmode $content, ':raw';
23
24         while ( $content->read( my $buffer, 1024 ) ) {
25             $body->add($buffer);
26         }
27
28         if ( $ENV{HTTP_BODY_DEBUG} ) {
29             warn Dumper( $body->param );
30             warn Dumper( $body->upload );
31             warn Dumper( $body->body );
32
33             warn "state          : " . $body->state;
34             warn "length         : " . $body->length;
35             warn "content length : " . $body->content_length;
36             warn "body length    : " . ( $body->body->stat )[7] if $body->body;
37             warn "buffer         : " . $body->buffer if $body->buffer;
38         }
39         
40         for my $field ( keys %{ $body->upload } ) {
41
42             my $value = $body->upload->{$field};
43
44             for ( ( ref($value) eq 'ARRAY' ) ? @{$value} : $value ) {
45                 delete $_->{tempname};
46             }
47         }
48
49         is_deeply( $body->body, $results->{body}, "$num-$format body" );
50         is_deeply( $body->param, $results->{param}, "$num-$format param" );
51         is_deeply( $body->upload, $results->{upload}, "$num-$format upload" );
52         cmp_ok( $body->state, 'eq', 'done', "$num-$format state" );
53         cmp_ok( $body->length, '==', $headers->{'Content-Length'}, "$num-$format length" );
54     }
55 }
56
57 1;