From 7d46fae0f0baed36488d1bd4e4e05c3505f14008 Mon Sep 17 00:00:00 2001 From: Lee Johnson Date: Wed, 18 Jun 2014 09:30:48 +0200 Subject: [PATCH] resolve #60 [rt.cpan.org #50576] - cookie -max-age argument to CGI::Cookie is now correctly set when this is passed in the constructor. previously the value from the -expires argument was being used, meaning it was not possible to pass *only* the -max-age argument and have this set. this also means that the values for the -max-age and -expires arguments can be different, should you want to do something like that update tests to reflect above change and increase coverage for the -max-age constructor argument / max_age method. also update perldoc adding missing documentation for the max_age method --- Changes | 6 ++++-- lib/CGI/Cookie.pm | 7 ++++++- t/cookie.t | 15 +++++++++++++++ 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/Changes b/Changes index c9603a75..7179287d 100644 --- a/Changes +++ b/Changes @@ -1,4 +1,4 @@ -4.03 ????-??-?? +4.03 2014-06-30 [ REMOVED / DEPRECATIONS ] - the -multiple option to popup_menu is now IGNORED as this did not @@ -8,7 +8,9 @@ [ SPEC / BUG FIXES ] - support redirects in mod_perl2, or fall back to using env variable for up to 5 redirects, when getting the query string (RT #36312) - + - CGI::Cookie now correctly supports the -max-age argument, previously + if this was passed the value of the -expires argument would be used + meaning there was no way to supply *only* this argument (RT #50576) [ DOCUMENTATION ] - clarify documentation regarding query_string method (RT #48370) diff --git a/lib/CGI/Cookie.pm b/lib/CGI/Cookie.pm index 4b0b9d3b..75a8e066 100644 --- a/lib/CGI/Cookie.pm +++ b/lib/CGI/Cookie.pm @@ -126,7 +126,7 @@ sub new { $self->domain( $domain ) if defined $domain; $self->secure( $secure ) if defined $secure; $self->expires( $expires ) if defined $expires; - $self->max_age($expires) if defined $max_age; + $self->max_age( $max_age ) if defined $max_age; $self->httponly( $httponly ) if defined $httponly; return $self; } @@ -337,6 +337,7 @@ See these URLs for more information: my $c = CGI::Cookie->new(-name => 'foo', -value => 'bar', -expires => '+3M', + '-max-age' => '+3M', -domain => '.capricorn.com', -path => '/cgi-bin/database', -secure => 1 @@ -503,6 +504,10 @@ Get or set the cookie's path. Get or set the cookie's expiration time. +=item B + +Get or set the cookie's max_age value. + =back diff --git a/t/cookie.t b/t/cookie.t index f10d3b6a..dda2f82c 100644 --- a/t/cookie.t +++ b/t/cookie.t @@ -175,6 +175,7 @@ my @test_cookie = ( is($c->name , 'baz', 'name is correct'); is($c->value , 'qux', 'value is correct'); ok(!defined $c->expires, 'expires is not set'); + ok(!defined $c->max_age, 'max_age is not set'); ok(!defined $c->domain , 'domain attributeis not set'); is($c->path, '/', 'path atribute is set to default'); ok(!defined $c->secure , 'secure attribute is set'); @@ -207,6 +208,7 @@ my @test_cookie = ( my $c = CGI::Cookie->new(-name => 'Jam', -value => 'Hamster', -expires => '+3M', + '-max-age' => '+3M', -domain => '.pie-shop.com', -path => '/', -secure => 1, @@ -222,6 +224,9 @@ my @test_cookie = ( my $expires = $c->expires; like($c->as_string, "/$expires/", "Stringified cookie contains expires"); + my $max_age = $c->max_age; + like($c->as_string, "/$max_age/", "Stringified cookie contains max_age"); + my $domain = $c->domain; like($c->as_string, "/$domain/", "Stringified cookie contains domain"); @@ -245,6 +250,8 @@ my @test_cookie = ( ok($c->as_string !~ /expires/, "Stringified cookie has no expires field"); + ok($c->as_string !~ /max-age/, "Stringified cookie has no max-age field"); + ok($c->as_string !~ /domain/, "Stringified cookie has no domain field"); $path = $c->path; @@ -364,6 +371,14 @@ MAX_AGE: { $cookie->max_age( '113' ); is $cookie->max_age => 13, 'max_age(num) as delta'; + + $cookie = CGI::Cookie->new( -name=>'a', value=>'b', '-max-age' => '+3d'); + is( $cookie->max_age,3*24*60*60,'-max-age in constructor' ); + ok( !$cookie->expires,' ... lack of expires' ); + + $cookie = CGI::Cookie->new( -name=>'a', value=>'b', '-expires' => 'now', '-max-age' => '+3d'); + is( $cookie->max_age,3*24*60*60,'-max-age in constructor' ); + ok( $cookie->expires,'-expires in constructor' ); }