forked from PerlDancer/Dancer2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
session_hooks.t
190 lines (156 loc) · 7.42 KB
/
session_hooks.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
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
use strict;
use warnings;
use Test::More;
use Plack::Test;
use HTTP::Cookies;
use HTTP::Request::Common;
my @hooks_to_test = qw(
engine.session.before_retrieve
engine.session.after_retrieve
engine.session.before_create
engine.session.after_create
engine.session.before_change_id
engine.session.after_change_id
engine.session.before_destroy
engine.session.after_destroy
engine.session.before_flush
engine.session.after_flush
);
# we'll set a flag here when each hook is called. Then our test will then verify this
my $test_flags = {};
{
package App;
use Dancer2;
set(
show_stacktrace => 1,
envoriment => 'production'
);
setting( session => 'Simple' );
for my $hook (@hooks_to_test) {
hook $hook => sub {
$test_flags->{$hook} ||= 0;
$test_flags->{$hook}++;
}
}
get '/set_session' => sub {
session foo => 'bar'; #setting causes a session flush
return "ok";
};
get '/get_session' => sub {
::is session->read('foo'), 'bar', "Got the right session back";
return "ok";
};
get '/change_session_id' => sub {
app->change_session_id;
return "ok";
};
get '/destroy_session' => sub {
app->destroy_session;
return "ok";
};
#setup each hook again and test whether they return the correct type
#there is unfortunately quite some duplication here.
hook 'engine.session.before_create' => sub {
my ($response) = @_;
::isa_ok( $response, 'Dancer2::Core::Session' );
};
hook 'engine.session.after_create' => sub {
my ($response) = @_;
::isa_ok( $response, 'Dancer2::Core::Session' );
};
hook 'engine.session.after_retrieve' => sub {
my ($response) = @_;
::isa_ok( $response, 'Dancer2::Core::Session' );
};
}
my $test = Plack::Test->create( App->to_app );
my $jar = HTTP::Cookies->new;
my $url = "http://localhost";
is_deeply( $test_flags, {}, 'Make sure flag hash is clear' );
subtest set_session => sub {
my $res = $test->request( GET "$url/set_session" );
is $res->content, "ok", "set_session ran ok";
$jar->extract_cookies($res);
};
# we verify whether the hooks were called correctly.
subtest 'verify hooks for session create and session flush' => sub {
is $test_flags->{'engine.session.before_create'}, 1, "session.before_create called";
is $test_flags->{'engine.session.after_create'}, 1, "session.after_create called";
is $test_flags->{'engine.session.before_flush'}, 1, "session.before_flush called";
is $test_flags->{'engine.session.after_flush'}, 1, "session.after_flush called";
is $test_flags->{'engine.session.before_change_id'}, undef, "session.before_change_id not called";
is $test_flags->{'engine.session.after_change_id'}, undef, "session.after_change_id not called";
is $test_flags->{'engine.session.before_retrieve'}, undef, "session.before_retrieve not called";
is $test_flags->{'engine.session.after_retrieve'}, undef, "session.after_retrieve not called";
is $test_flags->{'engine.session.before_destroy'}, undef, "session.before_destroy not called";
is $test_flags->{'engine.session.after_destroy'}, undef, "session.after_destroy not called";
};
subtest 'verify Handler::File (static content) does not retrieve session' => sub {
my $req = GET "$url/file.txt";
$jar->add_cookie_header($req);
my $res = $test->request($req);
$jar->extract_cookies($res);
# These should not change from previous subtest
is $test_flags->{'engine.session.before_create'}, 1, "session.before_create not called";
is $test_flags->{'engine.session.after_create'}, 1, "session.after_create not called";
is $test_flags->{'engine.session.before_retrieve'}, undef, "session.before_retrieve not called";
is $test_flags->{'engine.session.after_retrieve'}, undef, "session.after_retrieve not called";
};
subtest get_session => sub {
my $req = GET "$url/get_session";
$jar->add_cookie_header($req);
my $res = $test->request($req);
is $res->content, "ok", "get_session ran ok";
$jar->extract_cookies($res);
};
subtest 'verify hooks for session retrieve' => sub {
is $test_flags->{'engine.session.before_retrieve'}, 1, "session.before_retrieve called";
is $test_flags->{'engine.session.after_retrieve'}, 1, "session.after_retrieve called";
is $test_flags->{'engine.session.before_create'}, 1, "session.before_create not called";
is $test_flags->{'engine.session.after_create'}, 1, "session.after_create not called";
is $test_flags->{'engine.session.before_flush'}, 1, "session.before_flush not called";
is $test_flags->{'engine.session.after_flush'}, 1, "session.after_flush not called";
is $test_flags->{'engine.session.before_change_id'}, undef, "session.before_change_id not called";
is $test_flags->{'engine.session.after_change_id'}, undef, "session.after_change_id not called";
is $test_flags->{'engine.session.before_destroy'}, undef, "session.before_destroy not called";
is $test_flags->{'engine.session.after_destroy'}, undef, "session.after_destroy not called";
};
subtest change_session_id => sub {
my $req = GET "$url/change_session_id";
$jar->add_cookie_header($req);
my $res = $test->request($req);
is $res->content, "ok", "get_session ran ok";
$jar->clear;
$jar->extract_cookies($res);
};
subtest 'verify hooks for change session id' => sub {
# change_session_id causes a retrieve
is $test_flags->{'engine.session.before_retrieve'}, 2, "session.before_retrieve called";
is $test_flags->{'engine.session.after_retrieve'}, 2, "session.after_retrieve called";
is $test_flags->{'engine.session.before_create'}, 1, "session.before_create not called";
is $test_flags->{'engine.session.after_create'}, 1, "session.after_create not called";
is $test_flags->{'engine.session.before_flush'}, 1, "session.before_flush not called";
is $test_flags->{'engine.session.after_flush'}, 1, "session.after_flush not called";
is $test_flags->{'engine.session.before_change_id'}, 1, "session.before_change_id called";
is $test_flags->{'engine.session.after_change_id'}, 1, "session.after_change_id called";
is $test_flags->{'engine.session.before_destroy'}, undef, "session.before_destroy not called";
is $test_flags->{'engine.session.after_destroy'}, undef, "session.after_destroy not called";
};
subtest destroy_session => sub {
my $req = GET "$url/destroy_session";
$jar->add_cookie_header($req);
my $res = $test->request($req);
is $res->content, "ok", "destroy_session ran ok";
};
subtest 'verify session destroy hooks' => sub {
is $test_flags->{'engine.session.before_destroy'}, 1, "session.before_destroy called";
is $test_flags->{'engine.session.after_destroy'}, 1, "session.after_destroy called";
#not sure if before and after retrieve should be called when the session is destroyed. But this happens.
is $test_flags->{'engine.session.before_retrieve'}, 3, "session.before_retrieve called";
is $test_flags->{'engine.session.after_retrieve'}, 3, "session.after_retrieve called";
is $test_flags->{'engine.session.before_create'}, 1, "session.before_create not called";
is $test_flags->{'engine.session.after_create'}, 1, "session.after_create not called";
is $test_flags->{'engine.session.before_flush'}, 1, "session.before_flush not called";
is $test_flags->{'engine.session.after_flush'}, 1, "session.after_flush not called";
};
done_testing;