HTTP::Body, added support for chunked requests
[catagits/HTTP-Body.git] / t / 04multipart.t
CommitLineData
a9df1200 1#!perl
2
3use strict;
4use warnings;
5
0a66fd23 6use Test::More tests => 60;
a9df1200 7
8use Cwd;
9use HTTP::Body;
10use File::Spec::Functions;
11use IO::File;
12use YAML;
13
14my $path = catdir( getcwd(), 't', 'data', 'multipart' );
15
0a66fd23 16for ( my $i = 1; $i <= 12; $i++ ) {
a9df1200 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 for my $field ( keys %{ $body->upload } ) {
31
32 my $value = $body->upload->{$field};
33
34 for ( ( ref($value) eq 'ARRAY' ) ? @{$value} : $value ) {
35 delete $_->{tempname};
36 }
37 }
38
39 is_deeply( $body->body, $results->{body}, "$test MultiPart body" );
40 is_deeply( $body->param, $results->{param}, "$test MultiPart param" );
41 is_deeply( $body->upload, $results->{upload}, "$test MultiPart upload" );
42 cmp_ok( $body->state, 'eq', 'done', "$test MultiPart state" );
0a66fd23 43 cmp_ok( $body->length, '==', $body->content_length, "$test MultiPart length" );
a9df1200 44}