579689f99be2692e88485ffd18bf449480b7a156
[catagits/Web-Session.git] / t / lib / TestSession.pm
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
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
55         my $resp = $r->new_response;
56
57         lives_ok {
58             $s->finalize( $resp );
59         } '... finalized session successfully';
60
61         is_deeply( $s->store->dump_session( $sids[0] ), { foo => 'bar', bar => 'baz' }, '... got the session dump we expected');
62
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
92         is_deeply( $s->store->dump_session( $sids[1] ), { foo => 'baz' }, '... got the session dump we expected');
93
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
122         is_deeply( $s->store->dump_session( $sids[0] ), { bar => 'baz' }, '... got the session dump we expected');
123
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
147         is_deeply( $s->store->dump_session( $sids[1] ), { foo => 'baz' }, '... got the session dump we expected');
148
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 {
166             $s->set( baz => 'gorch' );
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
175         is_deeply( $s->store->dump_session( $sids[0] ), { bar => 'baz', baz => 'gorch' }, '... got the session dump we expected');
176
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
201         is_deeply( $s->store->dump_session( $sids[0] ), {}, '... got the session dump we expected');
202
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
226         is_deeply( $s->store->dump_session( $sids[2] ), {}, '... got the session dump we expected');
227
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
250         is_deeply( $s->store->dump_session( $sids[1] ), { foo => 'baz' }, '... got the session dump we expected');
251
252         $response_test->( $resp, $sids[1] );
253     }
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
282         is_deeply( $s->store->dump_session( $s->id ), { foo => 'baz' }, '... got the session dump we expected');
283
284         $response_test->( $resp, $s );
285     }
286 }
287
288 1;