File: forward_before_hook.t

package info (click to toggle)
libdancer2-perl 0.400001%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,580 kB
  • sloc: perl: 8,461; makefile: 9
file content (58 lines) | stat: -rw-r--r-- 1,183 bytes parent folder | download | duplicates (4)
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
use strict;
use warnings;
use Test::More import => ['!pass'], tests => 4;
use Dancer2;
use Plack::Test;
use HTTP::Request::Common;
use Ref::Util qw<is_coderef>;

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

get '/default' => sub {
    return 'Default';
};

get '/redirect' => sub {
    return 'Secret stuff never seen';
};

hook before => sub {
    return if request->path eq '/default';

    # Add some content to the response
    response->content("SillyStringIsSilly");

    # redirect - response should include the above content
    return redirect '/default'
        if request->path eq '/redirect';

    # The response object will get replaced by the result of the forward.
    forward '/default';
};

my $app = __PACKAGE__->to_app;
ok( is_coderef($app), 'Got app' );

test_psgi $app, sub {
    my $cb = shift;

    like(
        $cb->( GET '/' )->content,
        qr{Default},
        'forward in before hook',
    );

    my $r = $cb->( GET '/redirect' );

    # redirect in before hook
    is( $r->code, 302, 'redirect in before hook' );
    is(
        $r->content,
        'SillyStringIsSilly',
        '.. and the response content is correct',
    );
};

done_testing();