HTTP::Body 0.7, patch to support 0-length uploads
[catagits/HTTP-Body.git] / t / 06octetstream.t
1 use strict;
2 use warnings;
3
4 use Test::More tests => 8;
5
6 use Cwd;
7 use HTTP::Body;
8 use File::Spec::Functions;
9 use IO::File;
10 use YAML;
11
12 my $path = catdir( getcwd(), 't', 'data', 'octetstream' );
13
14 for ( my $i = 1 ; $i <= 2 ; $i++ ) {
15
16     my $test = sprintf( "%.3d", $i );
17     my $headers = YAML::LoadFile( catfile( $path, "$test-headers.yml" ) );
18     my $results =
19       slurp_fh( IO::File->new( catfile( $path, "$test-results.dat" ) ) );
20     my $content = IO::File->new( catfile( $path, "$test-content.dat" ) );
21     my $body = HTTP::Body->new( $headers->{'Content-Type'},
22         $headers->{'Content-Length'} );
23
24     binmode $content, ':raw';
25
26     while ( $content->read( my $buffer, 1024 ) ) {
27         $body->add($buffer);
28     }
29
30     isa_ok( $body->body, 'File::Temp', "$test OctetStream body isa" );
31     my $data = slurp_fh( $body->body );
32     is_deeply( $data, $results, "$test UrlEncoded body" );
33     cmp_ok( $body->state, 'eq', 'done', "$test UrlEncoded state" );
34     cmp_ok(
35         $body->length, '==',
36         $headers->{'Content-Length'},
37         "$test UrlEncoded length"
38     );
39 }
40
41 sub slurp_fh {
42     my ($fh) = @_;
43     my $data = '';
44     while ( $fh->read( my $buffer, 1024 ) ) {
45         $data .= $buffer;
46     }
47     $data;
48 }