Skip to content

Commit

Permalink
Merge pull request plack#7 from ranguard/master
Browse files Browse the repository at this point in the history
Support streaming in Deflater
  • Loading branch information
kazeburo committed May 16, 2012
2 parents 4327dd9 + 8aed3cd commit 47a12c2
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 3 deletions.
1 change: 1 addition & 0 deletions MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 0 additions & 3 deletions lib/Plack/Middleware/Deflater.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down
56 changes: 56 additions & 0 deletions t/streaming.t
Original file line number Diff line number Diff line change
@@ -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 47a12c2

Please sign in to comment.