51c22782e50308f997b8594d5187e97d27602dbc
[catagits/Web-Session.git] / t / 013_cookiestore.t
1 use strict;
2 use Test::More;
3 use Test::Requires qw(Digest::HMAC_SHA1);
4 use Plack::Test;
5 use Plack::Middleware::Session::Cookie;
6 use HTTP::Request::Common;
7 use LWP::UserAgent;
8 use HTTP::Cookies;
9
10 $Plack::Test::Impl = 'Server';
11
12 my $app = sub {
13     my $env = shift;
14     my $session = $env->{'psgix.session'};
15
16     my $counter = $session->{counter} || 0;
17     if ($session->{counter}++ >= 2) {
18         $env->{'psgix.session.options'}->{expire} = 1;
19     }
20
21     return [ 200, [], [ "counter=$counter" ] ];
22 };
23
24 $app = Plack::Middleware::Session::Cookie->wrap($app, secret => "foobar", expires => 3600);
25
26 my $ua = LWP::UserAgent->new;
27 $ua->cookie_jar( HTTP::Cookies->new );
28
29 test_psgi ua => $ua, app => $app, client => sub {
30     my $cb = shift;
31
32     my $res = $cb->(GET "/");
33     is $res->content, "counter=0";
34     like $res->header('Set-Cookie'), qr/expires=/;
35
36     $res = $cb->(GET "/");
37     is $res->content, "counter=1";
38     like $res->header('Set-Cookie'), qr/expires=/;
39
40     $res = $cb->(GET "/");
41     is $res->content, "counter=2";
42
43     $res = $cb->(GET "/");
44     is $res->content, "counter=0";
45 };
46
47 done_testing;