Skip to content

Commit

Permalink
Avoid warnings in filters callback when undef is pushed to the conten…
Browse files Browse the repository at this point in the history
…t body array. Fixes plackgh-92
  • Loading branch information
miyagawa committed Mar 29, 2010
1 parent c1d13a5 commit 5167a32
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/HTTP/Message/PSGI.pm
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ sub _res_from_psgi {
$res->headers->header(@$headers) if @$headers;

if (ref $body eq 'ARRAY') {
$res->content(join '', @$body);
$res->content(join '', grep defined, @$body);
} else {
local $/ = \4096;
my $content;
Expand Down
3 changes: 2 additions & 1 deletion lib/Plack/Component.pm
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ sub response_cb {
$line = $filter_cb->($line);
}
# Send EOF.
push @{ $res->[2] }, $filter_cb->( undef );
my $eof = $filter_cb->( undef );
push @{ $res->[2] }, $eof if defined $eof;
} else {
my $body = $res->[2];
my $getline = sub { $body->getline };
Expand Down
41 changes: 41 additions & 0 deletions t/Plack-Middleware/component.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use strict;
use Test::Requires qw(IO::Handle::Util);

package MyComponent;
use parent 'Plack::Component';
use Plack::Util::Accessor qw( res cb );

sub call { return $_[0]->response_cb( $_[0]->res, $_[0]->cb ); }

package main;
use IO::Handle::Util qw(:io_from);
use HTTP::Request::Common;
use Test::More;
use Plack::Test;

# Various kinds of PSGI responses.
my @res = (
[200, ['Content-Type' => 'text/plain'], ['Hello']],
[200, ['Content-Type' => 'text/plain'], io_from_array ['Hello']],
sub { $_[0]->([ 200, ['Content-Type' => 'text/plain'], ['Hello'] ]) },
sub {
my $writer = $_[0]->([ 200, ['Content-Type' => 'text/plain'] ]);
$writer->write( 'Hello' );
$writer->close;
},
);

# $body filters can return undef with no warnings.
for my $res (@res) {
my @warns;
local $SIG{__WARN__} = sub { push @warns, @_ };

my $app = MyComponent->new(
res => $res, cb => sub { sub { $_[0] } },
);
test_psgi( $app, sub { $_[0]->(GET '/') } );

is_deeply \@warns, [];
}

done_testing;

0 comments on commit 5167a32

Please sign in to comment.