Added Session::Cookie middleware which utilizes Cookie as both state and store.
[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->get('counter') || 0;
17     if ($counter >= 2) {
18         $session->expire;
19     } else {
20         $session->set(counter => $counter + 1);
21     }
22
23     return [ 200, [], [ "counter=$counter" ] ];
24 };
25
26 $app = Plack::Middleware::Session::Cookie->wrap($app, secret => "foobar");
27
28 my $ua = LWP::UserAgent->new;
29 $ua->cookie_jar( HTTP::Cookies->new );
30
31 test_psgi ua => $ua, app => $app, client => sub {
32     my $cb = shift;
33
34     my $res = $cb->(GET "/");
35     is $res->content, "counter=0";
36
37     $res = $cb->(GET "/");
38     is $res->content, "counter=1";
39
40     $res = $cb->(GET "/");
41     is $res->content, "counter=2";
42
43     my $res = $cb->(GET "/");
44     is $res->content, "counter=0";
45 };
46
47 done_testing;