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