1 package t::lib::TestSessionHash;
6 use Test::Fatal qw(lives_ok);
7 use Plack::Middleware::Session;
12 my ($session, $session_options);
15 $session = $env->{'psgix.session'};
16 $session_options = $env->{'psgix.session.options'};
18 my $responder = shift;
19 $responder->([ 200, [], [] ]);
23 my $res = $mw->($app)->($env);
25 return ($session, $session_options, $res);
45 my $m = $middleware_create_cb
46 || sub { Plack::Middleware::Session->wrap($_[0], state => $state, store => $storage) };
48 $response_test ||= sub {
49 my($res_cb, $session_id, $check_expired) = @_;
50 $res_cb->(sub { my $res = shift });
55 my($s, $opts, $res) = create_session($m, $env_cb->());
57 push @sids, $opts->{id};
59 ok(!$s->{'foo'}, '... no value stored in foo for session');
63 } '... set the value successfully in session';
65 is($s->{'foo'}, 'bar', '... got the foo value back successfully from session');
67 ok(!$s->{'bar'}, '... no value stored in foo for session');
71 } '... set the value successfully in session';
73 is($s->{'bar'}, 'baz', '... got the foo value back successfully from session');
75 is_deeply( $s, { foo => 'bar', bar => 'baz' }, '... got the session dump we expected');
77 $response_test->($res, $sids[0]);
81 my($s, $opts, $res) = create_session($m, $env_cb->());
83 push @sids, $opts->{id};
85 isnt($sids[0], $sids[1], "no same Session ID");
86 ok(!$s->{'foo'}, '... no value stored for foo in session');
90 } '... set the value successfully';
92 is($s->{'foo'}, 'baz', '... got the foo value back successfully from session');
94 is_deeply( $s, { foo => 'baz' }, '... got the session dump we expected');
96 $response_test->($res, $sids[1]);
100 my($s, $opts, $res) = create_session($m, $env_cb->({ plack_session => $sids[0] }));
101 is($opts->{id}, $sids[0], '... got a basic session id');
103 is($s->{'foo'}, 'bar', '... got the value for foo back successfully from session');
108 } '... removed the foo value successfully from session';
110 ok(!$s->{'foo'}, '... no value stored for foo in session');
112 is_deeply( $s, { bar => 'baz' }, '... got the session dump we expected');
114 $response_test->( $res, $sids[0] );
119 my($s, $opts, $res) = create_session($m, $env_cb->({ plack_session => $sids[1] }));
121 is($opts->{id}, $sids[1], '... got a basic session id');
123 is($s->{'foo'}, 'baz', '... got the foo value back successfully from session');
125 is_deeply( $s, { foo => 'baz' }, '... got the session dump we expected');
127 $response_test->( $res, $sids[1] );
131 my($s, $opts, $res) = create_session($m, $env_cb->({ plack_session => $sids[0] }));
133 is($opts->{id}, $sids[0], '... got a basic session id');
135 ok(!$s->{'foo'}, '... no value stored for foo in session');
139 } '... set the bar value successfully in session';
141 is_deeply( $s, { bar => 'baz', baz => 'gorch' }, '... got the session dump we expected');
143 $response_test->( $res, $sids[0] );
147 my($s, $opts, $res) = create_session($m, $env_cb->({ plack_session => $sids[0] }));
149 is($s->{'bar'}, 'baz', '... got the bar value back successfully from session');
153 } '... expired session successfully';
155 $response_test->( $res, $sids[0], 1 );
158 # this will not pass, because
159 # it is just a hash ref and we are
160 # not clearing it. Should we be?
162 # is_deeply( $s, {}, '... got the session dump we expected');
166 my($s, $opts, $res) = create_session($m, $env_cb->({ plack_session => $sids[0] }));
168 push @sids, $opts->{id};
169 isnt($opts->{id}, $sids[0], 'expired ... got a new session id');
171 ok(!$s->{'bar'}, '... no bar value stored');
173 is_deeply( $s, {}, '... got the session dump we expected');
175 $response_test->( $res, $sids[2] );
179 my($s, $opts, $res) = create_session($m, $env_cb->({ plack_session => $sids[1] }));
181 is($opts->{id}, $sids[1], '... got a basic session id');
183 is($s->{'foo'}, 'baz', '... got the foo value back successfully from session');
185 is_deeply( $s, { foo => 'baz' }, '... got the session dump we expected');
187 $response_test->( $res, $sids[1] );
191 # wrong format session_id
192 my($s, $opts, $res) = create_session($m, $env_cb->({ plack_session => "../wrong" }));
194 isnt('../wrong' => $opts->{id}, '... regenerate session id');
196 ok(!$s->{'foo'}, '... no value stored for foo in session');
200 } '... set the value successfully';
202 is($s->{'foo'}, 'baz', '... got the foo value back successfully from session');
204 is_deeply( $s, { foo => 'baz' }, '... got the session dump we expected');
206 $response_test->( $res, $opts->{id} );