Commit | Line | Data |
f331b3e0 |
1 | package t::lib::TestSession; |
2 | use strict; |
3 | use warnings; |
4 | |
5 | use Test::More; |
6 | use Test::Exception; |
7 | |
8 | sub run_all_tests { |
9 | my %params = @_; |
10 | |
11 | my ( |
12 | $request_creator, |
13 | $state, |
14 | $storage, |
15 | $response_test |
16 | ) = @params{qw[ |
17 | request_creator |
18 | state |
19 | store |
20 | response_test |
21 | ]}; |
22 | |
23 | $response_test = sub { |
24 | my ($response, $session_id, $check_expired) = @_; |
25 | }; |
26 | |
27 | my @sids; |
28 | { |
29 | my $r = $request_creator->(); |
30 | |
31 | my $s = Plack::Session->new( |
32 | state => $state, |
33 | store => $storage, |
34 | request => $r, |
35 | ); |
36 | |
37 | push @sids, $s->id; |
38 | |
39 | ok(!$s->get('foo'), '... no value stored in foo for session'); |
40 | |
41 | lives_ok { |
42 | $s->set( foo => 'bar' ); |
43 | } '... set the value successfully in session'; |
44 | |
45 | is($s->get('foo'), 'bar', '... got the foo value back successfully from session'); |
46 | |
74e0eff0 |
47 | ok(!$s->get('bar'), '... no value stored in foo for session'); |
48 | |
49 | lives_ok { |
50 | $s->set( bar => 'baz' ); |
51 | } '... set the value successfully in session'; |
52 | |
53 | is($s->get('bar'), 'baz', '... got the foo value back successfully from session'); |
54 | |
f331b3e0 |
55 | my $resp = $r->new_response; |
56 | |
57 | lives_ok { |
58 | $s->finalize( $resp ); |
59 | } '... finalized session successfully'; |
60 | |
74e0eff0 |
61 | is_deeply( $s->store->dump_session( $sids[0] ), { foo => 'bar', bar => 'baz' }, '... got the session dump we expected'); |
62 | |
f331b3e0 |
63 | $response_test->( $resp, $sids[0] ); |
64 | } |
65 | |
66 | { |
67 | my $r = $request_creator->(); |
68 | |
69 | my $s = Plack::Session->new( |
70 | state => $state, |
71 | store => $storage, |
72 | request => $r, |
73 | ); |
74 | |
75 | push @sids, $s->id; |
76 | |
77 | isnt($sids[0], $sids[1], "no same Session ID"); |
78 | ok(!$s->get('foo'), '... no value stored for foo in session'); |
79 | |
80 | lives_ok { |
81 | $s->set( foo => 'baz' ); |
82 | } '... set the value successfully'; |
83 | |
84 | is($s->get('foo'), 'baz', '... got the foo value back successfully from session'); |
85 | |
86 | my $resp = $r->new_response; |
87 | |
88 | lives_ok { |
89 | $s->finalize( $resp ); |
90 | } '... finalized session successfully'; |
91 | |
74e0eff0 |
92 | is_deeply( $s->store->dump_session( $sids[1] ), { foo => 'baz' }, '... got the session dump we expected'); |
93 | |
f331b3e0 |
94 | $response_test->( $resp, $sids[1] ); |
95 | } |
96 | |
97 | { |
98 | my $r = $request_creator->({ plack_session => $sids[0] }); |
99 | |
100 | my $s = Plack::Session->new( |
101 | state => $state, |
102 | store => $storage, |
103 | request => $r, |
104 | ); |
105 | |
106 | is($s->id, $sids[0], '... got a basic session id'); |
107 | |
108 | is($s->get('foo'), 'bar', '... got the value for foo back successfully from session'); |
109 | |
110 | lives_ok { |
111 | $s->remove( 'foo' ); |
112 | } '... removed the foo value successfully from session'; |
113 | |
114 | ok(!$s->get('foo'), '... no value stored for foo in session'); |
115 | |
116 | my $resp = $r->new_response; |
117 | |
118 | lives_ok { |
119 | $s->finalize( $resp ); |
120 | } '... finalized session successfully'; |
121 | |
74e0eff0 |
122 | is_deeply( $s->store->dump_session( $sids[0] ), { bar => 'baz' }, '... got the session dump we expected'); |
123 | |
f331b3e0 |
124 | $response_test->( $resp, $sids[0] ); |
125 | } |
126 | |
127 | |
128 | { |
129 | my $r = $request_creator->({ plack_session => $sids[1] }); |
130 | |
131 | my $s = Plack::Session->new( |
132 | state => $state, |
133 | store => $storage, |
134 | request => $r, |
135 | ); |
136 | |
137 | is($s->id, $sids[1], '... got a basic session id'); |
138 | |
139 | is($s->get('foo'), 'baz', '... got the foo value back successfully from session'); |
140 | |
141 | my $resp = $r->new_response; |
142 | |
143 | lives_ok { |
144 | $s->finalize( $resp ); |
145 | } '... finalized session successfully'; |
146 | |
74e0eff0 |
147 | is_deeply( $s->store->dump_session( $sids[1] ), { foo => 'baz' }, '... got the session dump we expected'); |
148 | |
f331b3e0 |
149 | $response_test->( $resp, $sids[1] ); |
150 | } |
151 | |
152 | { |
153 | my $r = $request_creator->({ plack_session => $sids[0] }); |
154 | |
155 | my $s = Plack::Session->new( |
156 | state => $state, |
157 | store => $storage, |
158 | request => $r, |
159 | ); |
160 | |
161 | is($s->id, $sids[0], '... got a basic session id'); |
162 | |
163 | ok(!$s->get('foo'), '... no value stored for foo in session'); |
164 | |
165 | lives_ok { |
74e0eff0 |
166 | $s->set( baz => 'gorch' ); |
f331b3e0 |
167 | } '... set the bar value successfully in session'; |
168 | |
169 | my $resp = $r->new_response; |
170 | |
171 | lives_ok { |
172 | $s->finalize( $resp ); |
173 | } '... finalized session successfully'; |
174 | |
74e0eff0 |
175 | is_deeply( $s->store->dump_session( $sids[0] ), { bar => 'baz', baz => 'gorch' }, '... got the session dump we expected'); |
176 | |
f331b3e0 |
177 | $response_test->( $resp, $sids[0] ); |
178 | } |
179 | |
180 | { |
181 | my $r = $request_creator->({ plack_session => $sids[0] }); |
182 | |
183 | my $s = Plack::Session->new( |
184 | state => $state, |
185 | store => $storage, |
186 | request => $r, |
187 | ); |
188 | |
189 | is($s->get('bar'), 'baz', '... got the bar value back successfully from session'); |
190 | |
191 | lives_ok { |
192 | $s->expire; |
193 | } '... expired session successfully'; |
194 | |
195 | my $resp = $r->new_response; |
196 | |
197 | lives_ok { |
198 | $s->finalize( $resp ); |
199 | } '... finalized session successfully'; |
200 | |
74e0eff0 |
201 | is_deeply( $s->store->dump_session( $sids[0] ), {}, '... got the session dump we expected'); |
202 | |
f331b3e0 |
203 | $response_test->( $resp, $sids[0], 1 ); |
204 | } |
205 | |
206 | { |
207 | my $r = $request_creator->({ plack_session => $sids[0] }); |
208 | |
209 | my $s = Plack::Session->new( |
210 | state => $state, |
211 | store => $storage, |
212 | request => $r, |
213 | ); |
214 | |
215 | push @sids, $s->id; |
216 | isnt($s->id, $sids[0], 'expired ... got a new session id'); |
217 | |
218 | ok(!$s->get('bar'), '... no bar value stored'); |
219 | |
220 | my $resp = $r->new_response; |
221 | |
222 | lives_ok { |
223 | $s->finalize( $resp ); |
224 | } '... finalized session successfully'; |
225 | |
74e0eff0 |
226 | is_deeply( $s->store->dump_session( $sids[2] ), {}, '... got the session dump we expected'); |
227 | |
f331b3e0 |
228 | $response_test->( $resp, $sids[2] ); |
229 | } |
230 | |
231 | { |
232 | my $r = $request_creator->({ plack_session => $sids[1] }); |
233 | |
234 | my $s = Plack::Session->new( |
235 | state => $state, |
236 | store => $storage, |
237 | request => $r, |
238 | ); |
239 | |
240 | is($s->id, $sids[1], '... got a basic session id'); |
241 | |
242 | is($s->get('foo'), 'baz', '... got the foo value back successfully from session'); |
243 | |
244 | my $resp = $r->new_response; |
245 | |
246 | lives_ok { |
247 | $s->finalize( $resp ); |
248 | } '... finalized session successfully'; |
249 | |
74e0eff0 |
250 | is_deeply( $s->store->dump_session( $sids[1] ), { foo => 'baz' }, '... got the session dump we expected'); |
251 | |
f331b3e0 |
252 | $response_test->( $resp, $sids[1] ); |
253 | } |
56b9910a |
254 | |
255 | { |
256 | # wrong format session_id |
257 | my $r = $request_creator->({ plack_session => '../wrong' }); |
258 | |
259 | my $s = Plack::Session->new( |
260 | state => $state, |
261 | store => $storage, |
262 | request => $r, |
263 | ); |
264 | |
265 | |
266 | isnt('../wrong' => $s->id, '... regenerate session id'); |
267 | |
268 | ok(!$s->get('foo'), '... no value stored for foo in session'); |
269 | |
270 | lives_ok { |
271 | $s->set( foo => 'baz' ); |
272 | } '... set the value successfully'; |
273 | |
274 | is($s->get('foo'), 'baz', '... got the foo value back successfully from session'); |
275 | |
276 | my $resp = $r->new_response; |
277 | |
278 | lives_ok { |
279 | $s->finalize( $resp ); |
280 | } '... finalized session successfully'; |
281 | |
74e0eff0 |
282 | is_deeply( $s->store->dump_session( $s->id ), { foo => 'baz' }, '... got the session dump we expected'); |
283 | |
56b9910a |
284 | $response_test->( $resp, $s ); |
285 | } |
f331b3e0 |
286 | } |
287 | |
56b9910a |
288 | 1; |