File: hooks.t

package info (click to toggle)
libdancer2-perl 2.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,768 kB
  • sloc: perl: 8,671; sql: 14; makefile: 8
file content (352 lines) | stat: -rw-r--r-- 9,582 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
use strict;
use warnings;
use Test::More;
use File::Spec;
use Plack::Test;
use HTTP::Request::Common;
use Capture::Tiny 0.12 'capture_stderr';
use JSON::MaybeXS;

eval { require Template; 1; }
    or plan skip_all => 'Template::Toolkit not present';

my $tests_flags = {};

{
    package App::WithSerializer;
    use Dancer2;
    use Ref::Util qw<is_arrayref is_hashref>;

    set serializer => 'JSON';

    my @hooks = qw(
        before_request
        after_request

        before_serializer
        after_serializer
    );

    for my $hook (@hooks) {
        hook $hook => sub {
            $tests_flags->{$hook} ||= 0;
            $tests_flags->{$hook}++;
        };
    }

    get '/' => sub { +{ "ok" => 1 } };

    hook 'before_serializer' => sub {
        my ($data) = @_;  # don't shift, want to alias..
        if ( is_arrayref($data) ) {
            push( @{$data}, ( added_in_hook => 1 ) );
        } elsif ( is_hashref($data) ) {
            $data->{'added_in_hook'} = 1;
        } else {
            $_[0] = +{ 'added_in_hook' => 1 };
        }
    };

    get '/forward' => sub { Test::More::note 'About to forward!'; forward '/' };

    get '/redirect' => sub { redirect '/' };

    get '/json' => sub { +[ foo => 42 ] };

    get '/nothing' => sub { return };
}

{
    package App::WithFile;
    use Dancer2;
    my @hooks = qw<
        before_file_render
        after_file_render
    >;

    for my $hook (@hooks) {
        hook $hook => sub {
            $tests_flags->{$hook} ||= 0;
            $tests_flags->{$hook}++;
        };
    }

    get '/send_file' => sub {
        send_file( File::Spec->rel2abs(__FILE__), system_path => 1 );
    };
}

{
    package App::WithTemplate;
    use Dancer2;
    set template => 'tiny';

    my @hooks = qw(
        before_template_render
        after_template_render
    );

    for my $hook (@hooks) {
        hook $hook => sub {
            $tests_flags->{$hook} ||= 0;
            $tests_flags->{$hook}++;
        };
    }

    get '/template' => sub {
        template \"PLOP";
    };
}

{
    package App::WithIntercept;
    use Dancer2;

    get '/intercepted' => sub {'not intercepted'};

    hook before => sub {
        response->content('halted by before');
        halt;
    };
}

{
    package App::WithError;
    use Dancer2;

    my @hooks = qw(
        on_route_exception
    );

    for my $hook (@hooks) {
        hook $hook => sub {
            $tests_flags->{$hook} ||= 0;
            $tests_flags->{$hook}++;
        };
    }

    get '/route_exception' => sub {die 'this is a route exception'};

    hook after => sub {
        # GH#540 - ensure setting default scalar does not
        # interfere with hook execution (aliasing)
        $_ = 42;
    };

    hook on_route_exception => sub {
        my ($app, $error) = @_;
        ::is ref($app), 'Dancer2::Core::App';
        ::like $error, qr/this is a route exception/;
    };

    hook init_error => sub {
        my ($error) = @_;
        ::is ref($error), 'Dancer2::Core::Error';
    };

    hook before_error => sub {
        my ($error) = @_;
        ::is ref($error), 'Dancer2::Core::Error';
    };

    hook after_error => sub {
        my ($response) = @_;
        ::is ref($response), 'Dancer2::Core::Response';
        ::ok !$response->is_halted;
        ::like $response->content, qr/Internal Server Error/;
    };
}

# 3 test apps for the exception hook - see comments below
{
    package App::HookException;
    use Dancer2;

    get '/hook_exception' => sub { return 1 };

    hook after => sub {
        die 'this is an exception in the after hook';
    };

    hook on_hook_exception => sub {
        my ($app, $error, $hook_name) = @_;
        ::is ref($app), 'Dancer2::Core::App';
        ::like $error, qr/this is an exception in the after hook/;
        ::is $hook_name, 'core.app.after_request';
        $app->response->content("Setting response from exception hook");
        $app->response->halt;
    };
}

{
    package App::HookExceptionDouble;
    use Dancer2;

    get '/hook_exception_double' => sub { return 1 };

    hook after => sub {
        die 'this is an exception in the after hook';
    };

    hook on_hook_exception => sub {
        my ($app, $error) = @_;
        $app->response->content("Setting response from exception hook and then dying");
        $app->response->halt;
        die 'this is an exception in the exception hook';
    };
}

