Middleware added, cookie state added, and example psgi app
[catagits/Web-Session.git] / t / 001_basic.t
CommitLineData
06190e8b 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;
13use Plack::Session::Store;
14
15sub 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
30my $storage = Plack::Session::Store->new;
31my $state = Plack::Session::State->new;
32
33{
bd992981 34 my $r = request();
35
06190e8b 36 my $s = Plack::Session->new(
37 state => $state,
38 store => $storage,
bd992981 39 request => $r,
06190e8b 40 );
41
42 is($s->id, 1, '... got a basic session id (1)');
43
44 ok(!$s->get('foo'), '... no value stored in foo for session (1)');
45
46 lives_ok {
47 $s->set( foo => 'bar' );
48 } '... set the value successfully in session (1)';
49
50 is($s->get('foo'), 'bar', '... got the foo value back successfully from session (1)');
51
bd992981 52 my $resp = $r->new_response;
53
06190e8b 54 lives_ok {
bd992981 55 $s->finalize( $resp );
06190e8b 56 } '... finalized session (1) successfully';
57}
58
59{
bd992981 60 my $r = request();
61
06190e8b 62 my $s = Plack::Session->new(
63 state => $state,
64 store => $storage,
bd992981 65 request => $r,
06190e8b 66 );
67
68 is($s->id, 2, '... got a basic session id (2)');
69
70 ok(!$s->get('foo'), '... no value stored for foo in session (2)');
71
72 lives_ok {
73 $s->set( foo => 'baz' );
74 } '... set the value successfully';
75
76 is($s->get('foo'), 'baz', '... got the foo value back successfully from session (2)');
77
bd992981 78 my $resp = $r->new_response;
79
06190e8b 80 lives_ok {
bd992981 81 $s->finalize( $resp );
06190e8b 82 } '... finalized session (2) successfully';
83}
84
85{
bd992981 86 my $r = request({ plack_session => 1 });
87
06190e8b 88 my $s = Plack::Session->new(
89 state => $state,
90 store => $storage,
bd992981 91 request => $r,
06190e8b 92 );
93
94 is($s->id, 1, '... got a basic session id (1)');
95
96 is($s->get('foo'), 'bar', '... got the value for foo back successfully from session (1)');
97
98 lives_ok {
99 $s->remove( 'foo' );
100 } '... removed the foo value successfully from session (1)';
101
102 ok(!$s->get('foo'), '... no value stored for foo in session (1)');
103
bd992981 104 my $resp = $r->new_response;
105
06190e8b 106 lives_ok {
bd992981 107 $s->finalize( $resp );
06190e8b 108 } '... finalized session (1) successfully';
109}
110
111
112{
bd992981 113 my $r = request({ plack_session => 2 });
114
06190e8b 115 my $s = Plack::Session->new(
116 state => $state,
117 store => $storage,
bd992981 118 request => $r,
06190e8b 119 );
120
121 is($s->id, 2, '... got a basic session id (2)');
122
123 is($s->get('foo'), 'baz', '... got the foo value back successfully from session (2)');
124
bd992981 125 my $resp = $r->new_response;
126
06190e8b 127 lives_ok {
bd992981 128 $s->finalize( $resp );
06190e8b 129 } '... finalized session (2) successfully';
130}
131
132{
bd992981 133 my $r = request({ plack_session => 1 });
134
06190e8b 135 my $s = Plack::Session->new(
136 state => $state,
137 store => $storage,
bd992981 138 request => $r,
06190e8b 139 );
140
141 is($s->id, 1, '... got a basic session id (1)');
142
143 ok(!$s->get('foo'), '... no value stored for foo in session (1)');
144
145 lives_ok {
146 $s->set( bar => 'baz' );
147 } '... set the bar value successfully in session (1)';
148
bd992981 149 my $resp = $r->new_response;
150
06190e8b 151 lives_ok {
bd992981 152 $s->finalize( $resp );
06190e8b 153 } '... finalized session (1) successfully';
154}
155
156{
bd992981 157 my $r = request({ plack_session => 1 });
158
06190e8b 159 my $s = Plack::Session->new(
160 state => $state,
161 store => $storage,
bd992981 162 request => $r,
06190e8b 163 );
164
165 is($s->id, 1, '... got a basic session id (1)');
166
167 is($s->get('bar'), 'baz', '... got the bar value back successfully from session (1)');
168
169 lives_ok {
170 $s->expire;
171 } '... expired session (1) successfully';
172}
173
174{
bd992981 175 my $r = request({ plack_session => 1 });
176
06190e8b 177 my $s = Plack::Session->new(
178 state => $state,
179 store => $storage,
bd992981 180 request => $r,
06190e8b 181 );
182
183 is($s->id, 3, '... got a new session id (3)');
184
185 ok(!$s->get('bar'), '... no bar value stored (from session (1)) in session (3)');
186}
187
188{
bd992981 189 my $r = request({ plack_session => 2 });
190
06190e8b 191 my $s = Plack::Session->new(
192 state => $state,
193 store => $storage,
bd992981 194 request => $r,
06190e8b 195 );
196
197 is($s->id, 2, '... got a basic session id (2)');
198
199 is($s->get('foo'), 'baz', '... got the foo value back successfully from session (2)');
200
bd992981 201 my $resp = $r->new_response;
202
06190e8b 203 lives_ok {
bd992981 204 $s->finalize( $resp );
06190e8b 205 } '... finalized session (2) successfully';
206}
207
208done_testing;