File: 013_cookiestore.t

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 (48 lines) | stat: -rw-r--r-- 1,161 bytes parent folder | download | duplicates (8)
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
use strict;
use Test::More;
use Test::Requires qw(Digest::HMAC_SHA1);
use Plack::Test;
use Plack::Middleware::Session::Cookie;
use HTTP::Request::Common;
use LWP::UserAgent;
use HTTP::Cookies;

$Plack::Test::Impl = 'Server';

my $app = sub {
    my $env = shift;
    my $session = $env->{'psgix.session'};

    my $counter = $session->{counter} || 0;
    if ($session->{counter}++ >= 2) {
        $env->{'psgix.session.options'}->{expire} = 1;
    }

    return [ 200, [], [ "counter=$counter" ] ];
};

$app = Plack::Middleware::Session::Cookie->wrap($app, secret => "foobar", expires => 3600);

my $ua = LWP::UserAgent->new;
$ua->cookie_jar( HTTP::Cookies->new );

test_psgi ua => $ua, app => $app, client => sub {
    my $cb = shift;

    my $res = $cb->(GET "/");
    is $res->content, "counter=0";
    like $res->header('Set-Cookie'), qr/expires=/;
    like $res->header('Set-Cookie'), qr/path=\//;

    $res = $cb->(GET "/");
    is $res->content, "counter=1";
    like $res->header('Set-Cookie'), qr/expires=/;

    $res = $cb->(GET "/");
    is $res->content, "counter=2";

    $res = $cb->(GET "/");
    is $res->content, "counter=0";
};

done_testing;