Debian Codegraph @

t/forward_before_hook.t

raw
1 use strict;
2 use warnings;
3 use Test::More import => ['!pass'], tests => 4;
4 use Dancer2;
5 use Plack::Test;
6 use HTTP::Request::Common;
7 use Ref::Util qw<is_coderef>;
8
9 get '/' => sub {
10 return 'Forbidden';
11 };
12
13 get '/default' => sub {
14 return 'Default';
15 };
16
17 get '/redirect' => sub {
18 return 'Secret stuff never seen';
19 };
20
21 hook before => sub {
22 return if request->path eq '/default';
23
24 # Add some content to the response
25 response->content("SillyStringIsSilly");
26
27 # redirect - response should include the above content
28 return redirect '/default'
29 if request->path eq '/redirect';
30
31 # The response object will get replaced by the result of the forward.
32 forward '/default';
33 };
34
35 my $app = __PACKAGE__->to_app;
36 ok( is_coderef($app), 'Got app' );
37
38 test_psgi $app, sub {
39 my $cb = shift;
40
41 like(
42 $cb->( GET '/' )->content,
43 qr{Default},
44 'forward in before hook',
45 );
46
47 my $r = $cb->( GET '/redirect' );
48
49 # redirect in before hook
50 is( $r->code, 302, 'redirect in before hook' );
51 is(
52 $r->content,
53 'SillyStringIsSilly',
54 '.. and the response content is correct',
55 );
56 };
57
58 done_testing();