/
forward_before_hook.t
57 lines (44 loc) · 1.13 KB
/
forward_before_hook.t
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
use strict;
use warnings;
use Test::More import => ['!pass'], tests => 4;
use Dancer2;
use Plack::Test;
use HTTP::Request::Common;
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;
is( ref $app, 'CODE', '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();