example short circuit prepare phase
John Napiorkowski [Wed, 6 Jul 2016 14:08:05 +0000 (09:08 -0500)]
t/unicode-exception-bug.t [new file with mode: 0644]

diff --git a/t/unicode-exception-bug.t b/t/unicode-exception-bug.t
new file mode 100644 (file)
index 0000000..cdcbd30
--- /dev/null
@@ -0,0 +1,74 @@
+use strict;
+use warnings;
+use Test::More;
+
+BEGIN {
+  package TestApp::Exception;
+  $INC{'TestApp/Exception.pm'} = __FILE__;
+
+  sub new {
+    my ($class, $code, $headers, $body) = @_;
+    return bless +{res => [$code, $headers, $body]}, $class;
+  }
+
+  sub throw { die shift->new(@_) }
+
+  sub as_psgi {
+    my ($self, $env) = @_;
+    my ($code, $headers, $body) = @{$self->{res}};
+
+    return [$code, $headers, $body]; # for now
+
+    return sub {
+      my $responder = shift;
+      $responder->([$code, $headers, $body]);
+    };
+  }
+
+  package TestApp::Controller::Root;
+  $INC{'TestApp/Controller/Root.pm'} = __FILE__;
+
+  use Moose;
+  use MooseX::MethodAttributes;
+  extends 'Catalyst::Controller';
+
+  sub main :Path('') :Args(1) {
+    my ($self, $c, $arg) = @_;
+    $c->res->body('<h1>OK</h1>');
+    $c->res->content_type('text/html');
+  }
+
+  TestApp::Controller::Root->config(namespace => '');
+}
+{
+  package TestApp;
+  $INC{'TestApp.pm'} = __FILE__;
+  use Catalyst;
+  use TestApp::Exception;
+
+  sub handle_unicode_encoding_exception {
+    my ( $self, $param_value, $error_msg ) = @_;
+    TestApp::Exception->throw(
+      200, ['content-type'=>'text/plain'], ['Bad unicode data']);
+  }
+
+  __PACKAGE__->setup;
+}
+use Catalyst::Test 'TestApp';
+
+{
+  my $res = request('/ok');
+  is ($res->status_line, "200 OK");
+  is ($res->content, '<h1>OK</h1>');
+}
+{
+  my $res = request('/%E2%C3%83%C6%92%C3%8');
+  is ($res->content, 'Bad unicode data');
+}
+
+done_testing;