use Test::Fatal instead of Test::Exception. Since Test::Exception have a XS deps.
[catagits/Web-Session.git] / t / lib / TestSessionHash.pm
CommitLineData
7ea446f1 1package t::lib::TestSessionHash;
2use strict;
3use warnings;
4
5use Test::More;
91d1d9de 6use Test::Fatal qw(lives_ok);
7ea446f1 7use Plack::Middleware::Session;
8
9sub create_session {
10 my($mw, $env) = @_;
11
12 my ($session, $session_options);
13 my $app = sub {
14 my $env = shift;
15 $session = $env->{'psgix.session'};
16 $session_options = $env->{'psgix.session.options'};
17 return sub {
18 my $responder = shift;
19 $responder->([ 200, [], [] ]);
20 };
21 };
22
23 my $res = $mw->($app)->($env);
24
25 return ($session, $session_options, $res);
26}
27
28sub run_all_tests {
29 my %params = @_;
30
31 my (
32 $env_cb,
33 $state,
34 $storage,
35 $response_test,
36 $middleware_create_cb
37 ) = @params{qw[
38 env_cb
39 state
40 store
41 response_test
42 middleware_create_cb
43 ]};
44
45 my $m = $middleware_create_cb
46 || sub { Plack::Middleware::Session->wrap($_[0], state => $state, store => $storage) };
47
48 $response_test ||= sub {
49 my($res_cb, $session_id, $check_expired) = @_;
50 $res_cb->(sub { my $res = shift });
51 };
52
53 my @sids;
54 {
55 my($s, $opts, $res) = create_session($m, $env_cb->());
56
57 push @sids, $opts->{id};
58
59 ok(!$s->{'foo'}, '... no value stored in foo for session');
60
61 lives_ok {
62 $s->{foo} = 'bar';
63 } '... set the value successfully in session';
64
65 is($s->{'foo'}, 'bar', '... got the foo value back successfully from session');
66
67 ok(!$s->{'bar'}, '... no value stored in foo for session');
68
69 lives_ok {
70 $s->{bar} = 'baz';
71 } '... set the value successfully in session';
72
73 is($s->{'bar'}, 'baz', '... got the foo value back successfully from session');
74
75 is_deeply( $s, { foo => 'bar', bar => 'baz' }, '... got the session dump we expected');
76
77 $response_test->($res, $sids[0]);
78 }
79
80 {
81 my($s, $opts, $res) = create_session($m, $env_cb->());
82
83 push @sids, $opts->{id};
84
85 isnt($sids[0], $sids[1], "no same Session ID");
86 ok(!$s->{'foo'}, '... no value stored for foo in session');
87
88 lives_ok {
89 $s->{foo} = 'baz';
90 } '... set the value successfully';
91
92 is($s->{'foo'}, 'baz', '... got the foo value back successfully from session');
93
94 is_deeply( $s, { foo => 'baz' }, '... got the session dump we expected');
95
96 $response_test->($res, $sids[1]);
97 }
98
99 {
100 my($s, $opts, $res) = create_session($m, $env_cb->({ plack_session => $sids[0] }));
101 is($opts->{id}, $sids[0], '... got a basic session id');
102
103 is($s->{'foo'}, 'bar', '... got the value for foo back successfully from session');
104
105
106 lives_ok {
107 delete $s->{'foo'};
108 } '... removed the foo value successfully from session';
109
110 ok(!$s->{'foo'}, '... no value stored for foo in session');
111
112 is_deeply( $s, { bar => 'baz' }, '... got the session dump we expected');
113
114 $response_test->( $res, $sids[0] );
115 }
116
117
118 {
119 my($s, $opts, $res) = create_session($m, $env_cb->({ plack_session => $sids[1] }));
120
121 is($opts->{id}, $sids[1], '... got a basic session id');
122
123 is($s->{'foo'}, 'baz', '... got the foo value back successfully from session');
124
125 is_deeply( $s, { foo => 'baz' }, '... got the session dump we expected');
126
127 $response_test->( $res, $sids[1] );
128 }
129
130 {
131 my($s, $opts, $res) = create_session($m, $env_cb->({ plack_session => $sids[0] }));
132
133 is($opts->{id}, $sids[0], '... got a basic session id');
134
135 ok(!$s->{'foo'}, '... no value stored for foo in session');
136
137 lives_ok {
138 $s->{baz} = 'gorch';
139 } '... set the bar value successfully in session';
140
141 is_deeply( $s, { bar => 'baz', baz => 'gorch' }, '... got the session dump we expected');
142
143 $response_test->( $res, $sids[0] );
144 }
145
146 {
147 my($s, $opts, $res) = create_session($m, $env_cb->({ plack_session => $sids[0] }));
148
149 is($s->{'bar'}, 'baz', '... got the bar value back successfully from session');
150
151 lives_ok {
152 $opts->{expire} = 1;
153 } '... expired session successfully';
154
155 $response_test->( $res, $sids[0], 1 );
156
157 # XXX
158 # this will not pass, because
159 # it is just a hash ref and we are
160 # not clearing it. Should we be?
161 # - SL
162 # is_deeply( $s, {}, '... got the session dump we expected');
163 }
164
165 {
166 my($s, $opts, $res) = create_session($m, $env_cb->({ plack_session => $sids[0] }));
167
168 push @sids, $opts->{id};
169 isnt($opts->{id}, $sids[0], 'expired ... got a new session id');
170
171 ok(!$s->{'bar'}, '... no bar value stored');
172
173 is_deeply( $s, {}, '... got the session dump we expected');
174
175 $response_test->( $res, $sids[2] );
176 }
177
178 {
179 my($s, $opts, $res) = create_session($m, $env_cb->({ plack_session => $sids[1] }));
180
181 is($opts->{id}, $sids[1], '... got a basic session id');
182
183 is($s->{'foo'}, 'baz', '... got the foo value back successfully from session');
184
185 is_deeply( $s, { foo => 'baz' }, '... got the session dump we expected');
186
187 $response_test->( $res, $sids[1] );
188 }
189
190 {
191 # wrong format session_id
192 my($s, $opts, $res) = create_session($m, $env_cb->({ plack_session => "../wrong" }));
193
194 isnt('../wrong' => $opts->{id}, '... regenerate session id');
195
196 ok(!$s->{'foo'}, '... no value stored for foo in session');
197
198 lives_ok {
199 $s->{foo} = 'baz';
200 } '... set the value successfully';
201
202 is($s->{'foo'}, 'baz', '... got the foo value back successfully from session');
203
204 is_deeply( $s, { foo => 'baz' }, '... got the session dump we expected');
205
206 $response_test->( $res, $opts->{id} );
207 }
208}
209
2101;