rename dist to Plack::Middleware::Session for consistency
[catagits/Web-Session.git] / t / 002_basic_w_cookie.t
CommitLineData
05b5f99d 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use Test::More;
7use Test::Exception;
8
9use Plack::Request;
10
11use Plack::Session;
12use Plack::Session::State::Cookie;
13use Plack::Session::Store;
14
15sub request {
16 my $cookies = shift;
17 open my $in, '<', \do { my $d };
18 my $env = {
19 'psgi.version' => [ 1, 0 ],
20 'psgi.input' => $in,
21 'psgi.errors' => *STDERR,
22 'psgi.url_scheme' => 'http',
23 SERVER_PORT => 80,
24 REQUEST_METHOD => 'GET',
25 HTTP_COOKIE => join "; " => map { $_ . "=" . $cookies->{ $_ } } keys %$cookies,
26 };
27 return Plack::Request->new( $env );
28}
29
30my $storage = Plack::Session::Store->new;
31my $state = Plack::Session::State::Cookie->new;
32
33my @sids;
34{
35 my $r = request();
36
37 my $s = Plack::Session->new(
38 state => $state,
39 store => $storage,
40 request => $r,
41 );
42
43 push @sids, $s->id;
44
45 ok(!$s->get('foo'), '... no value stored in foo for session');
46
47 lives_ok {
48 $s->set( foo => 'bar' );
49 } '... set the value successfully in session';
50
51 is($s->get('foo'), 'bar', '... got the foo value back successfully from session');
52
53 my $resp = $r->new_response;
54
55 lives_ok {
56 $s->finalize( $resp );
57 } '... finalized session successfully';
58
59 is_deeply(
60 $resp->cookies,
61 {
62 plack_session => {
63 value => $sids[0],
64 path => '/'
65 }
66 },
67 '... got the right cookies in the response'
68 );
69}
70
71{
72 my $r = request();
73
74 my $s = Plack::Session->new(
75 state => $state,
76 store => $storage,
77 request => $r,
78 );
79
80 push @sids, $s->id;
81
82 isnt($sids[0], $sids[1], "... not the 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 my $resp = $r->new_response;
92
93 lives_ok {
94 $s->finalize( $resp );
95 } '... finalized session successfully';
96
97 is_deeply(
98 $resp->cookies,
99 {
100 plack_session => {
101 value => $sids[1],
102 path => '/'
103 }
104 },
105 '... got the right cookies in the response'
106 );
107}
108
109{
110 my $r = request({ plack_session => $sids[0] });
111
112 my $s = Plack::Session->new(
113 state => $state,
114 store => $storage,
115 request => $r,
116 );
117
118 is($s->id, $sids[0], '... got a basic session id');
119
120 is($s->get('foo'), 'bar', '... got the value for foo back successfully from session');
121
122 lives_ok {
123 $s->remove( 'foo' );
124 } '... removed the foo value successfully from session';
125
126 ok(!$s->get('foo'), '... no value stored for foo in session');
127
128 my $resp = $r->new_response;
129
130 lives_ok {
131 $s->finalize( $resp );
132 } '... finalized session successfully';
133
134 is_deeply(
135 $resp->cookies,
136 {
137 plack_session => {
138 value => $sids[0],
139 path => '/'
140 }
141 },
142 '... got the right cookies in the response'
143 );
144}
145
146{
147 my $r = request({ plack_session => $sids[1] });
148
149 my $s = Plack::Session->new(
150 state => $state,
151 store => $storage,
152 request => $r,
153 );
154
155 is($s->id, $sids[1], '... got a basic session id');
156
157 is($s->get('foo'), 'baz', '... got the foo value back successfully from session');
158
159 my $resp = $r->new_response;
160
161 lives_ok {
162 $s->finalize( $resp );
163 } '... finalized session successfully';
164
165 is_deeply(
166 $resp->cookies,
167 {
168 plack_session => {
169 value => $sids[1],
170 path => '/'
171 }
172 },
173 '... got the right cookies in the response'
174 );
175}
176
177{
178 my $r = request({ plack_session => $sids[0] });
179
180 my $s = Plack::Session->new(
181 state => $state,
182 store => $storage,
183 request => $r,
184 );
185
186 is($s->id, $sids[0], '... got a basic session id');
187
188 ok(!$s->get('foo'), '... no value stored for foo in session');
189
190 lives_ok {
191 $s->set( bar => 'baz' );
192 } '... set the bar value successfully in session';
193
194 my $resp = $r->new_response;
195
196 lives_ok {
197 $s->finalize( $resp );
198 } '... finalized session successfully';
199
200 is_deeply(
201 $resp->cookies,
202 {
203 plack_session => {
204 value => $sids[0],
205 path => '/'
206 }
207 },
208 '... got the right cookies in the response'
209 );
210}
211
212{
213 my $r = request({ plack_session => $sids[0] });
214
215 my $s = Plack::Session->new(
216 state => $state,
217 store => $storage,
218 request => $r,
219 );
220
221 is($s->get('bar'), 'baz', '... got the bar value back successfully from session');
222
223 lives_ok {
224 $s->expire;
225 } '... expired session successfully';
226
227 my $resp = $r->new_response;
228
229 lives_ok {
230 $s->finalize( $resp );
231 } '... finalized session successfully';
232
233 is_deeply(
234 $resp->cookies,
235 {
236 plack_session => {
237 value => $sids[0],
238 path => '/',
239 expires => '0'
240 }
241 },
242 '... got the right cookies in the response'
243 );
244}
245
246{
247 my $r = request({ plack_session => $sids[0] });
248
249 my $s = Plack::Session->new(
250 state => $state,
251 store => $storage,
252 request => $r,
253 );
254
255 push @sids, $s->id;
256 isnt($s->id, $sids[0], 'expired ... got a new session id');
257
258 ok(!$s->get('bar'), '... no bar value stored');
259
260 my $resp = $r->new_response;
261
262 lives_ok {
263 $s->finalize( $resp );
264 } '... finalized session successfully';
265
266 is_deeply(
267 $resp->cookies,
268 {
269 plack_session => {
270 value => $sids[2],
271 path => '/',
272 }
273 },
274 '... got the right cookies in the response'
275 );
276}
277
278{
279 my $r = request({ plack_session => $sids[1] });
280
281 my $s = Plack::Session->new(
282 state => $state,
283 store => $storage,
284 request => $r,
285 );
286
287 is($s->id, $sids[1], '... got a basic session id');
288
289 is($s->get('foo'), 'baz', '... got the foo value back successfully from session');
290
291 my $resp = $r->new_response;
292
293 lives_ok {
294 $s->finalize( $resp );
295 } '... finalized session successfully';
296
297 is_deeply(
298 $resp->cookies,
299 {
300 plack_session => {
301 value => $sids[1],
302 path => '/',
303 }
304 },
305 '... got the right cookies in the response'
306 );
307}
308
309done_testing;