Skip to content

Commit

Permalink
Support binary response
Browse files Browse the repository at this point in the history
  • Loading branch information
miyagawa committed Jul 7, 2011
1 parent 66905d3 commit c8af5e6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/Tatsumaki/Handler.pm
Expand Up @@ -18,6 +18,7 @@ has writer => (is => 'rw');
has mxhr => (is => 'rw', isa => 'Bool');
has mxhr_boundary => (is => 'rw', isa => 'Str', lazy => 1, lazy_build => 1);
has json => (is => 'rw', isa => 'JSON', lazy => 1, default => sub { JSON->new->utf8 });
has binary => (is => 'rw', isa => 'Bool');

has _write_buffer => (is => 'rw', isa => 'ArrayRef', lazy => 1, default => sub { [] });

Expand Down Expand Up @@ -172,6 +173,8 @@ sub get_chunk {
$self->response->content_type('application/json');
return $self->json->encode($_[0]);
}
} elsif ($self->binary) {
join '', @_;
} else {
join '', map Encode::encode_utf8($_), @_;
}
Expand Down
31 changes: 31 additions & 0 deletions t/handler/binary.t
@@ -0,0 +1,31 @@
package BinaryApp;
use base qw(Tatsumaki::Handler);

sub get {
my $self = shift;
my $binary = "foo\xdabar";
$self->binary(1);
$self->response->content_type('text/plain; charset=latin-1');
$self->write($binary);
}

package main;
use Plack::Test;
use Test::More;
use HTTP::Request::Common;
use Tatsumaki::Application;
$Plack::Test::Impl = "Server";

my $app = Tatsumaki::Application->new([
'/binary' => 'BinaryApp',
]);

test_psgi $app, sub {
my $cb = shift;

my $res = $cb->(GET "http://localhost/binary");
is $res->content, "foo\xdabar";
is length $res->content, 7;
};

done_testing;

0 comments on commit c8af5e6

Please sign in to comment.