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