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