4 use HTTP::Request::Common;
5 use HTTP::Message::PSGI;
9 # Test to make sure we let HTTP style exceptions bubble up to the middleware
10 # rather than catching them outselves.
13 package MyApp::Exception;
16 my ($class, $code, $headers, $body) = @_;
17 return bless +{res => [$code, $headers, $body]}, $class;
20 sub throw { die shift->new(@_) }
23 my ($self, $env) = @_;
24 my ($code, $headers, $body) = @{$self->{res}};
26 return [$code, $headers, $body]; # for now
29 my $responder = shift;
30 $responder->([$code, $headers, $body]);
34 package MyApp::AnotherException;
36 sub new { bless +{}, shift }
40 sub as_string { 'bad stringy bad' }
42 package MyApp::Controller::Root;
44 use base 'Catalyst::Controller';
48 die MyApp::Exception->new(
49 404, ['content-type'=>'text/plain'], ['Not Found']);
52 sub from_psgi_app :Local {
54 $c->res->from_psgi_response(
59 sub from_catalyst :Local {
61 MyApp::Exception->throw(
62 403, ['content-type'=>'text/plain'], ['Forbidden']);
65 sub from_code_type :Local {
66 my $e = MyApp::AnotherException->new;
70 sub classic_error :Local {
72 Catalyst::Exception->throw("Ex Parrot");
77 die "I'm not dead yet";
80 sub end :Private { die "We should never hit end for HTTPExceptions" }
85 MyApp->config(abort_chain_on_error_fix=>1);
89 MyApp->setup_log('fatal');
92 $INC{'MyApp/Controller/Root.pm'} = __FILE__; # sorry...
93 MyApp->setup_log('error');
95 Test::More::ok(MyApp->setup);
97 ok my $psgi = MyApp->psgi_app;
99 test_psgi $psgi, sub {
101 my $res = $cb->(GET "/root/from_psgi_app");
103 is $res->content, 'Not Found', 'NOT FOUND';
104 unlike $res->content, qr'HTTPExceptions', 'HTTPExceptions';
107 test_psgi $psgi, sub {
109 my $res = $cb->(GET "/root/from_catalyst");
111 is $res->content, 'Forbidden', 'Forbidden';
112 unlike $res->content, qr'HTTPExceptions', 'HTTPExceptions';
115 test_psgi $psgi, sub {
117 my $res = $cb->(GET "/root/from_code_type");
119 is $res->content, 'bad stringy bad', 'bad stringy bad';
120 unlike $res->content, qr'HTTPExceptions', 'HTTPExceptions';
123 test_psgi $psgi, sub {
125 my $res = $cb->(GET "/root/classic_error");
127 like $res->content, qr'Ex Parrot', 'Ex Parrot';
128 like $res->content, qr'HTTPExceptions', 'HTTPExceptions';
131 test_psgi $psgi, sub {
133 my $res = $cb->(GET "/root/just_die");
135 like $res->content, qr'not dead yet', 'not dead yet';
136 like $res->content, qr'HTTPExceptions', 'HTTPExceptions';
141 # We need to specify the number of expected tests because tests that live
142 # in the callbacks might never get run (thus all ran tests pass but not all
143 # required tests run).