HTTP::Body, added support for chunked requests
[catagits/HTTP-Body.git] / t / 05urlencoded.t
CommitLineData
a9df1200 1#!perl
2
3use strict;
4use warnings;
5
0a66fd23 6use Test::More tests => 16;
a9df1200 7
8use Cwd;
0a66fd23 9use Digest::MD5 qw(md5_hex);
a9df1200 10use HTTP::Body;
11use File::Spec::Functions;
12use IO::File;
13use YAML;
14
15my $path = catdir( getcwd(), 't', 'data', 'urlencoded' );
16
0a66fd23 17for ( my $i = 1; $i <= 3; $i++ ) {
a9df1200 18
19 my $test = sprintf( "%.3d", $i );
20 my $headers = YAML::LoadFile( catfile( $path, "$test-headers.yml" ) );
21 my $results = YAML::LoadFile( catfile( $path, "$test-results.yml" ) );
22 my $content = IO::File->new( catfile( $path, "$test-content.dat" ) );
23 my $body = HTTP::Body->new( $headers->{'Content-Type'}, $headers->{'Content-Length'} );
24
25 binmode $content, ':raw';
26
27 while ( $content->read( my $buffer, 1024 ) ) {
28 $body->add($buffer);
29 }
30
31 is_deeply( $body->body, $results->{body}, "$test UrlEncoded body" );
32 is_deeply( $body->param, $results->{param}, "$test UrlEncoded param" );
33 is_deeply( $body->upload, $results->{upload}, "$test UrlEncoded upload" );
34 cmp_ok( $body->state, 'eq', 'done', "$test UrlEncoded state" );
0a66fd23 35 cmp_ok( $body->length, '==', $body->content_length, "$test UrlEncoded length" );
36
37 # Check trailing header on the chunked request
38 if ( $i == 3 ) {
39 my $content = IO::File->new( catfile( $path, "002-content.dat" ) );
40 $content->read( my $buf, 4096 );
41 is( $body->trailing_headers->header('Content-MD5'), md5_hex($buf), "$test trailing header ok" );
42 }
a9df1200 43}