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