as minimalist as I could possibly be
[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 $s = Plack::Session->new(
35         state   => $state,
36         store   => $storage,
37         request => request(),
38     );
39
40     is($s->id, 1, '... got a basic session id (1)');
41
42     ok(!$s->get('foo'), '... no value stored in foo for session (1)');
43
44     lives_ok {
45         $s->set( foo => 'bar' );
46     } '... set the value successfully in session (1)';
47
48     is($s->get('foo'), 'bar', '... got the foo value back successfully from session (1)');
49
50     lives_ok {
51         $s->finalize;
52     } '... finalized session (1) successfully';
53 }
54
55 {
56     my $s = Plack::Session->new(
57         state   => $state,
58         store   => $storage,
59         request => request(),
60     );
61
62     is($s->id, 2, '... got a basic session id (2)');
63
64     ok(!$s->get('foo'), '... no value stored for foo in session (2)');
65
66     lives_ok {
67         $s->set( foo => 'baz' );
68     } '... set the value successfully';
69
70     is($s->get('foo'), 'baz', '... got the foo value back successfully from session (2)');
71
72     lives_ok {
73         $s->finalize;
74     } '... finalized session (2) successfully';
75 }
76
77 {
78     my $s = Plack::Session->new(
79         state   => $state,
80         store   => $storage,
81         request => request({ plack_session => 1 }),
82     );
83
84     is($s->id, 1, '... got a basic session id (1)');
85
86     is($s->get('foo'), 'bar', '... got the value for foo back successfully from session (1)');
87
88     lives_ok {
89         $s->remove( 'foo' );
90     } '... removed the foo value successfully from session (1)';
91
92     ok(!$s->get('foo'), '... no value stored for foo in session (1)');
93
94     lives_ok {
95         $s->finalize;
96     } '... finalized session (1) successfully';
97 }
98
99
100 {
101     my $s = Plack::Session->new(
102         state   => $state,
103         store   => $storage,
104         request => request({ plack_session => 2 }),
105     );
106
107     is($s->id, 2, '... got a basic session id (2)');
108
109     is($s->get('foo'), 'baz', '... got the foo value back successfully from session (2)');
110
111     lives_ok {
112         $s->finalize;
113     } '... finalized session (2) successfully';
114 }
115
116 {
117     my $s = Plack::Session->new(
118         state   => $state,
119         store   => $storage,
120         request => request({ plack_session => 1 }),
121     );
122
123     is($s->id, 1, '... got a basic session id (1)');
124
125     ok(!$s->get('foo'), '... no value stored for foo in session (1)');
126
127     lives_ok {
128         $s->set( bar => 'baz' );
129     } '... set the bar value successfully in session (1)';
130
131     lives_ok {
132         $s->finalize;
133     } '... finalized session (1) successfully';
134 }
135
136 {
137     my $s = Plack::Session->new(
138         state   => $state,
139         store   => $storage,
140         request => request({ plack_session => 1 }),
141     );
142
143     is($s->id, 1, '... got a basic session id (1)');
144
145     is($s->get('bar'), 'baz', '... got the bar value back successfully from session (1)');
146
147     lives_ok {
148         $s->expire;
149     } '... expired session (1) successfully';
150 }
151
152 {
153     my $s = Plack::Session->new(
154         state   => $state,
155         store   => $storage,
156         request => request({ plack_session => 1 }),
157     );
158
159     is($s->id, 3, '... got a new session id (3)');
160
161     ok(!$s->get('bar'), '... no bar value stored (from session (1)) in session (3)');
162 }
163
164 {
165     my $s = Plack::Session->new(
166         state   => $state,
167         store   => $storage,
168         request => request({ plack_session => 2 }),
169     );
170
171     is($s->id, 2, '... got a basic session id (2)');
172
173     is($s->get('foo'), 'baz', '... got the foo value back successfully from session (2)');
174
175     lives_ok {
176         $s->finalize;
177     } '... finalized session (2) successfully';
178 }
179
180 done_testing;