fix for problem when a http style exception overloads stringification
[catagits/Catalyst-Runtime.git] / t / http_exceptions.t
CommitLineData
660f9bb0 1use warnings;
2use strict;
3use Test::More;
4use HTTP::Request::Common;
5use HTTP::Message::PSGI;
6use Plack::Util;
7use 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
6e94698d 15 use overload
16 # Use the overloading thet HTTP::Exception uses
17 bool => sub { 1 }, '""' => 'as_string', fallback => 1;
18
660f9bb0 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 }
6e94698d 37
38 sub as_string { 'bad stringy bad' }
660f9bb0 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...
82MyApp->setup_log('error');
83
84Test::More::ok(MyApp->setup);
85
86ok my $psgi = MyApp->psgi_app;
87
88test_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
95test_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
102test_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
109test_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
122done_testing(10);
123