Skip to content

Commit

Permalink
be even more conservative
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed May 8, 2012
1 parent 0a325c9 commit 4e0b28a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 19 deletions.
24 changes: 12 additions & 12 deletions lib/Mojo/Cookie/Response.pm
Expand Up @@ -6,7 +6,7 @@ use Mojo::Util 'quote';

has [qw/domain httponly max_age path secure/];

my $ATTR_RE = qr/(domain|expires|HttpOnly|Max-Age|path|secure)/msi;
my $ATTR_RE = qr/(expires|domain|path|secure|HttpOnly|Max-Age)/msi;

sub expires {
my $self = shift;
Expand Down Expand Up @@ -57,30 +57,30 @@ sub parse {
sub to_string {
my $self = shift;

# Name and value
# Name and value (Netscape)
return '' unless my $name = $self->name;
my $value = $self->value // '';
$value = $value =~ /[,;"]/ ? quote($value) : $value;
my $cookie = "$name=$value";

# "domain"
# "expires" (Netscape)
if (defined(my $e = $self->expires)) { $cookie .= "; expires=$e" }

# "domain" (Netscape)
if (my $domain = $self->domain) { $cookie .= "; domain=$domain" }

# "path"
# "path" (Netscape)
if (my $path = $self->path) { $cookie .= "; path=$path" }

# "Max-Age"
if (defined(my $m = $self->max_age)) { $cookie .= "; Max-Age=$m" }

# "expires"
if (defined(my $e = $self->expires)) { $cookie .= "; expires=$e" }

# "secure"
# "secure" (Netscape)
if (my $secure = $self->secure) { $cookie .= "; secure" }

# "HttpOnly"
# "HttpOnly" (RFC 6265)
if (my $httponly = $self->httponly) { $cookie .= "; HttpOnly" }

# "Max-Age" (RFC 6265)
if (defined(my $m = $self->max_age)) { $cookie .= "; Max-Age=$m" }

return $cookie;
}

Expand Down
17 changes: 10 additions & 7 deletions t/mojo/cookie.t
Expand Up @@ -169,8 +169,9 @@ $cookie->max_age(60);
$cookie->expires(1218092879);
$cookie->secure(1);
$cookie->httponly(1);
is $cookie->to_string, 'foo=ba r; domain=kraih.com; path=/test; Max-Age=60;'
. ' expires=Thu, 07 Aug 2008 07:07:59 GMT; secure; HttpOnly', 'right format';
is $cookie->to_string,
'foo=ba r; expires=Thu, 07 Aug 2008 07:07:59 GMT; domain=kraih.com;'
. ' path=/test; secure; HttpOnly; Max-Age=60', 'right format';

# Parse response cookie (RFC 6265)
$cookies
Expand Down Expand Up @@ -330,8 +331,9 @@ is $cookies->[0]->max_age, 60, 'right max age value';
is $cookies->[0]->expires, 'Thu, 07 Aug 2008 07:07:59 GMT',
'right expires value';
is $cookies->[0]->secure, '1', 'right secure flag';
is $cookies->[0]->to_string, 'foo=; domain=kraih.com; path=/test; Max-Age=60;'
. ' expires=Thu, 07 Aug 2008 07:07:59 GMT; secure', 'right result';
is $cookies->[0]->to_string,
'foo=; expires=Thu, 07 Aug 2008 07:07:59 GMT; domain=kraih.com;'
. ' path=/test; secure; Max-Age=60', 'right result';
is $cookies->[1], undef, 'no more cookies';
$cookies
= Mojo::Cookie::Response->parse(
Expand All @@ -345,8 +347,9 @@ is $cookies->[0]->max_age, 60, 'right max age value';
is $cookies->[0]->expires, 'Thu, 07 Aug 2008 07:07:59 GMT',
'right expires value';
is $cookies->[0]->secure, '1', 'right secure flag';
is $cookies->[0]->to_string, 'foo=; domain=kraih.com; path=/test; Max-Age=60;'
. ' expires=Thu, 07 Aug 2008 07:07:59 GMT; secure', 'right result';
is $cookies->[0]->to_string,
'foo=; expires=Thu, 07 Aug 2008 07:07:59 GMT; domain=kraih.com;'
. ' path=/test; secure; Max-Age=60', 'right result';
is $cookies->[1], undef, 'no more cookies';

# Response cookie with Max-Age 0 and Expires 0
Expand All @@ -357,7 +360,7 @@ $cookie->path('/');
$cookie->max_age(0);
$cookie->expires(0);
is $cookie->to_string,
'foo=bar; path=/; Max-Age=0; expires=Thu, 01 Jan 1970 00:00:00 GMT',
'foo=bar; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/; Max-Age=0',
'right format';

# Parse response cookie with Max-Age 0 and Expires 0 (RFC 6265)
Expand Down

0 comments on commit 4e0b28a

Please sign in to comment.