44820786b4a381572415d5cd2cfb9bb329829305
[catagits/Web-Session.git] / t / 011_middleware_w_custom_session.t
1 use Plack::Test;
2 use Plack::Middleware::Session;
3 use Test::More;
4 use HTTP::Request::Common;
5 use HTTP::Cookies;
6
7 {
8     package My::Custom::Session;
9     use strict;
10     use warnings;
11     use parent 'Plack::Session';
12 }
13
14 my $app = sub {
15     my $env = shift;
16
17     isa_ok($env->{'plack.session'}, 'My::Custom::Session');
18
19     my $counter = $env->{'plack.session'}->get('counter') || 0;
20
21     my $body = "Counter=$counter";
22     $counter++;
23     $env->{'plack.session'}->set(counter => $counter);
24
25     return [ 200, [], [ $body ] ];
26 };
27
28 $app = Plack::Middleware::Session->wrap(
29     $app,
30     session_class => 'My::Custom::Session'
31 );
32
33 test_psgi $app, sub {
34     my $cb = shift;
35
36     my $jar = HTTP::Cookies->new;
37
38     my $res = $cb->(GET "http://localhost/");
39     is $res->content, "Counter=0";
40     $jar->extract_cookies($res);
41
42     my $req = GET "http://localhost/";
43     $jar->add_cookie_header($req);
44     $res = $cb->($req);
45     is $res->content, "Counter=1";
46 };
47
48 done_testing;
49