1#!perl
2
3use strict;
4use warnings;
5
6use Test::More qw[no_plan];
7use t::Util    qw[tmpfile rewind $CRLF];
8use HTTP::Tiny;
9
10{
11    no warnings 'redefine';
12    sub HTTP::Tiny::Handle::can_read  { 1 };
13    sub HTTP::Tiny::Handle::can_write { 1 };
14}
15
16{
17    my $body    = join($CRLF, map { sprintf('%x', length $_) . $CRLF . $_ } 'A'..'Z', '') . $CRLF;
18    my $fh      = tmpfile($body);
19    my $handle  = HTTP::Tiny::Handle->new(fh => $fh);
20    my $exp     = ['A'..'Z'];
21    my $got     = [];
22    my $cb      = sub { push @$got, $_[0] };
23    my $response = { headers => {} };
24    $handle->read_chunked_body($cb, $response);
25    is_deeply($response->{headers}, {}, 'chunked trailers');
26    is_deeply($got, $exp, "chunked chunks");
27}
28
29{
30    my $fh      = tmpfile();
31    my $handle  = HTTP::Tiny::Handle->new(fh => $fh);
32
33    my $exp      = ['A'..'Z'];
34    my $trailers = { foo => 'Bar', bar => 'Baz' };
35    my $got      = [];
36
37    {
38        my @chunks = @$exp;
39        my $request = {
40          cb => sub { shift @chunks },
41          trailer_cb => sub { $trailers },
42        };
43        $handle->write_chunked_body($request);
44    }
45
46    rewind($fh);
47
48    {
49        my $cb = sub { push @$got, $_[0] };
50        my $response = { headers => {} };
51        $handle->read_chunked_body($cb, $response);
52        is_deeply($response->{headers}, $trailers, 'roundtrip chunked trailers');
53    }
54
55    is_deeply($got, $exp, "roundtrip chunked chunks");
56}
57
58
59