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 # Use the overloading thet HTTP::Exception uses
17 bool => sub { 1 }, '""' => 'as_string', fallback => 1;
20 my ($class, $code, $headers, $body) = @_;
21 return bless +{res => [$code, $headers, $body]}, $class;
24 sub throw { die shift->new(@_) }
27 my ($self, $env) = @_;
28 my ($code, $headers, $body) = @{$self->{res}};
30 return [$code, $headers, $body]; # for now
33 my $responder = shift;
34 $responder->([$code, $headers, $body]);
38 sub as_string { 'bad stringy bad' }
40 package MyApp::Controller::Root;
42 use base 'Catalyst::Controller';
46 die MyApp::Exception->new(
47 404, ['content-type'=>'text/plain'], ['Not Found']);
50 sub from_psgi_app :Local {
52 $c->res->from_psgi_response(
57 sub from_catalyst :Local {
59 MyApp::Exception->throw(
60 403, ['content-type'=>'text/plain'], ['Forbidden']);
63 sub classic_error :Local {
65 Catalyst::Exception->throw("Ex Parrot");
70 die "I'm not dead yet";
73 sub end :Private { die "We should never hit end for HTTPExceptions" }
78 MyApp->config(abort_chain_on_error_fix=>1);
82 MyApp->setup_log('fatal');
85 $INC{'MyApp/Controller/Root.pm'} = __FILE__; # sorry...
86 MyApp->setup_log('error');
88 Test::More::ok(MyApp->setup);
90 ok my $psgi = MyApp->psgi_app;
92 test_psgi $psgi, sub {
94 my $res = $cb->(GET "/root/from_psgi_app");
96 is $res->content, 'Not Found', 'NOT FOUND';
97 unlike $res->content, qr'HTTPExceptions', 'HTTPExceptions';
100 test_psgi $psgi, sub {
102 my $res = $cb->(GET "/root/from_catalyst");
104 is $res->content, 'Forbidden', 'Forbidden';
105 unlike $res->content, qr'HTTPExceptions', 'HTTPExceptions';
108 test_psgi $psgi, sub {
110 my $res = $cb->(GET "/root/classic_error");
112 like $res->content, qr'Ex Parrot', 'Ex Parrot';
113 like $res->content, qr'HTTPExceptions', 'HTTPExceptions';
116 test_psgi $psgi, sub {
118 my $res = $cb->(GET "/root/just_die");
120 like $res->content, qr'not dead yet', 'not dead yet';
121 like $res->content, qr'HTTPExceptions', 'HTTPExceptions';
126 # We need to specify the number of expected tests because tests that live
127 # in the callbacks might never get run (thus all ran tests pass but not all
128 # required tests run).