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";
78 MyApp->setup_log('fatal');
81 $INC{'MyApp/Controller/Root.pm'} = '1'; # sorry...
82 MyApp->setup_log('error');
84 Test::More::ok(MyApp->setup);
86 ok my $psgi = MyApp->psgi_app;
88 test_psgi $psgi, sub {
90 my $res = $cb->(GET "/root/from_psgi_app");
92 is $res->content, 'Not Found', 'NOT FOUND';
95 test_psgi $psgi, sub {
97 my $res = $cb->(GET "/root/from_catalyst");
99 is $res->content, 'Forbidden', 'Forbidden';
102 test_psgi $psgi, sub {
104 my $res = $cb->(GET "/root/classic_error");
106 like $res->content, qr'Ex Parrot', 'Ex Parrot';
109 test_psgi $psgi, sub {
111 my $res = $cb->(GET "/root/just_die");
113 like $res->content, qr'not dead yet', 'not dead yet';
118 # We need to specify the number of expected tests because tests that live
119 # in the callbacks might never get run (thus all ran tests pass but not all
120 # required tests run).