C::P::Session::State::Cookie - changes in option cookie_secure + new option cookie_ht...
[catagits/Catalyst-Plugin-Session-State-Cookie.git] / t / basic.t
CommitLineData
1a776a0c 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
8bdcbb46 6use Test::More tests => 13;
1a776a0c 7use Test::MockObject;
8use Test::MockObject::Extends;
9
10my $m;
11BEGIN { use_ok( $m = "Catalyst::Plugin::Session::State::Cookie" ) }
12
13my $cookie = Test::MockObject->new;
14$cookie->set_always( value => "the session id" );
15
16my $req = Test::MockObject->new;
17my %req_cookies;
18$req->set_always( cookies => \%req_cookies );
19
20my $res = Test::MockObject->new;
21my %res_cookies;
22$res->set_always( cookies => \%res_cookies );
23
24my $cxt =
25 Test::MockObject::Extends->new("Catalyst::Plugin::Session::State::Cookie");
26
c3f2575c 27$cxt->set_always( config => {} );
1a776a0c 28$cxt->set_always( request => $req );
29$cxt->set_always( response => $res );
74586782 30$cxt->set_always( session => { } );
31$cxt->set_always( session_expires => 123 );
1a776a0c 32$cxt->set_false("debug");
33my $sessionid;
34$cxt->mock( sessionid => sub { shift; $sessionid = shift if @_; $sessionid } );
35
7acfe95d 36can_ok( $m, "setup_session" );
37
38$cxt->setup_session;
39
74586782 40like( $cxt->config->{session}{cookie_name},
41 qr/_session$/, "default cookie name is set" );
42
43$cxt->config->{session}{cookie_name} = "session";
7acfe95d 44
2bde9162 45can_ok( $m, "get_session_id" );
1a776a0c 46
2bde9162 47ok( !$cxt->get_session_id, "no session id yet");
1a776a0c 48
49$cxt->clear;
50
51%req_cookies = ( session => $cookie );
52
2bde9162 53is( $cxt->get_session_id, "the session id", "session ID was restored from cookie" );
1a776a0c 54
55$cxt->clear;
56$res->clear;
57
2bde9162 58can_ok( $m, "set_session_id" );
59$cxt->set_session_id("moose");
1a776a0c 60
2bde9162 61$res->called_ok( "cookies", "created a cookie on set" );
1a776a0c 62
1a776a0c 63$cxt->clear;
64$res->clear;
65
2bde9162 66$cxt->set_session_id($sessionid);
1a776a0c 67
2bde9162 68$res->called_ok( "cookies", "response cookie was set when sessionid changed" );
1a776a0c 69is_deeply(
70 \%res_cookies,
c3f2575c 71 { session => { value => $sessionid, expires => 123 } },
1a776a0c 72 "cookie was set correctly"
73);
8bdcbb46 74
75$cxt->clear;
76$req->clear;
77
78can_ok( $m, "cookie_is_rejecting" );
79
80%req_cookies = ( path => '/foo' );
91e4fe2d 81$req->set_always( path => '' );
8bdcbb46 82ok( $cxt->cookie_is_rejecting(\%req_cookies), "cookie is rejecting" );
91e4fe2d 83$req->set_always( path => 'foo/bar' );
8bdcbb46 84ok( !$cxt->cookie_is_rejecting(\%req_cookies), "cookie is not rejecting" );