Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Support binary response

  • Loading branch information...
commit c8af5e60284cc932936e24a7bbb7f2b4d36b3752 1 parent 66905d3
@miyagawa authored
Showing with 34 additions and 0 deletions.
  1. +3 −0  lib/Tatsumaki/Handler.pm
  2. +31 −0 t/handler/binary.t
View
3  lib/Tatsumaki/Handler.pm
@@ -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 { [] });
@@ -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($_), @_;
}
View
31 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;
Please sign in to comment.
Something went wrong with that request. Please try again.