From: John Napiorkowski Date: Wed, 7 Jan 2015 19:37:39 +0000 (-0600) Subject: tests and donot let you change encoding too late into the response X-Git-Tag: 5.90079_008~7 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Runtime.git;a=commitdiff_plain;h=70005e986f12f433f81595e35c902358487b7869 tests and donot let you change encoding too late into the response --- diff --git a/lib/Catalyst.pm b/lib/Catalyst.pm index a255543..bc54ac7 100644 --- a/lib/Catalyst.pm +++ b/lib/Catalyst.pm @@ -1049,7 +1049,11 @@ Clears the encoding for the current context =head2 encoding -Sets or gets the application encoding. +Sets or gets the application encoding. Setting encoding takes either an +Encoding object or a string that we try to resolve via L. + +You would expect to get the encoding object back if you attempt to set it. If +there is a failure you will get undef returned and an error message in the log. =cut @@ -1060,7 +1064,7 @@ sub clear_encoding { if(blessed $c) { $c->encoding(undef); } else { - $c->debug->error("You can't clear encoding on the application"); + $c->log->error("You can't clear encoding on the application"); } } @@ -1069,6 +1073,13 @@ sub encoding { my $encoding; if ( scalar @_ ) { + + # Don't let one change this once we are too far into the response + if(blessed $c && $c->res->finalized_headers) { + Carp::croak("You may not change the encoding once the headers are finalized"); + return; + } + # Let it be set to undef if (my $wanted = shift) { $encoding = Encode::find_encoding($wanted) diff --git a/t/utf_incoming.t b/t/utf_incoming.t index 373a428..638cef5 100644 --- a/t/utf_incoming.t +++ b/t/utf_incoming.t @@ -167,10 +167,19 @@ use JSON::MaybeXS; sub override_encoding :Local { my ($self, $c) = @_; $c->res->content_type('text/plain'); + $c->encoding(Encode::find_encoding('UTF-8')); $c->encoding(Encode::find_encoding('Shift_JIS')); $c->response->body("テスト"); } + sub stream_write_error :Local { + my ($self, $c) = @_; + $c->response->content_type('text/html'); + $c->response->write("

This is stream_write action ♥

"); + $c->encoding(Encode::find_encoding('Shift_JIS')); + $c->response->write("

This is stream_write action ♥

"); + } + package MyApp; use Catalyst; @@ -395,6 +404,14 @@ SKIP: { is $res->content_charset, 'UTF-8'; } +{ + my $res = request "/root/stream_write_error"; + + is $res->code, 200, 'OK'; + like decode_utf8($res->content), qr[

This is stream_write action ♥