Debian Codegraph @

t/hooks.t

raw
1 use strict;
2 use warnings;
3 use Test::More;
4 use Path::Tiny ();
5 use Plack::Test;
6 use HTTP::Request::Common;
7 use Capture::Tiny 0.12 'capture_stderr';
8 use JSON::MaybeXS;
9
10 eval { require Template; 1; }
11 or plan skip_all => 'Template::Toolkit not present';
12
13 my $tests_flags = {};
14
15 {
16 package App::WithSerializer;
17 use Dancer2;
18 use Ref::Util qw<is_arrayref is_hashref>;
19
20 set serializer => 'JSON';
21
22 my @hooks = qw(
23 before_request
24 after_request
25
26 before_serializer
27 after_serializer
28 );
29
30 for my $hook (@hooks) {
31 hook $hook => sub {
32 $tests_flags->{$hook} ||= 0;
33 $tests_flags->{$hook}++;
34 };
35 }
36
37 get '/' => sub { +{ "ok" => 1 } };
38
39 hook 'before_serializer' => sub {
40 my ($data) = @_; # don't shift, want to alias..
41 if ( is_arrayref($data) ) {
42 push( @{$data}, ( added_in_hook => 1 ) );
43 } elsif ( is_hashref($data) ) {
44 $data->{'added_in_hook'} = 1;
45 } else {
46 $_[0] = +{ 'added_in_hook' => 1 };
47 }
48 };
49
50 get '/forward' => sub { Test::More::note 'About to forward!'; forward '/' };
51
52 get '/redirect' => sub { redirect '/' };
53
54 get '/json' => sub { +[ foo => 42 ] };
55
56 get '/nothing' => sub { return };
57 }
58
59 {
60 package App::WithFile;
61 use Dancer2;
62 my @hooks = qw<
63 before_file_render
64 after_file_render
65 >;
66
67 for my $hook (@hooks) {
68 hook $hook => sub {
69 $tests_flags->{$hook} ||= 0;
70 $tests_flags->{$hook}++;
71 };
72 }
73
74 get '/send_file' => sub {
75 send_file( Path::Tiny::path(__FILE__)->absolute->stringify, system_path => 1 );
76 };
77 }
78
79 {
80 package App::WithTemplate;
81 use Dancer2;
82 set template => 'tiny';
83
84 my @hooks = qw(
85 before_template_render
86 after_template_render
87 );
88
89 for my $hook (@hooks) {
90 hook $hook => sub {
91 $tests_flags->{$hook} ||= 0;
92 $tests_flags->{$hook}++;
93 };
94 }
95
96 get '/template' => sub {
97 template \"PLOP";
98 };
99 }
100
101 {
102 package App::WithIntercept;
103 use Dancer2;
104
105 get '/intercepted' => sub {'not intercepted'};
106
107 hook before => sub {
108 response->content('halted by before');
109 halt;
110 };
111 }
112
113 {
114 package App::WithError;
115 use Dancer2;
116
117 my @hooks = qw(
118 on_route_exception
119 );
120
121 for my $hook (@hooks) {
122 hook $hook => sub {
123 $tests_flags->{$hook} ||= 0;
124 $tests_flags->{$hook}++;
125 };
126 }
127
128 get '/route_exception' => sub {die 'this is a route exception'};
129
130 hook after => sub {
131 # GH#540 - ensure setting default scalar does not
132 # interfere with hook execution (aliasing)
133 $_ = 42;
134 };
135
136 hook on_route_exception => sub {
137 my ($app, $error) = @_;
138 ::is ref($app), 'Dancer2::Core::App';
139 ::like $error, qr/this is a route exception/;
140 };
141
142 hook init_error => sub {
143 my ($error) = @_;
144 ::is ref($error), 'Dancer2::Core::Error';
145 };
146
147 hook before_error => sub {
148 my ($error) = @_;
149 ::is ref($error), 'Dancer2::Core::Error';
150 };
151
152 hook after_error => sub {
153 my ($response) = @_;
154 ::is ref($response), 'Dancer2::Core::Response';
155 ::ok !$response->is_halted;
156 ::like $response->content, qr/Internal Server Error/;
157 };
158 }
159
160 # 3 test apps for the exception hook - see comments below
161 {
162 package App::HookException;
163 use Dancer2;
164
165 get '/hook_exception' => sub { return 1 };
166
167 hook after => sub {
168 die 'this is an exception in the after hook';
169 };
170
171 hook on_hook_exception => sub {
172 my ($app, $error, $hook_name) = @_;
173 ::is ref($app), 'Dancer2::Core::App';
174 ::like $error, qr/this is an exception in the after hook/;
175 ::is $hook_name, 'core.app.after_request';
176 $app->response->content("Setting response from exception hook");
177 $app->response->halt;
178 };
179 }
180
181 {
182 package App::HookExceptionDouble;
183 use Dancer2;
184
185 get '/hook_exception_double' => sub { return 1 };
186
187 hook after => sub {
188 die 'this is an exception in the after hook';
189 };
190
191 hook on_hook_exception => sub {
192 my ($app, $error) = @_;
193 $app->response->content("Setting response from exception hook and then dying");
194 $app->response->halt;
195 die 'this is an exception in the exception hook';
196 };
197 }
198
199 {
200 package App::HookExceptionRecursive;
201 use Dancer2;
202
203 get '/hook_exception_recursive' => sub { return 1 };
204
205 hook after => sub {
206 die 'this is an exception in the after hook';
207 };
208
209 hook on_hook_exception => sub {
210 my ($app, $error) = @_;
211 die 'this is an exception in the hook exception causing recursion';
212 };
213 }
214
215 subtest 'Request hooks' => sub {
216 my $test = Plack::Test->create( App::WithSerializer->to_app );
217 $test->request( GET '/' );
218
219 is( $tests_flags->{before_request}, 1, "before_request was called" );
220 is( $tests_flags->{after_request}, 1, "after_request was called" );
221 is( $tests_flags->{before_serializer}, 1, "before_serializer was called" );
222 is( $tests_flags->{after_serializer}, 1, "after_serializer was called" );
223 is( $tests_flags->{before_file_render}, undef, "before_file_render undef" );
224
225 note 'after hook called once per request';
226 # Get current value of the 'after_request' tests flag.
227 my $current = $tests_flags->{after_request};
228
229 $test->request( GET '/redirect' );
230 is(
231 $tests_flags->{after_request},
232 ++$current,
233 "after_request called after redirect",
234 );
235
236 note 'Serializer hooks';
237
238 $test->request( GET '/forward' );
239 is(
240 $tests_flags->{after_request},
241 ++$current,
242 "after_request called only once after forward",
243 );
244
245 my $res = $test->request( GET '/json' );
246 is( $res->content, '["foo",42,"added_in_hook",1]', 'Response serialized' );
247 is( $tests_flags->{before_serializer}, 4, 'before_serializer was called' );
248 is( $tests_flags->{after_serializer}, 4, 'after_serializer was called' );
249 is( $tests_flags->{before_file_render}, undef, "before_file_render undef" );
250
251 $res = $test->request( GET '/nothing' );
252 is( $res->content, '{"added_in_hook":1}', 'Before hook modified content' );
253 is( $tests_flags->{before_serializer}, 5, 'before_serializer was called with no content' );
254 is( $tests_flags->{after_serializer}, 5, 'after_serializer was called after content changes in hook' );
255 };
256
257 subtest 'file render hooks' => sub {
258 my $test = Plack::Test->create( App::WithFile->to_app );
259 $test->request( GET '/send_file' );
260 is( $tests_flags->{before_file_render}, 1, "before_file_render was called" );
261 is( $tests_flags->{after_file_render}, 1, "after_file_render was called" );
262 };
263
264 subtest 'template render hook' => sub {
265 my $test = Plack::Test->create( App::WithTemplate->to_app );
266
267 $test->request( GET '/template' );
268 is(
269 $tests_flags->{before_template_render},
270 1,
271 "before_template_render was called",
272 );
273
274 is(
275 $tests_flags->{after_template_render},
276 1,
277 "after_template_render was called",
278 );
279 };
280
281 subtest 'before can halt' => sub {
282 my $test = Plack::Test->create( App::WithIntercept->to_app );
283 my $resp = $test->request( GET '/intercepted' );
284 is( $resp->content, 'halted by before' );
285 };
286
287 subtest 'route_exception' => sub {
288 my $test = Plack::Test->create( App::WithError->to_app );
289 capture_stderr { $test->request( GET '/route_exception' ) };
290 };
291
292 # A basic test for a hook that is called in the event of an exception in a hook
293 subtest 'hook_exception' => sub {
294 my $test = Plack::Test->create( App::HookException->to_app );
295 my $resp = $test->request( GET '/hook_exception' );
296 is( $resp->content, 'Setting response from exception hook' );
297 };
298
299 # A more advanced test that checks that an exception can take place within the
300 # exception hook, but only if it sets the response content and performs a halt
301 subtest 'hook_exception_double' => sub {
302 my $test = Plack::Test->create( App::HookExceptionDouble->to_app );
303 my $resp = $test->request( GET '/hook_exception_double');
304 is( $resp->content, 'Setting response from exception hook and then dying' );
305 };
306
307 # Similar to the above, but without any handling for the second exception which
308 # would otherwise result in a recursive situation
309 subtest 'hook_exception_recursive' => sub {
310 my $test = Plack::Test->create( App::HookExceptionRecursive->to_app );
311 my $resp = $test->request( GET '/hook_exception_recursive');
312 like( $resp->content, qr/Internal Server Error/ );
313 };
314
315 subtest 'hook entries logging' => sub {
316 $::hook_counter = 0;
317
318 package App::HookEntries {
319 use Sub::Util qw/ set_subname /;
320 use Dancer2;
321
322 set log => 'core';
323 set logger => 'capture';
324
325 $::trap = engine('logger')->trapper;
326
327 get '/' => sub { 'hello there' };
328
329 hook 'before_request' => set_subname my_before => sub {
330 $::hook_counter++;
331 };
332
333 sub my_after { $::hook_counter++ }
334
335 hook 'after_request' => \&my_after;
336 }
337
338 my $app = App::HookEntries->to_app;
339 my $test = Plack::Test->create( $app );
340 $test->request( GET '/' );
341
342 is $::hook_counter => 2, "we hit both hooks";
343
344 my @logs = map {$_->{message}} @{$::trap->read};
345
346 for my $hook ( qw/ my_before my_after / ) {
347 ok scalar( grep { /$hook/ } @logs ), "App::HookEntries::$hook"
348 }
349 };
350
351 done_testing;