{
    package App::HookExceptionRecursive;
    use Dancer2;

    get '/hook_exception_recursive' => sub { return 1 };

    hook after => sub {
        die 'this is an exception in the after hook';
    };

    hook on_hook_exception => sub {
        my ($app, $error) = @_;
        die 'this is an exception in the hook exception causing recursion';
    };
}

subtest 'Request hooks' => sub {
    my $test = Plack::Test->create( App::WithSerializer->to_app );
    $test->request( GET '/' );

    is( $tests_flags->{before_request},     1,     "before_request was called" );
    is( $tests_flags->{after_request},      1,     "after_request was called" );
    is( $tests_flags->{before_serializer},  1, "before_serializer was called" );
    is( $tests_flags->{after_serializer},   1, "after_serializer was called" );
    is( $tests_flags->{before_file_render}, undef, "before_file_render undef" );

    note 'after hook called once per request';
    # Get current value of the 'after_request' tests flag.
    my $current = $tests_flags->{after_request};

    $test->request( GET '/redirect' );
    is(
        $tests_flags->{after_request},
        ++$current,
        "after_request called after redirect",
    );

    note 'Serializer hooks';

    $test->request( GET '/forward' );
    is(
        $tests_flags->{after_request},
        ++$current,
        "after_request called only once after forward",
    );

    my $res = $test->request( GET '/json' );
    is( $res->content, '["foo",42,"added_in_hook",1]', 'Response serialized' );
    is( $tests_flags->{before_serializer}, 4, 'before_serializer was called' );
    is( $tests_flags->{after_serializer},  4, 'after_serializer was called' );
    is( $tests_flags->{before_file_render}, undef, "before_file_render undef" );

    $res = $test->request( GET '/nothing' );
    is( $res->content, '{"added_in_hook":1}', 'Before hook modified content' );
    is( $tests_flags->{before_serializer}, 5, 'before_serializer was called with no content' );
    is( $tests_flags->{after_serializer},  5, 'after_serializer was called after content changes in hook' );
};

subtest 'file render hooks' => sub {
    my $test = Plack::Test->create( App::WithFile->to_app );
    $test->request( GET '/send_file' );
    is( $tests_flags->{before_file_render}, 1, "before_file_render was called" );
    is( $tests_flags->{after_file_render},  1, "after_file_render was called" );
};

subtest 'template render hook' => sub {
    my $test = Plack::Test->create( App::WithTemplate->to_app );

    $test->request( GET '/template' );
    is(
        $tests_flags->{before_template_render},
        1,
        "before_template_render was called",
    );

    is(
        $tests_flags->{after_template_render},
        1,
        "after_template_render was called",
    );
};

subtest 'before can halt' => sub {
    my $test = Plack::Test->create( App::WithIntercept->to_app );
    my $resp = $test->request( GET '/intercepted' );
    is( $resp->content, 'halted by before' );
};

subtest 'route_exception' => sub {
    my $test = Plack::Test->create( App::WithError->to_app );
    capture_stderr { $test->request( GET '/route_exception' ) };
};

# A basic test for a hook that is called in the event of an exception in a hook
subtest 'hook_exception' => sub {
    my $test = Plack::Test->create( App::HookException->to_app );
    my $resp = $test->request( GET '/hook_exception' );
    is( $resp->content, 'Setting response from exception hook' );
};

# A more advanced test that checks that an exception can take place within the
# exception hook, but only if it sets the response content and performs a halt
subtest 'hook_exception_double' => sub {
    my $test = Plack::Test->create( App::HookExceptionDouble->to_app );
    my $resp = $test->request( GET '/hook_exception_double');
    is( $resp->content, 'Setting response from exception hook and then dying' );
};

# Similar to the above, but without any handling for the second exception which
# would otherwise result in a recursive situation
subtest 'hook_exception_recursive' => sub {
    my $test = Plack::Test->create( App::HookExceptionRecursive->to_app );
    my $resp = $test->request( GET '/hook_exception_recursive');
    like( $resp->content, qr/Internal Server Error/ );
};

subtest 'hook entries logging' => sub {
    $::hook_counter = 0;
    $::trap;

    package App::HookEntries {
        use Sub::Util qw/ set_subname /;
        use Dancer2;

        set log => 'core';
        set logger => 'capture';

        $::trap = engine('logger')->trapper;

        get '/' => sub { 'hello there' };

        hook 'before_request' => set_subname my_before => sub {
            $::hook_counter++;
        };

        sub my_after { $::hook_counter++ }

        hook 'after_request' => \&my_after;
    }

    my $app = App::HookEntries->to_app;
    my $test = Plack::Test->create( $app );
    $test->request( GET '/' );

    is $::hook_counter => 2, "we hit both hooks";

    my @logs = map {$_->{message}} @{$::trap->read};

    for my $hook ( qw/ my_before my_after / ) {
        ok scalar( grep { /$hook/ } @logs ), "App::HookEntries::$hook"
    }
};

done_testing;