460769d2a296fe5037ec8155d64b23fa25e43810
[catagits/Catalyst-Runtime.git] / t / http_exceptions.t
1 use warnings;
2 use strict;
3 use Test::More;
4 use HTTP::Request::Common;
5 use HTTP::Message::PSGI;
6 use Plack::Util;
7 use Plack::Test;
8
9 # Test to make sure we let HTTP style exceptions bubble up to the middleware
10 # rather than catching them outselves.
11
12 {
13   package MyApp::Exception;
14
15   use overload
16     # Use the overloading thet HTTP::Exception uses
17     bool => sub { 1 }, '""' => 'as_string', fallback => 1;
18
19   sub new {
20     my ($class, $code, $headers, $body) = @_;
21     return bless +{res => [$code, $headers, $body]}, $class;
22   }
23
24   sub throw { die shift->new(@_) }
25
26   sub as_psgi {
27     my ($self, $env) = @_;
28     my ($code, $headers, $body) = @{$self->{res}};
29
30     return [$code, $headers, $body]; # for now
31
32     return sub {
33       my $responder = shift;
34       $responder->([$code, $headers, $body]);
35     };
36   }
37
38   sub as_string { 'bad stringy bad' }
39   
40   package MyApp::Controller::Root;
41
42   use base 'Catalyst::Controller';
43
44   my $psgi_app = sub {
45     my $env = shift;
46     die MyApp::Exception->new(
47       404, ['content-type'=>'text/plain'], ['Not Found']);
48   };
49
50   sub from_psgi_app :Local {
51     my ($self, $c) = @_;
52     $c->res->from_psgi_response(
53       $psgi_app->(
54         $c->req->env));
55   }
56
57   sub from_catalyst :Local {
58     my ($self, $c) = @_;
59     MyApp::Exception->throw(
60       403, ['content-type'=>'text/plain'], ['Forbidden']);
61   }
62
63   sub classic_error :Local {
64     my ($self, $c) = @_;
65     Catalyst::Exception->throw("Ex Parrot");
66   }
67
68   sub just_die :Local {
69     my ($self, $c) = @_;
70     die "I'm not dead yet";
71   }
72
73   package MyApp;
74   use Catalyst;
75
76   sub debug { 1 }
77
78   MyApp->setup_log('fatal');
79 }
80
81 $INC{'MyApp/Controller/Root.pm'} = '1'; # sorry...
82 MyApp->setup_log('error');
83
84 Test::More::ok(MyApp->setup);
85
86 ok my $psgi = MyApp->psgi_app;
87
88 test_psgi $psgi, sub {
89     my $cb = shift;
90     my $res = $cb->(GET "/root/from_psgi_app");
91     is $res->code, 404;
92     is $res->content, 'Not Found', 'NOT FOUND';
93 };
94
95 test_psgi $psgi, sub {
96     my $cb = shift;
97     my $res = $cb->(GET "/root/from_catalyst");
98     is $res->code, 403;
99     is $res->content, 'Forbidden', 'Forbidden';
100 };
101
102 test_psgi $psgi, sub {
103     my $cb = shift;
104     my $res = $cb->(GET "/root/classic_error");
105     is $res->code, 500;
106     like $res->content, qr'Ex Parrot', 'Ex Parrot';
107 };
108
109 test_psgi $psgi, sub {
110     my $cb = shift;
111     my $res = $cb->(GET "/root/just_die");
112     is $res->code, 500;
113     like $res->content, qr'not dead yet', 'not dead yet';
114 };
115
116
117
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).
121
122 done_testing(10);
123