Browse files

Merge pull request #276 from wreis/head_streaming

Add support for streaming app to P::M::Head
  • Loading branch information...
2 parents 6a0233f + 591b12d commit abf55db8172f44917a9f9b96970187aeb83627c8 @miyagawa miyagawa committed May 16, 2012
Showing with 45 additions and 1 deletion.
  1. +13 −1 lib/Plack/Middleware/Head.pm
  2. +32 −0 t/Plack-Middleware/head_streaming.t
View
14 lib/Plack/Middleware/Head.pm
@@ -11,7 +11,19 @@ sub call {
$self->response_cb($self->app->($env), sub {
my $res = shift;
- $res->[2] = [];
+ if ( $res->[2] ) {
+ $res->[2] = [];
+ }
+ else {
+ my $done;
+ return sub {
+ unless ($done) {
+ return q{};
+ }
+ $done = 1;
+ return defined $_[0] ? q{} : undef;
+ };
+ }
});
}
View
32 t/Plack-Middleware/head_streaming.t
@@ -0,0 +1,32 @@
+use strict;
+use Test::More;
+use Plack::Test;
+use Plack::Builder;
+use HTTP::Request::Common;
+
+my $app = sub {
+ my $env = shift;
+ return sub {
+ my $writer = shift->( [ 200, [
+ 'Content-Type' => 'text/plain',
+ ] ] );
+ $writer->write($_) for qw{Hello World};
+ $writer->close;
+ };
+};
+
+$app = builder { enable "Head"; $app };
+
+test_psgi $app, sub {
+ my $cb = shift;
+
+ my $res = $cb->(GET "/");
+ is $res->content, "HelloWorld";
+
+ $res = $cb->(HEAD "/");
+ ok !$res->content;
+ ok(!$res->content_length);
+};
+
+done_testing;
+

0 comments on commit abf55db

Please sign in to comment.