Minimal pod, and removed req on YAML - tests will skip all if not found
[catagits/HTTP-Body.git] / t / 06octetstream.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5
6 eval { require YAML; import YAML 'LoadFile'; };
7 if ($@) {
8   eval { require YAML::Syck; import YAML::Syck 'LoadFile'; }
9 }
10
11 plan skip_all => 'Tests need YAML or YAML::Syck' if $@;
12
13 plan tests => 8;
14
15 use Cwd;
16 use HTTP::Body;
17 use File::Spec::Functions;
18 use IO::File;
19
20 my $path = catdir( getcwd(), 't', 'data', 'octetstream' );
21
22 for ( my $i = 1 ; $i <= 2 ; $i++ ) {
23
24     my $test = sprintf( "%.3d", $i );
25     my $headers = YAML::LoadFile( catfile( $path, "$test-headers.yml" ) );
26     my $results =
27       slurp_fh( IO::File->new( catfile( $path, "$test-results.dat" ) ) );
28     my $content = IO::File->new( catfile( $path, "$test-content.dat" ) );
29     my $body = HTTP::Body->new( $headers->{'Content-Type'},
30         $headers->{'Content-Length'} );
31
32     binmode $content, ':raw';
33
34     while ( $content->read( my $buffer, 1024 ) ) {
35         $body->add($buffer);
36     }
37
38     isa_ok( $body->body, 'File::Temp', "$test OctetStream body isa" );
39     my $data = slurp_fh( $body->body );
40     is_deeply( $data, $results, "$test UrlEncoded body" );
41     cmp_ok( $body->state, 'eq', 'done', "$test UrlEncoded state" );
42     cmp_ok(
43         $body->length, '==',
44         $headers->{'Content-Length'},
45         "$test UrlEncoded length"
46     );
47 }
48
49 sub slurp_fh {
50     my ($fh) = @_;
51     my $data = '';
52     while ( $fh->read( my $buffer, 1024 ) ) {
53         $data .= $buffer;
54     }
55     $data;
56 }