Fixed I generation to use SHA1 instead of sequential. Added sane defaults to Middleware.
[catagits/Web-Session.git] / t / 001_basic.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7 use Test::Exception;
8
9 use Plack::Request;
10
11 use Plack::Session;
12 use Plack::Session::State;
13 use Plack::Session::Store;
14
15 sub request {
16     open my $in, '<', \do { my $d };
17     my $env = {
18         'psgi.version'    => [ 1, 0 ],
19         'psgi.input'      => $in,
20         'psgi.errors'     => *STDERR,
21         'psgi.url_scheme' => 'http',
22         SERVER_PORT       => 80,
23         REQUEST_METHOD    => 'GET',
24     };
25     my $r = Plack::Request->new( $env );
26     $r->parameters( @_ );
27     $r;
28 }
29
30 my $storage = Plack::Session::Store->new;
31 my $state   = Plack::Session::State->new;
32
33 my @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 sessio successfully';
58 }
59
60 {
61     my $r = request();
62
63     my $s = Plack::Session->new(
64         state   => $state,
65         store   => $storage,
66         request => $r,
67     );
68
69     push @sids, $s->id;
70
71     isnt($sids[0], $sids[1], "no same Session ID");
72     ok(!$s->get('foo'), '... no value stored for foo in session');
73
74     lives_ok {
75         $s->set( foo => 'baz' );
76     } '... set the value successfully';
77
78     is($s->get('foo'), 'baz', '... got the foo value back successfully from session');
79
80     my $resp = $r->new_response;
81
82     lives_ok {
83         $s->finalize( $resp );
84     } '... finalized session successfully';
85 }
86
87 {
88     my $r = request({ plack_session => $sids[0] });
89
90     my $s = Plack::Session->new(
91         state   => $state,
92         store   => $storage,
93         request => $r,
94     );
95
96     is($s->id, $sids[0], '... got a basic session id');
97
98     is($s->get('foo'), 'bar', '... got the value for foo back successfully from session');
99
100     lives_ok {
101         $s->remove( 'foo' );
102     } '... removed the foo value successfully from session';
103
104     ok(!$s->get('foo'), '... no value stored for foo in session');
105
106     my $resp = $r->new_response;
107
108     lives_ok {
109         $s->finalize( $resp );
110     } '... finalized session successfully';
111 }
112
113
114 {
115     my $r = request({ 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
134 {
135     my $r = request({ plack_session => $sids[0] });
136
137     my $s = Plack::Session->new(
138         state   => $state,
139         store   => $storage,
140         request => $r,
141     );
142
143     is($s->id, $sids[0], '... got a basic session id');
144
145     ok(!$s->get('foo'), '... no value stored for foo in session');
146
147     lives_ok {
148         $s->set( bar => 'baz' );
149     } '... set the bar value successfully in session';
150
151     my $resp = $r->new_response;
152
153     lives_ok {
154         $s->finalize( $resp );
155     } '... finalized session successfully';
156 }
157
158 {
159     my $r = request({ plack_session => $sids[0] });
160
161     my $s = Plack::Session->new(
162         state   => $state,
163         store   => $storage,
164         request => $r,
165     );
166
167     is($s->get('bar'), 'baz', '... got the bar value back successfully from session');
168
169     lives_ok {
170         $s->expire;
171     } '... expired session successfully';
172 }
173
174 {
175     my $r = request({ plack_session => $sids[0] });
176
177     my $s = Plack::Session->new(
178         state   => $state,
179         store   => $storage,
180         request => $r,
181     );
182
183     push @sids, $s->id;
184     isnt($s->id, $sids[0], 'expired ... got a new session id');
185
186     ok(!$s->get('bar'), '... no bar value stored');
187 }
188
189 {
190     my $r = request({ plack_session => $sids[1] });
191
192     my $s = Plack::Session->new(
193         state   => $state,
194         store   => $storage,
195         request => $r,
196     );
197
198     is($s->id, $sids[1], '... got a basic session id');
199
200     is($s->get('foo'), 'baz', '... got the foo value back successfully from session');
201
202     my $resp = $r->new_response;
203
204     lives_ok {
205         $s->finalize( $resp );
206     } '... finalized session successfully';
207 }
208
209 done_testing;