Debian Codegraph @

t/integration/error/fallback.t

raw
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use Plack::Test;
6 use HTTP::Request::Common;
7 use Path::Tiny ();
8
9 # What renders an error page, and what happens when the thing meant to render
10 # it is itself broken.
11 #
12 # Dancer2::Core::Error::_build_content tries four sources in order:
13 #
14 # 1. a view named after the status (views/500.tt) - Error.pm:298-317
15 # 2. public/<status>.html - Error.pm:319-324
16 # 3. the configured error_template - Error.pm:326-341
17 # 4. the built-in default page - Error.pm:343
18 #
19 # Steps 1 and 3 render through the app's template engine, which is exactly what
20 # may have caused the error in the first place, so both are wrapped in an eval.
21 # A throwing template must therefore fall through rather than take the handler
22 # down - that is the behavior these tests exist for.
23 #
24 # Step 2 is skipped for a 500 when show_stacktrace is on (Error.pm:320): a
25 # static page would throw away the diagnostics the developer turned on.
26 #
27 # Template::Toolkit is used rather than the default Template::Tiny because
28 # [% THROW %] gives a template that reliably dies. It is a hard dependency of
29 # this distribution, so this adds nothing.
30
31 my $DIR;
32 BEGIN {
33 $DIR = Path::Tiny->tempdir;
34 $DIR->child('views')->mkpath;
35 $DIR->child('public')->mkpath;
36 $DIR->child('empty-public')->mkpath;
37
38 # A view named for the status that blows up when rendered.
39 $DIR->child( 'views', '500.tt' )
40 ->spew_utf8('[% THROW nope "this template is broken" %]');
41
42 # Static pages, distinguishable in the body.
43 $DIR->child( 'public', '500.html' )->spew_utf8('STATIC-500-PAGE');
44 $DIR->child( 'public', '404.html' )->spew_utf8('STATIC-404-PAGE');
45
46 # A working configured error_template.
47 $DIR->child( 'views', 'my_error.tt' )
48 ->spew_utf8('CONFIGURED-ERROR-TEMPLATE status=[% status %]');
49 }
50
51 # show_stacktrace on, broken 500.tt, static pages present.
52 {
53 package TraceOnApp;
54 use Dancer2;
55 set template => 'template_toolkit';
56 set views => $DIR->child('views')->stringify;
57 set public_dir => $DIR->child('public')->stringify;
58 set show_stacktrace => 1;
59 set logger => 'capture';
60
61 get '/die' => sub { die "kaboom\n" };
62 get '/nf' => sub { send_error( 'gone', 404 ) };
63 }
64
65 # Identical, but show_stacktrace off.
66 {
67 package TraceOffApp;
68 use Dancer2;
69 set template => 'template_toolkit';
70 set views => $DIR->child('views')->stringify;
71 set public_dir => $DIR->child('public')->stringify;
72 set show_stacktrace => 0;
73 set logger => 'capture';
74
75 get '/die' => sub { die "kaboom\n" };
76 get '/nf' => sub { send_error( 'gone', 404 ) };
77 }
78
79 # No static pages at all, but a configured error_template.
80 {
81 package ErrorTemplateApp;
82 use Dancer2;
83 set template => 'template_toolkit';
84 set views => $DIR->child('views')->stringify;
85 set public_dir => $DIR->child('empty-public')->stringify;
86 set show_stacktrace => 0;
87 set error_template => 'my_error';
88 set logger => 'capture';
89
90 get '/die' => sub { die "kaboom\n" };
91 }
92
93 sub logs_of {
94 my $package = shift;
95 return $package->dancer_app->logger_engine->trapper->read;
96 }
97
98 subtest 'a throwing status template falls through to the static page' => sub {
99 my $test = Plack::Test->create( TraceOffApp->to_app );
100
101 logs_of('TraceOffApp'); # drain anything from app startup
102
103 my $response = $test->request( GET '/die' );
104
105 # The handler survived: a response came back at all, with the right status.
106 is( $response->code, 500, 'the request still gets a 500, not a hang or a crash' );
107 is( $response->content, 'STATIC-500-PAGE',
108 'and the static page is served instead of the broken template' );
109
110 # The failure is not swallowed silently - it is logged as a warning, so a
111 # developer can find out their error template is broken.
112 my @warnings = grep { $_->{level} eq 'warning' } @{ logs_of('TraceOffApp') };
113 ok( scalar @warnings, 'the template failure is logged' );
114 like(
115 join( "\n", map { $_->{message} } @warnings ),
116 qr/this template is broken/,
117 'and the log names the template error',
118 );
119 };
120
121 subtest 'a status template is preferred when it works' => sub {
122 # 404 has no 404.tt, so the static 404.html is used. This is the control
123 # for the subtest above: it shows the static page is reached by falling
124 # through the template step, not because static always wins.
125 my $test = Plack::Test->create( TraceOffApp->to_app );
126
127 my $response = $test->request( GET '/nf' );
128 is( $response->code, 404, 'the 404 keeps its status' );
129 is( $response->content, 'STATIC-404-PAGE',
130 'and gets its own static page' );
131 };
132
133 subtest 'a 500 with show_stacktrace on skips the static page' => sub {
134 my $test = Plack::Test->create( TraceOnApp->to_app );
135
136 my $response = $test->request( GET '/die' );
137 my $body = $response->content;
138
139 is( $response->code, 500, 'still a 500' );
140
141 # Same app layout as TraceOffApp, which served STATIC-500-PAGE here. With
142 # show_stacktrace on, the static page is deliberately bypassed so the
143 # diagnostics are not thrown away.
144 isnt( $body, 'STATIC-500-PAGE',
145 'the static 500 page is not used when a stack trace was asked for' );
146 like( $body, qr/<div class="title">Stack</,
147 'the built-in diagnostic page is rendered instead' );
148
149 # A 4xx is unaffected by that rule and still gets its static page.
150 is( $test->request( GET '/nf' )->content, 'STATIC-404-PAGE',
151 'a 404 still gets its static page with show_stacktrace on' );
152 };
153
154 subtest 'the configured error_template is used when there is no static page' => sub {
155 my $test = Plack::Test->create( ErrorTemplateApp->to_app );
156
157 my $response = $test->request( GET '/die' );
158
159 is( $response->code, 500, 'the status is a 500' );
160 like( $response->content, qr/^CONFIGURED-ERROR-TEMPLATE/,
161 'the configured error_template renders the page' );
162 like( $response->content, qr/status=500/,
163 'and is given the status to render' );
164 };
165
166 done_testing();