Skip to content

Commit

Permalink
improved cookie header generation slightly
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Dec 28, 2011
1 parent 48cfa41 commit 3c092ad
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 16 deletions.
14 changes: 4 additions & 10 deletions lib/Mojo/Message/Request.pm
Expand Up @@ -40,24 +40,18 @@ sub cookies {
my $self = shift;

# Add cookies
my $headers = $self->headers;
if (@_) {
my $cookies = shift;
$cookies = Mojo::Cookie::Request->new($cookies)
if ref $cookies eq 'HASH';
$cookies = $cookies->to_string;
for my $cookie (@_) {
$cookie = Mojo::Cookie::Request->new($cookie)
if ref $cookie eq 'HASH';
$cookies .= "; $cookie";
$cookie = Mojo::Cookie::Request->new($cookie) if ref $cookie eq 'HASH';
$self->headers->add('Cookie', "$cookie");
}
$self->headers->add('Cookie', $cookies);
return $self;
}

# Cookie
my @cookies;
push @cookies, @{Mojo::Cookie::Request->parse($_)}
for $self->headers->cookie;
push @cookies, @{Mojo::Cookie::Request->parse($_)} for $headers->cookie;
return \@cookies;
}

Expand Down
9 changes: 4 additions & 5 deletions lib/Mojo/Message/Response.pm
Expand Up @@ -82,20 +82,19 @@ sub cookies {
my $self = shift;

# Add cookies
my $headers = $self->headers;
if (@_) {
for my $cookie (@_) {
$cookie = Mojo::Cookie::Response->new($cookie)
if ref $cookie eq 'HASH';
$self->headers->add('Set-Cookie', "$cookie");
$cookie = Mojo::Cookie::Response->new($cookie) if ref $cookie eq 'HASH';
$headers->add('Set-Cookie', "$cookie");
}
return $self;
}

# Parse cookies
my @cookies;
push @cookies, @{Mojo::Cookie::Response->parse($_)}
for $self->headers->set_cookie;

for $headers->set_cookie;
return \@cookies;
}

Expand Down
2 changes: 1 addition & 1 deletion t/mojo/request.t
Expand Up @@ -1598,7 +1598,7 @@ ok !$req2->at_least_version('1.2'), 'not version 1.2';
is $req2->headers->expect, '100-continue', 'right "Expect" value';
is $req2->headers->host, '127.0.0.1', 'right "Host" value';
is $req2->headers->content_length, 13, 'right "Content-Length" value';
is $req2->headers->cookie, 'foo=bar; bar=baz', 'right "Cookie" value';
is $req2->headers->cookie, 'foo=bar, bar=baz', 'right "Cookie" value';
is $req2->url, '/foo/bar', 'right URL';
is $req2->url->to_abs, 'http://127.0.0.1/foo/bar', 'right absolute URL';
is defined $req2->cookie('foo'), 1, 'right value';
Expand Down

0 comments on commit 3c092ad

Please sign in to comment.