Browse files

Support streaming (just removed the line that checked $res->[2] and a…

…dded a test)
  • Loading branch information...
1 parent 4327dd9 commit 8aed3cdbf446989f621867dd161135e2b7ec2f60 leo user committed May 9, 2012
Showing with 57 additions and 3 deletions.
  1. +1 −0 MANIFEST
  2. +0 −3 lib/Plack/Middleware/Deflater.pm
  3. +56 −0 t/streaming.t
View
1 MANIFEST
@@ -28,6 +28,7 @@ README
t/00_compile.t
t/content_type.t
t/deflater.t
+t/streaming.t
t/vary.t
t/xno-compress.t
xt/perlcritic.t
View
3 lib/Plack/Middleware/Deflater.pm
@@ -25,9 +25,6 @@ sub call {
$self->response_cb($res, sub {
my $res = shift;
- # do not support streaming response
- return unless defined $res->[2];
-
# can't operate on Content-Ranges
return if $env->{HTTP_CONTENT_RANGE};
View
56 t/streaming.t
@@ -0,0 +1,56 @@
+use strict;
+use warnings;
+use FindBin;
+use Test::More;
+use HTTP::Request::Common;
+use Plack::Test;
+use Plack::Builder;
+
+my $app = builder {
+ enable sub {
+ my $cb = shift;
+ sub {
+ my $env = shift;
+ $env->{HTTP_ACCEPT_ENCODING} =~ s/(gzip|deflate)//gi
+ if $env->{HTTP_USER_AGENT} =~ m!^Mozilla/4!
+ and $env->{HTTP_USER_AGENT} !~ m!\bMSIE\s(7|8)!;
+ $cb->($env);
+ }
+ };
+ enable 'Deflater', content_type => 'text/plain', vary_user_agent => 1;
+
+ # Non streaming
+ # sub { [200, [ 'Content-Type' => 'text/plain' ], [ "Hello World" ]] }
+
+ # streaming
+ sub {
+ my $env = shift;
+ return sub {
+ my $r = shift;
+ my $w = $r->([ '200', [ 'Content-Type' => 'text/plain' ]]);
+ $w->write("Hello World");
+ $w->close;
+ };
+ };
+};
+
+my @impl = qw(MockHTTP Server);
+sub flip_backend { $Plack::Test::Impl = shift @impl }
+
+test_psgi
+ app => $app,
+ client => sub {
+ my $cb = shift;
+ my $req = HTTP::Request->new( GET => "http://localhost/" );
+ $req->accept_decodable;
+ $req->user_agent(
+ "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/5.0)");
+ my $res = $cb->($req);
+ is $res->decoded_content, 'Hello World';
+ is $res->content_encoding, 'gzip';
+ like $res->header('Vary'), qr/Accept-Encoding/;
+ like $res->header('Vary'), qr/User-Agent/;
+ }
+ while flip_backend;
+
+done_testing;

0 comments on commit 8aed3cd

Please sign in to comment.