Fixed a bug where path is not set to / by default.
Tatsuhiko Miyagawa [Wed, 3 Feb 2010 04:40:19 +0000 (20:40 -0800)]
If you want to set a cookie for only the current URL, you should set

   # delete won't work
   $env->{'psgi.session.options'}->{path} = undef;

lib/Plack/Session/State/Cookie.pm
t/013_cookiestore.t
t/015_cookie_options_mw.t

index 9ea7a68..9be093c 100644 (file)
@@ -27,11 +27,12 @@ sub merge_options {
 
     delete $options{id};
 
-    $options{path}     = $self->path || '/' if !exists $options{path} && defined $self->path;
+    $options{path}     = $self->path || '/' if !exists $options{path};
     $options{domain}   = $self->domain      if !exists $options{domain} && defined $self->domain;
     $options{secure}   = $self->secure      if !exists $options{secure} && defined $self->secure;
     $options{httponly} = $self->httponly    if !exists $options{httponly} && defined $self->httponly;
 
+
     if (!exists $options{expires} && defined $self->expires) {
         $options{expires} = time + $self->expires;
     }
index 51c2278..f1f1a58 100644 (file)
@@ -32,6 +32,7 @@ test_psgi ua => $ua, app => $app, client => sub {
     my $res = $cb->(GET "/");
     is $res->content, "counter=0";
     like $res->header('Set-Cookie'), qr/expires=/;
+    like $res->header('Set-Cookie'), qr/path=\//;
 
     $res = $cb->(GET "/");
     is $res->content, "counter=1";
index b373c46..8e37715 100644 (file)
@@ -10,7 +10,8 @@ my $app = sub {
 
     $env->{'psgix.session'}->{counter} = 1;
 
-    $env->{'psgix.session.options'}{path}     = '/foo';
+    my $path = $env->{PATH_INFO} =~ /with_path/ ? "/foo" : undef;
+    $env->{'psgix.session.options'}{path}     = $path;
     $env->{'psgix.session.options'}{domain}   = '.example.com';
     $env->{'psgix.session.options'}{httponly} = 1;
 
@@ -23,6 +24,9 @@ test_psgi $app, sub {
     my $cb = shift;
 
     my $res = $cb->(GET "http://localhost/");
+    like $res->header('Set-Cookie'), qr/plack_session=\w+; domain=.example.com; HttpOnly/;
+
+    $res = $cb->(GET "http://localhost/with_path");
     like $res->header('Set-Cookie'), qr/plack_session=\w+; domain=.example.com; path=\/foo; HttpOnly/;
 };