File: TestSession.pm

package info (click to toggle)
libplack-middleware-session-perl 0.36-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 276 kB
  • sloc: perl: 1,322; makefile: 2
file content (202 lines) | stat: -rw-r--r-- 5,527 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package TestSession;
use strict;
use warnings;

use Test::More;
use Test::Fatal qw(lives_ok);
use Plack::Middleware::Session;
use Plack::Session;

sub create_session {
    my($mw, $env) = @_;

    my $session;
    my $app = sub {
        my $env = shift;
        $session = Plack::Session->new($env);
        return sub {
            my $responder = shift;
            $responder->([ 200, [], [] ]);
        };
    };

    my $res = $mw->($app)->($env);

    return ($session, $res);
}

sub run_all_tests {
    my %params = @_;

    my (
        $env_cb,
        $state,
        $storage,
        $response_test
    ) = @params{qw[
        env_cb
        state
        store
        response_test
    ]};

    my $m = sub { Plack::Middleware::Session->wrap($_[0], state => $state, store => $storage) };

    $response_test ||= sub {
        my($res_cb, $session_id, $check_expired) = @_;
        $res_cb->(sub { my $res = shift });
    };

    my @sids;
    {
        my($s, $res) = create_session($m, $env_cb->());

        push @sids, $s->id;

        ok(!$s->get('foo'), '... no value stored in foo for session');

        lives_ok {
            $s->set( foo => 'bar' );
        } '... set the value successfully in session';

        is($s->get('foo'), 'bar', '... got the foo value back successfully from session');

        ok(!$s->get('bar'), '... no value stored in foo for session');

        lives_ok {
            $s->set( bar => 'baz' );
        } '... set the value successfully in session';

        is($s->get('bar'), 'baz', '... got the foo value back successfully from session');

        is_deeply( $s->dump, { foo => 'bar', bar => 'baz' }, '... got the session dump we expected');

        $response_test->($res, $sids[0]);
    }

    {
        my($s, $res) = create_session($m, $env_cb->());

        push @sids, $s->id;

        isnt($sids[0], $sids[1], "no same Session ID");
        ok(!$s->get('foo'), '... no value stored for foo in session');

        lives_ok {
            $s->set( foo => 'baz' );
        } '... set the value successfully';

        is($s->get('foo'), 'baz', '... got the foo value back successfully from session');

        is_deeply( $s->dump, { foo => 'baz' }, '... got the session dump we expected');

        $response_test->($res, $sids[1]);
    }

    {
        my($s, $res) = create_session($m, $env_cb->({ plack_session => $sids[0] }));
        is($s->id, $sids[0], '... got a basic session id');

        is($s->get('foo'), 'bar', '... got the value for foo back successfully from session');


        lives_ok {
            $s->remove( 'foo' );
        } '... removed the foo value successfully from session';

        ok(!$s->get('foo'), '... no value stored for foo in session');

        is_deeply( $s->dump, { bar => 'baz' }, '... got the session dump we expected');

        $response_test->( $res, $sids[0] );
    }


    {
        my($s, $res) = create_session($m, $env_cb->({ plack_session => $sids[1] }));

        is($s->id, $sids[1], '... got a basic session id');

        is($s->get('foo'), 'baz', '... got the foo value back successfully from session');

        is_deeply( $s->dump, { foo => 'baz' }, '... got the session dump we expected');

        $response_test->( $res, $sids[1] );
    }

    {
        my($s, $res) = create_session($m, $env_cb->({ plack_session => $sids[0] }));

        is($s->id, $sids[0], '... got a basic session id');

        ok(!$s->get('foo'), '... no value stored for foo in session');

        lives_ok {
            $s->set( baz => 'gorch' );
        } '... set the bar value successfully in session';

        is_deeply( $s->dump, { bar => 'baz', baz => 'gorch' }, '... got the session dump we expected');

        $response_test->( $res, $sids[0] );
    }

    {
        my($s, $res) = create_session($m, $env_cb->({ plack_session => $sids[0] }));

        is($s->get('bar'), 'baz', '... got the bar value back successfully from session');

        lives_ok {
            $s->expire;
        } '... expired session successfully';

        $response_test->( $res, $sids[0], 1 );

        is_deeply( $s->dump, {}, '... got the session dump we expected');
    }

    {
        my($s, $res) = create_session($m, $env_cb->({ plack_session => $sids[0] }));

        push @sids, $s->id;
        isnt($s->id, $sids[0], 'expired ... got a new session id');

        ok(!$s->get('bar'), '... no bar value stored');

        is_deeply( $s->dump, {}, '... got the session dump we expected');

        $response_test->( $res, $sids[2] );
    }

    {
        my($s, $res) = create_session($m, $env_cb->({ plack_session => $sids[1] }));

        is($s->id, $sids[1], '... got a basic session id');

        is($s->get('foo'), 'baz', '... got the foo value back successfully from session');

        is_deeply( $s->dump, { foo => 'baz' }, '... got the session dump we expected');

        $response_test->( $res, $sids[1] );
    }

    {
        # wrong format session_id
        my($s, $res) = create_session($m, $env_cb->({ plack_session => "../wrong" }));

        isnt('../wrong' => $s->id, '... regenerate session id');

        ok(!$s->get('foo'), '... no value stored for foo in session');

        lives_ok {
            $s->set( foo => 'baz' );
        } '... set the value successfully';

        is($s->get('foo'), 'baz', '... got the foo value back successfully from session');

        is_deeply( $s->dump, { foo => 'baz' }, '... got the session dump we expected');

        $response_test->( $res, $s->id );
    }
}

1;