Middleware added, cookie state added, and example psgi app
[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 {
34     my $r = request();
35
36     my $s = Plack::Session->new(
37         state   => $state,
38         store   => $storage,
39         request => $r,
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
52     my $resp = $r->new_response;
53
54     lives_ok {
55         $s->finalize( $resp );
56     } '... finalized session (1) successfully';
57 }
58
59 {
60     my $r = request();
61
62     my $s = Plack::Session->new(
63         state   => $state,
64         store   => $storage,
65         request => $r,
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
78     my $resp = $r->new_response;
79
80     lives_ok {
81         $s->finalize( $resp );
82     } '... finalized session (2) successfully';
83 }
84
85 {
86     my $r = request({ plack_session => 1 });
87
88     my $s = Plack::Session->new(
89         state   => $state,
90         store   => $storage,
91         request => $r,
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
104     my $resp = $r->new_response;
105
106     lives_ok {
107         $s->finalize( $resp );
108     } '... finalized session (1) successfully';
109 }
110
111
112 {
113     my $r = request({ plack_session => 2 });
114
115     my $s = Plack::Session->new(
116         state   => $state,
117         store   => $storage,
118         request => $r,
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
125     my $resp = $r->new_response;
126
127     lives_ok {
128         $s->finalize( $resp );
129     } '... finalized session (2) successfully';
130 }
131
132 {
133     my $r = request({ plack_session => 1 });
134
135     my $s = Plack::Session->new(
136         state   => $state,
137         store   => $storage,
138         request => $r,
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
149     my $resp = $r->new_response;
150
151     lives_ok {
152         $s->finalize( $resp );
153     } '... finalized session (1) successfully';
154 }
155
156 {
157     my $r = request({ plack_session => 1 });
158
159     my $s = Plack::Session->new(
160         state   => $state,
161         store   => $storage,
162         request => $r,
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 {
175     my $r = request({ plack_session => 1 });
176
177     my $s = Plack::Session->new(
178         state   => $state,
179         store   => $storage,
180         request => $r,
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 {
189     my $r = request({ plack_session => 2 });
190
191     my $s = Plack::Session->new(
192         state   => $state,
193         store   => $storage,
194         request => $r,
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
201     my $resp = $r->new_response;
202
203     lives_ok {
204         $s->finalize( $resp );
205     } '... finalized session (2) successfully';
206 }
207
208 done_testing;