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