1 package t::lib::TestSession;
7 use Plack::Middleware::Session;
15 $session = $env->{'plack.session'};
17 my $responder = shift;
18 $responder->([ 200, [], [] ]);
22 my $res = $mw->($app)->($env);
24 return ($session, $res);
42 my $m = sub { Plack::Middleware::Session->wrap($_[0], session_class => "Plack::Session", state => $state, store => $storage) };
44 $response_test ||= sub {
45 my($res_cb, $session_id, $check_expired) = @_;
46 $res_cb->(sub { my $res = shift });
51 my($s, $res) = create_session($m, $env_cb->());
55 ok(!$s->get('foo'), '... no value stored in foo for session');
58 $s->set( foo => 'bar' );
59 } '... set the value successfully in session';
61 is($s->get('foo'), 'bar', '... got the foo value back successfully from session');
63 ok(!$s->get('bar'), '... no value stored in foo for session');
66 $s->set( bar => 'baz' );
67 } '... set the value successfully in session';
69 is($s->get('bar'), 'baz', '... got the foo value back successfully from session');
71 is_deeply( $s->dump, { foo => 'bar', bar => 'baz' }, '... got the session dump we expected');
73 $response_test->($res, $sids[0]);
77 my($s, $res) = create_session($m, $env_cb->());
81 isnt($sids[0], $sids[1], "no same Session ID");
82 ok(!$s->get('foo'), '... no value stored for foo in session');
85 $s->set( foo => 'baz' );
86 } '... set the value successfully';
88 is($s->get('foo'), 'baz', '... got the foo value back successfully from session');
90 is_deeply( $s->dump, { foo => 'baz' }, '... got the session dump we expected');
92 $response_test->($res, $sids[1]);
96 my($s, $res) = create_session($m, $env_cb->({ plack_session => $sids[0] }));
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 is_deeply( $s->dump, { bar => 'baz' }, '... got the session dump we expected');
110 $response_test->( $res, $sids[0] );
115 my($s, $res) = create_session($m, $env_cb->({ plack_session => $sids[1] }));
117 is($s->id, $sids[1], '... got a basic session id');
119 is($s->get('foo'), 'baz', '... got the foo value back successfully from session');
121 is_deeply( $s->dump, { foo => 'baz' }, '... got the session dump we expected');
123 $response_test->( $res, $sids[1] );
127 my($s, $res) = create_session($m, $env_cb->({ plack_session => $sids[0] }));
129 is($s->id, $sids[0], '... got a basic session id');
131 ok(!$s->get('foo'), '... no value stored for foo in session');
134 $s->set( baz => 'gorch' );
135 } '... set the bar value successfully in session';
137 is_deeply( $s->dump, { bar => 'baz', baz => 'gorch' }, '... got the session dump we expected');
139 $response_test->( $res, $sids[0] );
143 my($s, $res) = create_session($m, $env_cb->({ plack_session => $sids[0] }));
145 is($s->get('bar'), 'baz', '... got the bar value back successfully from session');
149 } '... expired session successfully';
151 $response_test->( $res, $sids[0], 1 );
153 is_deeply( $s->dump, {}, '... got the session dump we expected');
157 my($s, $res) = create_session($m, $env_cb->({ plack_session => $sids[0] }));
160 isnt($s->id, $sids[0], 'expired ... got a new session id');
162 ok(!$s->get('bar'), '... no bar value stored');
164 is_deeply( $s->dump, {}, '... got the session dump we expected');
166 $response_test->( $res, $sids[2] );
170 my($s, $res) = create_session($m, $env_cb->({ plack_session => $sids[1] }));
172 is($s->id, $sids[1], '... got a basic session id');
174 is($s->get('foo'), 'baz', '... got the foo value back successfully from session');
176 is_deeply( $s->dump, { foo => 'baz' }, '... got the session dump we expected');
178 $response_test->( $res, $sids[1] );
182 # wrong format session_id
183 my($s, $res) = create_session($m, $env_cb->({ plack_session => "../wrong" }));
185 isnt('../wrong' => $s->id, '... regenerate session id');
187 ok(!$s->get('foo'), '... no value stored for foo in session');
190 $s->set( foo => 'baz' );
191 } '... set the value successfully';
193 is($s->get('foo'), 'baz', '... got the foo value back successfully from session');
195 is_deeply( $s->dump, { foo => 'baz' }, '... got the session dump we expected');
197 $response_test->( $res, $s->id );