c35f34dff81ceec8c2113a298f522de1c43a6ea8
[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 create_session {
10     my($mw, $env) = @_;
11
12     my $session;
13     my $app = sub {
14         my $env = shift;
15         $session = $env->{'plack.session'};
16         return sub {
17             my $responder = shift;
18             $responder->([ 200, [], [] ]);
19         };
20     };
21
22     my $res = $mw->($app)->($env);
23
24     return ($session, $res);
25 }
26
27 sub run_all_tests {
28     my %params = @_;
29
30     my (
31         $env_cb,
32         $state,
33         $storage,
34         $response_test
35     ) = @params{qw[
36         env_cb
37         state
38         store
39         response_test
40     ]};
41
42     my $m = sub { Plack::Middleware::Session->wrap($_[0], session_class => "Plack::Session", state => $state, store => $storage) };
43
44     $response_test ||= sub {
45         my($res_cb, $session_id, $check_expired) = @_;
46         $res_cb->(sub { my $res = shift });
47     };
48
49     my @sids;
50     {
51         my($s, $res) = create_session($m, $env_cb->());
52
53         push @sids, $s->id;
54
55         ok(!$s->get('foo'), '... no value stored in foo for session');
56
57         lives_ok {
58             $s->set( foo => 'bar' );
59         } '... set the value successfully in session';
60
61         is($s->get('foo'), 'bar', '... got the foo value back successfully from session');
62
63         ok(!$s->get('bar'), '... no value stored in foo for session');
64
65         lives_ok {
66             $s->set( bar => 'baz' );
67         } '... set the value successfully in session';
68
69         is($s->get('bar'), 'baz', '... got the foo value back successfully from session');
70
71         is_deeply( $s->dump, { foo => 'bar', bar => 'baz' }, '... got the session dump we expected');
72
73         $response_test->($res, $sids[0]);
74     }
75
76     {
77         my($s, $res) = create_session($m, $env_cb->());
78
79         push @sids, $s->id;
80
81         isnt($sids[0], $sids[1], "no same Session ID");
82         ok(!$s->get('foo'), '... no value stored for foo in session');
83
84         lives_ok {
85             $s->set( foo => 'baz' );
86         } '... set the value successfully';
87
88         is($s->get('foo'), 'baz', '... got the foo value back successfully from session');
89
90         is_deeply( $s->dump, { foo => 'baz' }, '... got the session dump we expected');
91
92         $response_test->($res, $sids[1]);
93     }
94
95     {
96         my($s, $res) = create_session($m, $env_cb->({ plack_session => $sids[0] }));
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         is_deeply( $s->dump, { bar => 'baz' }, '... got the session dump we expected');
109
110         $response_test->( $res, $sids[0] );
111     }
112
113
114     {
115         my($s, $res) = create_session($m, $env_cb->({ plack_session => $sids[1] }));
116
117         is($s->id, $sids[1], '... got a basic session id');
118
119         is($s->get('foo'), 'baz', '... got the foo value back successfully from session');
120
121         is_deeply( $s->dump, { foo => 'baz' }, '... got the session dump we expected');
122
123         $response_test->( $res, $sids[1] );
124     }
125
126     {
127         my($s, $res) = create_session($m, $env_cb->({ plack_session => $sids[0] }));
128
129         is($s->id, $sids[0], '... got a basic session id');
130
131         ok(!$s->get('foo'), '... no value stored for foo in session');
132
133         lives_ok {
134             $s->set( baz => 'gorch' );
135         } '... set the bar value successfully in session';
136
137         is_deeply( $s->dump, { bar => 'baz', baz => 'gorch' }, '... got the session dump we expected');
138
139         $response_test->( $res, $sids[0] );
140     }
141
142     {
143         my($s, $res) = create_session($m, $env_cb->({ plack_session => $sids[0] }));
144
145         is($s->get('bar'), 'baz', '... got the bar value back successfully from session');
146
147         lives_ok {
148             $s->expire;
149         } '... expired session successfully';
150
151         $response_test->( $res, $sids[0], 1 );
152
153         is_deeply( $s->dump, {}, '... got the session dump we expected');
154     }
155
156     {
157         my($s, $res) = create_session($m, $env_cb->({ plack_session => $sids[0] }));
158
159         push @sids, $s->id;
160         isnt($s->id, $sids[0], 'expired ... got a new session id');
161
162         ok(!$s->get('bar'), '... no bar value stored');
163
164         is_deeply( $s->dump, {}, '... got the session dump we expected');
165
166         $response_test->( $res, $sids[2] );
167     }
168
169     {
170         my($s, $res) = create_session($m, $env_cb->({ plack_session => $sids[1] }));
171
172         is($s->id, $sids[1], '... got a basic session id');
173
174         is($s->get('foo'), 'baz', '... got the foo value back successfully from session');
175
176         is_deeply( $s->dump, { foo => 'baz' }, '... got the session dump we expected');
177
178         $response_test->( $res, $sids[1] );
179     }
180
181     {
182         # wrong format session_id
183         my($s, $res) = create_session($m, $env_cb->({ plack_session => "../wrong" }));
184
185         isnt('../wrong' => $s->id, '... regenerate session id');
186
187         ok(!$s->get('foo'), '... no value stored for foo in session');
188
189         lives_ok {
190             $s->set( foo => 'baz' );
191         } '... set the value successfully';
192
193         is($s->get('foo'), 'baz', '... got the foo value back successfully from session');
194
195         is_deeply( $s->dump, { foo => 'baz' }, '... got the session dump we expected');
196
197         $response_test->( $res, $s->id );
198     }
199 }
200
201 1;