1use strict;
2use Test::Requires qw(IO::Handle::Util);
3
4package MyComponent;
5use parent 'Plack::Component';
6use Plack::Util::Accessor qw( res cb );
7
8sub call { return $_[0]->response_cb( $_[0]->res, $_[0]->cb ); }
9
10package main;
11use IO::Handle::Util qw(:io_from);
12use HTTP::Request::Common;
13use Test::More;
14use Plack::Test;
15
16# Various kinds of PSGI responses.
17sub generate_responses {
18    [200, ['Content-Type' => 'text/plain'], ['Hello']],
19    [200, ['Content-Type' => 'text/plain'], io_from_array ['Hello']],
20    sub { $_[0]->([ 200, ['Content-Type' => 'text/plain'], ['Hello'] ]) },
21    sub {
22        my $writer = $_[0]->([ 200, ['Content-Type' => 'text/plain'] ]);
23        $writer->write( 'Hello' );
24        $writer->close;
25    },
26}
27
28# $body filters can return undef with no warnings.
29for my $res ( generate_responses ) {
30    my @warns;
31    local $SIG{__WARN__} = sub { push @warns, @_ };
32
33    my $app = MyComponent->new(
34        res => $res, cb => sub { sub { $_[0] } },
35    );
36    test_psgi( $app, sub { $_[0]->(GET '/') } );
37
38    is_deeply \@warns, [];
39}
40
41for my $res ( generate_responses ) {
42    my $app = MyComponent->new(
43        res => $res, cb => sub {
44            my $done;
45            sub {
46                return if $done;
47                if (defined $_[0]) {
48                    return $_[0];
49                } else {
50                    $done = 1;
51                    return 'END';
52                }
53            },
54        },
55    );
56    test_psgi( $app, sub {
57        my $res = $_[0]->(GET '/');
58        is $res->content, 'HelloEND';
59    } );
60}
61
62done_testing;
63