Skip to content

Commit

Permalink
fix Max-Age handling with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jvolkening committed Sep 16, 2020
1 parent bff018a commit ad3ef66
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/HTTP/CookieJar.pm
Expand Up @@ -67,7 +67,12 @@ sub add {
# set timestamps and normalize expires
my $now = $parse->{creation_time} = $parse->{last_access_time} = time;
if ( exists $parse->{'max-age'} ) {
$parse->{expires} = $now + delete $parse->{'max-age'};
# "If delta-seconds is less than or equal to zero (0), let expiry-time
# be the earliest representable date and time."
$parse->{expires} = $parse->{'max-age'} <= 0
? 0
: $now + $parse->{'max-age'};
delete $parse->{'max-age'};
}
# update creation time from old cookie, if exists
if ( my $old = $self->{store}{$domain}{$path}{$name} ) {
Expand Down
27 changes: 27 additions & 0 deletions t/add.t
Expand Up @@ -207,6 +207,33 @@ my @cases = (
},
},
},
# check that Max-Age supercedes Expires and that Max-Age <= 0 forces
# expiration
{
label => "max-age supercedes expires",
request => "http://example.com/",
cookies => [
"lang=en-us; Max-Age=100; Expires=Thu, 1 Jan 1970 00:00:00 GMT",
"SID=31d4d96e407aad42; Expires=Thu, 3 Jan 4841 00:00:00 GMT",
"SID=0000000000000000; Max-Age=0",
],
store => {
'example.com' => {
'/' => {
lang => {
name => "lang",
value => "en-us",
expires => ignore(),
creation_time => ignore(),
last_access_time => ignore(),
domain => "example.com",
hostonly => 1,
path => "/",
},
},
},
},
},
);

for my $c (@cases) {
Expand Down

0 comments on commit ad3ef66

Please sign in to comment.