| 1 |
use strict; |
| 2 |
use warnings; |
| 3 |
|
| 4 |
use Test::More; |
| 5 |
use Plack::Test; |
| 6 |
use HTTP::Request::Common; |
| 7 |
|
| 8 |
# The hook chain: what runs, in what order, and what happens when a hook dies. |
| 9 |
# |
| 10 |
# Every app hook is wrapped by App::compile_hooks (Dancer2/Core/App.pm:1313), |
| 11 |
# which does three things worth pinning: it skips the hook entirely if the |
| 12 |
# response is already halted, it fires core.app.hook_exception when the hook |
| 13 |
# dies, and it decides between keeping a response the exception handler set and |
| 14 |
# croaking on to the 500 handler. |
| 15 |
# |
| 16 |
# Traces are collected in package arrays rather than asserted through the |
| 17 |
# response body, because several of these cases never reach a route at all and |
| 18 |
# the ordering is the thing being tested. |
| 19 |
# |
| 20 |
# Each of OrderApp/HaltApp/HaltInExceptionApp's PSGI coderef is built exactly |
| 21 |
# once, at file scope, simply so every subtest for a given app shares one |
| 22 |
# compiled app. Hook compilation is idempotent (see the to_app subtest below), |
| 23 |
# so this is no longer load-bearing the way it once was, but there is still |
| 24 |
# no reason to rebuild the same app repeatedly. |
| 25 |
|
| 26 |
{ |
| 27 |
package OrderApp; |
| 28 |
use Dancer2; |
| 29 |
set logger => 'null'; |
| 30 |
|
| 31 |
our @trace; |
| 32 |
|
| 33 |
hook on_hook_exception => sub { push @trace, "hook_exception:$_[2]" }; |
| 34 |
hook on_route_exception => sub { push @trace, 'route_exception' }; |
| 35 |
|
| 36 |
hook before => sub { |
| 37 |
push @trace, 'before'; |
| 38 |
die "before blew up\n" if request->path =~ /boom/; |
| 39 |
}; |
| 40 |
hook after => sub { push @trace, 'after' }; |
| 41 |
|
| 42 |
get '/ok' => sub { push @trace, 'route'; 'OK' }; |
| 43 |
get '/boom' => sub { push @trace, 'route-MUST-NOT-RUN'; 'NOPE' }; |
| 44 |
} |
| 45 |
|
| 46 |
{ |
| 47 |
package HaltApp; |
| 48 |
use Dancer2; |
| 49 |
set logger => 'null'; |
| 50 |
|
| 51 |
our @trace; |
| 52 |
|
| 53 |
hook before => sub { push @trace, 'before-1'; halt('HALTED-IN-BEFORE') }; |
| 54 |
hook before => sub { push @trace, 'before-2-MUST-NOT-RUN' }; |
| 55 |
hook after => sub { push @trace, 'after-MUST-NOT-RUN' }; |
| 56 |
|
| 57 |
get '/x' => sub { push @trace, 'route-MUST-NOT-RUN'; 'ROUTE' }; |
| 58 |
} |
| 59 |
|
| 60 |
# A hook_exception handler that sets its own response and halts. The wrapper |
| 61 |
# in compile_hooks explicitly supports this (see its comment at |
| 62 |
# Dancer2/Core/App.pm:1343-1349): halting in the handler is how it keeps a |
| 63 |
# custom response instead of croaking on to the 500 handler. |
| 64 |
{ |
| 65 |
package HaltInExceptionApp; |
| 66 |
use Dancer2; |
| 67 |
set logger => 'null'; |
| 68 |
|
| 69 |
our @trace; |
| 70 |
|
| 71 |
hook on_hook_exception => sub { |
| 72 |
my ( $app, $err, $position ) = @_; |
| 73 |
$err =~ s/\n.*//s; |
| 74 |
push @trace, "hook_exception:$position"; |
| 75 |
$app->response->status(418); |
| 76 |
$app->response->content('CUSTOM-FROM-HANDLER'); |
| 77 |
$app->response->is_halted(1); |
| 78 |
}; |
| 79 |
hook on_route_exception => sub { push @trace, 'route_exception' }; |
| 80 |
|
| 81 |
hook before => sub { push @trace, 'before'; die "before blew up\n" }; |
| 82 |
hook after => sub { push @trace, 'after' }; |
| 83 |
|
| 84 |
get '/x' => sub { push @trace, 'route'; 'ROUTE-BODY' }; |
| 85 |
} |
| 86 |
|
| 87 |
# Built once each - see the note at the top of this file. (RecompiledApp, |
| 88 |
# below, deliberately builds its PSGI coderef more than once - that's the |
| 89 |
# thing it's testing.) |
| 90 |
my $order_test = Plack::Test->create( OrderApp->to_app ); |
| 91 |
my $halt_test = Plack::Test->create( HaltApp->to_app ); |
| 92 |
my $halt_in_exception_test = |
| 93 |
Plack::Test->create( HaltInExceptionApp->to_app ); |
| 94 |
|
| 95 |
subtest 'hooks run around the route in order' => sub { |
| 96 |
my $test = $order_test; |
| 97 |
|
| 98 |
@OrderApp::trace = (); |
| 99 |
my $response = $test->request( GET '/ok' ); |
| 100 |
|
| 101 |
is( $response->code, 200, 'the request succeeds' ); |
| 102 |
is( $response->content, 'OK', 'the route produced the body' ); |
| 103 |
is_deeply( \@OrderApp::trace, [ 'before', 'route', 'after' ], |
| 104 |
'before, then the route, then after' ); |
| 105 |
}; |
| 106 |
|
| 107 |
subtest 'a dying before hook fires both exception hooks and 500s' => sub { |
| 108 |
my $test = $order_test; |
| 109 |
|
| 110 |
@OrderApp::trace = (); |
| 111 |
my $response = $test->request( GET '/boom' ); |
| 112 |
|
| 113 |
is( $response->code, 500, 'the request becomes a 500' ); |
| 114 |
|
| 115 |
# Both hooks fire, and hook_exception is told which position died. The |
| 116 |
# route must not run: the before hook refused the request. |
| 117 |
is_deeply( |
| 118 |
\@OrderApp::trace, |
| 119 |
[ |
| 120 |
'before', |
| 121 |
'hook_exception:core.app.before_request', |
| 122 |
'route_exception', |
| 123 |
], |
| 124 |
'hook_exception fires first with the position, then route_exception, and the route never runs', |
| 125 |
); |
| 126 |
|
| 127 |
like( $response->content, qr/<!DOCTYPE html>/, |
| 128 |
'and an error page is rendered' ); |
| 129 |
}; |
| 130 |
|
| 131 |
subtest 'halt in a before hook stops the whole chain' => sub { |
| 132 |
my $test = $halt_test; |
| 133 |
|
| 134 |
@HaltApp::trace = (); |
| 135 |
my $response = $test->request( GET '/x' ); |
| 136 |
|
| 137 |
is( $response->code, 200, 'halt keeps the status it was given' ); |
| 138 |
is( $response->content, 'HALTED-IN-BEFORE', |
| 139 |
'and the halted body is what is returned' ); |
| 140 |
|
| 141 |
# This is the assertion: exactly one hook ran. A second before hook, the |
| 142 |
# route, and the after hook are all skipped, which is compile_hooks' |
| 143 |
# is_halted check (Dancer2/Core/App.pm:1322-1324) doing its job. |
| 144 |
is_deeply( \@HaltApp::trace, ['before-1'], |
| 145 |
'no later hook and no route runs after halt' ); |
| 146 |
}; |
| 147 |
|
| 148 |
subtest 'a halting hook_exception handler keeps the route refused' => sub { |
| 149 |
|
| 150 |
# Fixed. The wrapper captures is_halted before deciding whether to |
| 151 |
# call $app->cleanup (Dancer2/Core/App.pm:1335-1342), and now skips |
| 152 |
# cleanup when the response was halted, as well as when this is itself |
| 153 |
# the hook_exception handler. A halt means "this response is final", so |
| 154 |
# the request, response and session the handler set up must survive for |
| 155 |
# dispatch to see - not be cleared out from under it. |
| 156 |
# |
| 157 |
# With cleanup skipped, dispatch's own is_halted check |
| 158 |
# (Dancer2/Core/App.pm:1748, reached via _dispatch_route's check at |
| 159 |
# ~1831) sees the same halted response the handler built, returns it |
| 160 |
# immediately, and neither the route nor the after hook ever run. |
| 161 |
|
| 162 |
my $test = $halt_in_exception_test; |
| 163 |
|
| 164 |
@HaltInExceptionApp::trace = (); |
| 165 |
my $response = $test->request( GET '/x' ); |
| 166 |
|
| 167 |
is( $response->code, 418, 'the handler\'s status is what the client gets' ); |
| 168 |
is( $response->content, 'CUSTOM-FROM-HANDLER', |
| 169 |
'and the handler\'s body' ); |
| 170 |
|
| 171 |
# The route the before hook refused must never run. |
| 172 |
ok( |
| 173 |
!scalar( grep { $_ eq 'route' } @HaltInExceptionApp::trace ), |
| 174 |
'the route does not run - the before hook\'s refusal holds', |
| 175 |
); |
| 176 |
|
| 177 |
# The exception handler fires exactly once, for the before hook that |
| 178 |
# really failed. There is no second, spurious failure caused by cleanup |
| 179 |
# destroying state the dispatcher still needed. |
| 180 |
my @exceptions = grep { /^hook_exception:/ } @HaltInExceptionApp::trace; |
| 181 |
is( scalar @exceptions, 1, |
| 182 |
'the exception handler fires exactly once' ); |
| 183 |
is( $exceptions[0], 'hook_exception:core.app.before_request', |
| 184 |
'for the before hook that failed' ); |
| 185 |
|
| 186 |
is_deeply( |
| 187 |
\@HaltInExceptionApp::trace, |
| 188 |
[ |
| 189 |
'before', |
| 190 |
'hook_exception:core.app.before_request', |
| 191 |
], |
| 192 |
'the full sequence: before dies, the handler runs, and nothing else', |
| 193 |
); |
| 194 |
}; |
| 195 |
|
| 196 |
subtest 'to_app compiles the hooks only once, however many times it is called' => sub { |
| 197 |
|
| 198 |
# Fixed. finish() calls compile_hooks(), which wraps each hook and |
| 199 |
# puts the wrappers back via replace_hook - so a second to_app() would |
| 200 |
# wrap the already-wrapped hooks again, and on the failure path each |
| 201 |
# layer would treat the inner layer's croak as a fresh hook failure and |
| 202 |
# fire core.app.hook_exception itself: a single dying hook reporting N |
| 203 |
# times after N calls to to_app. |
| 204 |
# |
| 205 |
# compile_hooks now records each wrapper it produces in the app's |
| 206 |
# _compiled_hooks registry (Dancer2/Core/App.pm) and passes its own |
| 207 |
# earlier work through untouched, so a hook is wrapped exactly once |
| 208 |
# however many times compile_hooks runs. That restores the single-fire |
| 209 |
# intent the wrapper already states for itself: |
| 210 |
# it carries an explicit guard against firing hook_exception recursively |
| 211 |
# (Dancer2/Core/App.pm:1329-1334), which only ever considered recursion |
| 212 |
# through the handler - it's the second layer of wrapping this closes. |
| 213 |
# |
| 214 |
# Every other app in this file still builds its PSGI coderef once, simply |
| 215 |
# because there's no reason to rebuild it - not because a second call |
| 216 |
# would misbehave, as this subtest demonstrates. |
| 217 |
|
| 218 |
{ |
| 219 |
package RecompiledApp; |
| 220 |
use Dancer2; |
| 221 |
set logger => 'null'; |
| 222 |
|
| 223 |
our @trace; |
| 224 |
|
| 225 |
hook on_hook_exception => sub { push @trace, 'hook_exception' }; |
| 226 |
hook on_route_exception => sub { push @trace, 'route_exception' }; |
| 227 |
hook before => sub { push @trace, 'before'; die "boom\n" }; |
| 228 |
|
| 229 |
get '/x' => sub { 'X' }; |
| 230 |
} |
| 231 |
|
| 232 |
my @counts; |
| 233 |
for my $call ( 1 .. 3 ) { |
| 234 |
my $test = Plack::Test->create( RecompiledApp->to_app ); |
| 235 |
@RecompiledApp::trace = (); |
| 236 |
$test->request( GET '/x' ); |
| 237 |
push @counts, |
| 238 |
scalar grep { $_ eq 'hook_exception' } @RecompiledApp::trace; |
| 239 |
} |
| 240 |
|
| 241 |
is( $counts[0], 1, |
| 242 |
'the first to_app reports the failing hook once, correctly' ); |
| 243 |
is_deeply( \@counts, [ 1, 1, 1 ], |
| 244 |
'every further to_app still reports the same failure exactly once' ); |
| 245 |
}; |
| 246 |
|
| 247 |
subtest 'a hook registered after the first to_app is still compiled' => sub { |
| 248 |
|
| 249 |
# The companion to the subtest above, and the reason that fix records |
| 250 |
# individual wrappers rather than setting a single "hooks are compiled" |
| 251 |
# flag on the app. Hooks can arrive after the first compile: finish() |
| 252 |
# adds postponed plugin hooks immediately *after* calling compile_hooks, |
| 253 |
# so a per-app flag would mean a later to_app() left them permanently |
| 254 |
# unwrapped - and an unwrapped hook is not merely unreported, it dies |
| 255 |
# straight out through the dispatcher instead of reaching |
| 256 |
# core.app.hook_exception at all. |
| 257 |
# |
| 258 |
# Asserted through the same observable as the subtest above: a dying |
| 259 |
# hook that reaches the wrapper reports itself exactly once. |
| 260 |
|
| 261 |
{ |
| 262 |
package LateHookApp; |
| 263 |
use Dancer2; |
| 264 |
set logger => 'null'; |
| 265 |
|
| 266 |
our @trace; |
| 267 |
|
| 268 |
hook on_hook_exception => sub { push @trace, 'hook_exception' }; |
| 269 |
get '/x' => sub { 'X' }; |
| 270 |
} |
| 271 |
|
| 272 |
# Compile once with no before hook at all, so the late one below cannot |
| 273 |
# ride in on the first compile. |
| 274 |
LateHookApp->to_app; |
| 275 |
|
| 276 |
LateHookApp->to_app; # and again, so the registry has been consulted |
| 277 |
|
| 278 |
Dancer2->runner->apps->[-1]->add_hook( |
| 279 |
Dancer2::Core::Hook->new( |
| 280 |
name => 'before', |
| 281 |
code => sub { push @LateHookApp::trace, 'late'; die "boom\n" }, |
| 282 |
) |
| 283 |
); |
| 284 |
|
| 285 |
my $test = Plack::Test->create( LateHookApp->to_app ); |
| 286 |
@LateHookApp::trace = (); |
| 287 |
$test->request( GET '/x' ); |
| 288 |
|
| 289 |
is( scalar( grep { $_ eq 'late' } @LateHookApp::trace ), 1, |
| 290 |
'the late hook runs' ); |
| 291 |
is( scalar( grep { $_ eq 'hook_exception' } @LateHookApp::trace ), 1, |
| 292 |
'and its failure is reported exactly once, so it was wrapped' ); |
| 293 |
}; |
| 294 |
|
| 295 |
done_testing(); |