Commit | Line | Data |
f331b3e0 |
1 | package t::lib::TestSession; |
2 | use strict; |
3 | use warnings; |
4 | |
5 | use Test::More; |
6 | use Test::Exception; |
7 | |
8 | sub run_all_tests { |
9 | my %params = @_; |
10 | |
11 | my ( |
12 | $request_creator, |
13 | $state, |
14 | $storage, |
15 | $response_test |
16 | ) = @params{qw[ |
17 | request_creator |
18 | state |
19 | store |
20 | response_test |
21 | ]}; |
22 | |
23 | $response_test = sub { |
24 | my ($response, $session_id, $check_expired) = @_; |
25 | }; |
26 | |
27 | my @sids; |
28 | { |
29 | my $r = $request_creator->(); |
30 | |
31 | my $s = Plack::Session->new( |
32 | state => $state, |
33 | store => $storage, |
34 | request => $r, |
35 | ); |
36 | |
37 | push @sids, $s->id; |
38 | |
39 | ok(!$s->get('foo'), '... no value stored in foo for session'); |
40 | |
41 | lives_ok { |
42 | $s->set( foo => 'bar' ); |
43 | } '... set the value successfully in session'; |
44 | |
45 | is($s->get('foo'), 'bar', '... got the foo value back successfully from session'); |
46 | |
47 | my $resp = $r->new_response; |
48 | |
49 | lives_ok { |
50 | $s->finalize( $resp ); |
51 | } '... finalized session successfully'; |
52 | |
53 | $response_test->( $resp, $sids[0] ); |
54 | } |
55 | |
56 | { |
57 | my $r = $request_creator->(); |
58 | |
59 | my $s = Plack::Session->new( |
60 | state => $state, |
61 | store => $storage, |
62 | request => $r, |
63 | ); |
64 | |
65 | push @sids, $s->id; |
66 | |
67 | isnt($sids[0], $sids[1], "no same Session ID"); |
68 | ok(!$s->get('foo'), '... no value stored for foo in session'); |
69 | |
70 | lives_ok { |
71 | $s->set( foo => 'baz' ); |
72 | } '... set the value successfully'; |
73 | |
74 | is($s->get('foo'), 'baz', '... got the foo value back successfully from session'); |
75 | |
76 | my $resp = $r->new_response; |
77 | |
78 | lives_ok { |
79 | $s->finalize( $resp ); |
80 | } '... finalized session successfully'; |
81 | |
82 | $response_test->( $resp, $sids[1] ); |
83 | } |
84 | |
85 | { |
86 | my $r = $request_creator->({ plack_session => $sids[0] }); |
87 | |
88 | my $s = Plack::Session->new( |
89 | state => $state, |
90 | store => $storage, |
91 | request => $r, |
92 | ); |
93 | |
94 | is($s->id, $sids[0], '... got a basic session id'); |
95 | |
96 | is($s->get('foo'), 'bar', '... got the value for foo back successfully from session'); |
97 | |
98 | lives_ok { |
99 | $s->remove( 'foo' ); |
100 | } '... removed the foo value successfully from session'; |
101 | |
102 | ok(!$s->get('foo'), '... no value stored for foo in session'); |
103 | |
104 | my $resp = $r->new_response; |
105 | |
106 | lives_ok { |
107 | $s->finalize( $resp ); |
108 | } '... finalized session successfully'; |
109 | |
110 | $response_test->( $resp, $sids[0] ); |
111 | } |
112 | |
113 | |
114 | { |
115 | my $r = $request_creator->({ plack_session => $sids[1] }); |
116 | |
117 | my $s = Plack::Session->new( |
118 | state => $state, |
119 | store => $storage, |
120 | request => $r, |
121 | ); |
122 | |
123 | is($s->id, $sids[1], '... got a basic session id'); |
124 | |
125 | is($s->get('foo'), 'baz', '... got the foo value back successfully from session'); |
126 | |
127 | my $resp = $r->new_response; |
128 | |
129 | lives_ok { |
130 | $s->finalize( $resp ); |
131 | } '... finalized session successfully'; |
132 | |
133 | $response_test->( $resp, $sids[1] ); |
134 | } |
135 | |
136 | { |
137 | my $r = $request_creator->({ plack_session => $sids[0] }); |
138 | |
139 | my $s = Plack::Session->new( |
140 | state => $state, |
141 | store => $storage, |
142 | request => $r, |
143 | ); |
144 | |
145 | is($s->id, $sids[0], '... got a basic session id'); |
146 | |
147 | ok(!$s->get('foo'), '... no value stored for foo in session'); |
148 | |
149 | lives_ok { |
150 | $s->set( bar => 'baz' ); |
151 | } '... set the bar value successfully in session'; |
152 | |
153 | my $resp = $r->new_response; |
154 | |
155 | lives_ok { |
156 | $s->finalize( $resp ); |
157 | } '... finalized session successfully'; |
158 | |
159 | $response_test->( $resp, $sids[0] ); |
160 | } |
161 | |
162 | { |
163 | my $r = $request_creator->({ plack_session => $sids[0] }); |
164 | |
165 | my $s = Plack::Session->new( |
166 | state => $state, |
167 | store => $storage, |
168 | request => $r, |
169 | ); |
170 | |
171 | is($s->get('bar'), 'baz', '... got the bar value back successfully from session'); |
172 | |
173 | lives_ok { |
174 | $s->expire; |
175 | } '... expired session successfully'; |
176 | |
177 | my $resp = $r->new_response; |
178 | |
179 | lives_ok { |
180 | $s->finalize( $resp ); |
181 | } '... finalized session successfully'; |
182 | |
183 | $response_test->( $resp, $sids[0], 1 ); |
184 | } |
185 | |
186 | { |
187 | my $r = $request_creator->({ plack_session => $sids[0] }); |
188 | |
189 | my $s = Plack::Session->new( |
190 | state => $state, |
191 | store => $storage, |
192 | request => $r, |
193 | ); |
194 | |
195 | push @sids, $s->id; |
196 | isnt($s->id, $sids[0], 'expired ... got a new session id'); |
197 | |
198 | ok(!$s->get('bar'), '... no bar value stored'); |
199 | |
200 | my $resp = $r->new_response; |
201 | |
202 | lives_ok { |
203 | $s->finalize( $resp ); |
204 | } '... finalized session successfully'; |
205 | |
206 | $response_test->( $resp, $sids[2] ); |
207 | } |
208 | |
209 | { |
210 | my $r = $request_creator->({ plack_session => $sids[1] }); |
211 | |
212 | my $s = Plack::Session->new( |
213 | state => $state, |
214 | store => $storage, |
215 | request => $r, |
216 | ); |
217 | |
218 | is($s->id, $sids[1], '... got a basic session id'); |
219 | |
220 | is($s->get('foo'), 'baz', '... got the foo value back successfully from session'); |
221 | |
222 | my $resp = $r->new_response; |
223 | |
224 | lives_ok { |
225 | $s->finalize( $resp ); |
226 | } '... finalized session successfully'; |
227 | |
228 | $response_test->( $resp, $sids[1] ); |
229 | } |
56b9910a |
230 | |
231 | { |
232 | # wrong format session_id |
233 | my $r = $request_creator->({ plack_session => '../wrong' }); |
234 | |
235 | my $s = Plack::Session->new( |
236 | state => $state, |
237 | store => $storage, |
238 | request => $r, |
239 | ); |
240 | |
241 | |
242 | isnt('../wrong' => $s->id, '... regenerate session id'); |
243 | |
244 | ok(!$s->get('foo'), '... no value stored for foo in session'); |
245 | |
246 | lives_ok { |
247 | $s->set( foo => 'baz' ); |
248 | } '... set the value successfully'; |
249 | |
250 | is($s->get('foo'), 'baz', '... got the foo value back successfully from session'); |
251 | |
252 | my $resp = $r->new_response; |
253 | |
254 | lives_ok { |
255 | $s->finalize( $resp ); |
256 | } '... finalized session successfully'; |
257 | |
258 | $response_test->( $resp, $s ); |
259 | } |
f331b3e0 |
260 | } |
261 | |
56b9910a |
262 | 1; |