File: 06octetstream.t

package info (click to toggle)
libhttp-body-perl 0.6-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 272 kB
  • ctags: 36
  • sloc: perl: 387; makefile: 45
file content (48 lines) | stat: -rw-r--r-- 1,233 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
use strict;
use warnings;

use Test::More tests => 8;

use Cwd;
use HTTP::Body;
use File::Spec::Functions;
use IO::File;
use YAML;

my $path = catdir( getcwd(), 't', 'data', 'octetstream' );

for ( my $i = 1 ; $i <= 2 ; $i++ ) {

    my $test = sprintf( "%.3d", $i );
    my $headers = YAML::LoadFile( catfile( $path, "$test-headers.yml" ) );
    my $results =
      slurp_fh( IO::File->new( catfile( $path, "$test-results.dat" ) ) );
    my $content = IO::File->new( catfile( $path, "$test-content.dat" ) );
    my $body = HTTP::Body->new( $headers->{'Content-Type'},
        $headers->{'Content-Length'} );

    binmode $content, ':raw';

    while ( $content->read( my $buffer, 1024 ) ) {
        $body->add($buffer);
    }

    isa_ok( $body->body, 'File::Temp', "$test OctetStream body isa" );
    my $data = slurp_fh( $body->body );
    is_deeply( $data, $results, "$test UrlEncoded body" );
    cmp_ok( $body->state, 'eq', 'done', "$test UrlEncoded state" );
    cmp_ok(
        $body->length, '==',
        $headers->{'Content-Length'},
        "$test UrlEncoded length"
    );
}

sub slurp_fh {
    my ($fh) = @_;
    my $data = '';
    while ( $fh->read( my $buffer, 1024 ) ) {
        $data .= $buffer;
    }
    $data;
}