add configuration of 'cookie_path'
Hu Hailin [Sun, 6 Aug 2006 12:10:37 +0000 (12:10 +0000)]
Changes
lib/Catalyst/Plugin/Session/State/Cookie.pm
t/basic.t

diff --git a/Changes b/Changes
index 8b4b8f8..75df1c6 100644 (file)
--- a/Changes
+++ b/Changes
@@ -1,5 +1,8 @@
 Revision history for Perl extension Catalyst::Plugin::Session::State::Cookie
 
+0.05    2006-08-06 20:50:00
+        - Add configuration of 'cookie_path' (Michael W Peterson)
+
 0.04
         - Depend on a higher version of C::P::Session
 
index 7fbde70..a87ccc8 100644 (file)
@@ -7,7 +7,7 @@ use warnings;
 use NEXT;
 use Catalyst::Utils ();
 
-our $VERSION = "0.04";
+our $VERSION = "0.05";
 
 sub setup_session {
     my $c = shift;
@@ -38,8 +38,21 @@ sub set_session_id {
 
 sub update_session_cookie {
     my ( $c, $updated ) = @_;
-    my $cookie_name = $c->config->{session}{cookie_name};
-    $c->response->cookies->{$cookie_name} = $updated;
+    
+    unless ( $c->cookie_is_rejecting( $updated ) ) {
+        my $cookie_name = $c->config->{session}{cookie_name};
+        $c->response->cookies->{$cookie_name} = $updated;
+    }
+}
+
+sub cookie_is_rejecting {
+    my ( $c, $cookie ) = @_;
+    
+    if ( $cookie->{path} ) {
+        return 1 if index $c->request->path, $cookie->{path};
+    }
+    
+    return 0;
 }
 
 sub make_session_cookie {
@@ -49,6 +62,7 @@ sub make_session_cookie {
     my $cookie = {
         value => $sid,
         ( $cfg->{cookie_domain} ? ( domain => $cfg->{cookie_domain} ) : () ),
+        ( $cfg->{cookie_path} ? ( path => $cfg->{cookie_path} ) : () ),
         %attrs,
     };
 
@@ -190,6 +204,10 @@ user's browser is shut down.
 
 If this attribute set true, the cookie will only be sent via HTTPS.
 
+=item cookie_path
+
+The path of the request url where cookie should be baked.
+
 =back
 
 =head1 CAVEATS
index 9620451..843ff26 100644 (file)
--- a/t/basic.t
+++ b/t/basic.t
@@ -3,7 +3,7 @@
 use strict;
 use warnings;
 
-use Test::More tests => 10;
+use Test::More tests => 13;
 use Test::MockObject;
 use Test::MockObject::Extends;
 
@@ -71,3 +71,14 @@ is_deeply(
     { session => { value => $sessionid, expires => 123 } },
     "cookie was set correctly"
 );
+
+$cxt->clear;
+$req->clear;
+
+can_ok( $m, "cookie_is_rejecting" );
+
+%req_cookies = ( path => '/foo' );
+$req->set_always( path => '/' );
+ok( $cxt->cookie_is_rejecting(\%req_cookies), "cookie is rejecting" );
+$req->set_always( path => '/foo/bar' );
+ok( !$cxt->cookie_is_rejecting(\%req_cookies), "cookie is not rejecting" );