Added XXX-results.yml
[catagits/HTTP-Body.git] / t / 01use.t
CommitLineData
aac7ca02 1use Test::More 'no_plan';
2
3use strict;
4use FindBin;
5use_ok 'HTTP::Body';
6use YAML 'LoadFile';
7use Path::Class;
8use Data::Dumper;
9
10for my $format (qw/multipart urlencoded/) {
44761c00 11
aac7ca02 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;
44761c00 17 my $headers = LoadFile( file( $FindBin::Bin, 'data', $format, "$num-headers.yml" ) );
18 my $results = LoadFile( file( $FindBin::Bin, 'data', $format, "$num-results.yml" ) );
aac7ca02 19 my $content = $file->open('<');
44761c00 20 my $body = HTTP::Body->new( $headers->{'Content-Type'}, $headers->{'Content-Length'} );
21
22 binmode $content, ':raw';
aac7ca02 23
24 while ( $content->read( my $buffer, 1024 ) ) {
25 $body->add($buffer);
26 }
44761c00 27
28 if ( $ENV{HTTP_BODY_DEBUG} ) {
aac7ca02 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 }
44761c00 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 ok( $body->state eq 'done', "$num-$format state" );
aac7ca02 53 }
54}
55
561;