1 package t::lib::TestSession;
7 use Plack::Middleware::Session;
24 my $m = Plack::Middleware::Session->new(state => $state, store => $storage);
26 $response_test = sub {
27 my ($response, $session_id, $check_expired) = @_;
32 my $r = $request_creator->();
34 my $s = Plack::Session->fetch_or_create($r, $m);
38 ok(!$s->get('foo'), '... no value stored in foo for session');
41 $s->set( foo => 'bar' );
42 } '... set the value successfully in session';
44 is($s->get('foo'), 'bar', '... got the foo value back successfully from session');
46 ok(!$s->get('bar'), '... no value stored in foo for session');
49 $s->set( bar => 'baz' );
50 } '... set the value successfully in session';
52 is($s->get('bar'), 'baz', '... got the foo value back successfully from session');
54 my $resp = $r->new_response;
57 $s->finalize( $resp );
58 } '... finalized session successfully';
60 is_deeply( $s->dump, { foo => 'bar', bar => 'baz' }, '... got the session dump we expected');
62 $response_test->( $resp, $sids[0] );
66 my $r = $request_creator->();
68 my $s = Plack::Session->fetch_or_create($r, $m);
72 isnt($sids[0], $sids[1], "no same Session ID");
73 ok(!$s->get('foo'), '... no value stored for foo in session');
76 $s->set( foo => 'baz' );
77 } '... set the value successfully';
79 is($s->get('foo'), 'baz', '... got the foo value back successfully from session');
81 my $resp = $r->new_response;
84 $s->finalize( $resp );
85 } '... finalized session successfully';
87 is_deeply( $s->dump, { foo => 'baz' }, '... got the session dump we expected');
89 $response_test->( $resp, $sids[1] );
93 my $r = $request_creator->({ plack_session => $sids[0] });
95 my $s = Plack::Session->fetch_or_create($r, $m);
97 is($s->id, $sids[0], '... got a basic session id');
99 is($s->get('foo'), 'bar', '... got the value for foo back successfully from session');
104 } '... removed the foo value successfully from session';
106 ok(!$s->get('foo'), '... no value stored for foo in session');
108 my $resp = $r->new_response;
111 $s->finalize( $resp );
112 } '... finalized session successfully';
114 is_deeply( $s->dump, { bar => 'baz' }, '... got the session dump we expected');
116 $response_test->( $resp, $sids[0] );
121 my $r = $request_creator->({ plack_session => $sids[1] });
123 my $s = Plack::Session->fetch_or_create($r, $m);
125 is($s->id, $sids[1], '... got a basic session id');
127 is($s->get('foo'), 'baz', '... got the foo value back successfully from session');
129 my $resp = $r->new_response;
132 $s->finalize( $resp );
133 } '... finalized session successfully';
135 is_deeply( $s->dump, { foo => 'baz' }, '... got the session dump we expected');
137 $response_test->( $resp, $sids[1] );
141 my $r = $request_creator->({ plack_session => $sids[0] });
143 my $s = Plack::Session->fetch_or_create($r, $m);
145 is($s->id, $sids[0], '... got a basic session id');
147 ok(!$s->get('foo'), '... no value stored for foo in session');
150 $s->set( baz => 'gorch' );
151 } '... set the bar value successfully in session';
153 my $resp = $r->new_response;
156 $s->finalize( $resp );
157 } '... finalized session successfully';
159 is_deeply( $s->dump, { bar => 'baz', baz => 'gorch' }, '... got the session dump we expected');
161 $response_test->( $resp, $sids[0] );
165 my $r = $request_creator->({ plack_session => $sids[0] });
167 my $s = Plack::Session->fetch_or_create($r, $m);
169 is($s->get('bar'), 'baz', '... got the bar value back successfully from session');
173 } '... expired session successfully';
175 my $resp = $r->new_response;
178 $s->finalize( $resp );
179 } '... finalized session successfully';
181 is_deeply( $s->dump, {}, '... got the session dump we expected');
183 $response_test->( $resp, $sids[0], 1 );
187 my $r = $request_creator->({ plack_session => $sids[0] });
189 my $s = Plack::Session->fetch_or_create($r, $m);
192 isnt($s->id, $sids[0], 'expired ... got a new session id');
194 ok(!$s->get('bar'), '... no bar value stored');
196 my $resp = $r->new_response;
199 $s->finalize( $resp );
200 } '... finalized session successfully';
202 is_deeply( $s->dump, {}, '... got the session dump we expected');
204 $response_test->( $resp, $sids[2] );
208 my $r = $request_creator->({ plack_session => $sids[1] });
210 my $s = Plack::Session->fetch_or_create($r, $m);
212 is($s->id, $sids[1], '... got a basic session id');
214 is($s->get('foo'), 'baz', '... got the foo value back successfully from session');
216 my $resp = $r->new_response;
219 $s->finalize( $resp );
220 } '... finalized session successfully';
222 is_deeply( $s->dump, { foo => 'baz' }, '... got the session dump we expected');
224 $response_test->( $resp, $sids[1] );
228 # wrong format session_id
229 my $r = $request_creator->({ plack_session => '../wrong' });
231 my $s = Plack::Session->fetch_or_create($r, $m);
233 isnt('../wrong' => $s->id, '... regenerate session id');
235 ok(!$s->get('foo'), '... no value stored for foo in session');
238 $s->set( foo => 'baz' );
239 } '... set the value successfully';
241 is($s->get('foo'), 'baz', '... got the foo value back successfully from session');
243 my $resp = $r->new_response;
246 $s->finalize( $resp );
247 } '... finalized session successfully';
249 is_deeply( $s->dump, { foo => 'baz' }, '... got the session dump we expected');
251 $response_test->( $resp, $s );