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