Skip to content

Commit

Permalink
added tests for disabled cookie jar
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Apr 29, 2012
1 parent e9b6c9a commit 7488633
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 68 deletions.
20 changes: 10 additions & 10 deletions lib/Mojo/Cookie/Response.pm
Expand Up @@ -9,19 +9,19 @@ has [qw/domain httponly max_age path secure/];
my $ATTR_RE = qr/(Domain|expires|HttpOnly|Max-Age|Path|Secure)/msi;

sub expires {
my ($self, $expires) = @_;

# New expires value
if (defined $expires) {
$self->{expires} = $expires;
return $self;
}
my $self = shift;

# Upgrade
$self->{expires} = Mojo::Date->new($self->{expires})
if defined $self->{expires} && !ref $self->{expires};
return $self->{expires}
= defined $self->{expires} && !ref $self->{expires}
? Mojo::Date->new($self->{expires})
: $self->{expires}
unless @_;

# New expires value
$self->{expires} = shift;

return $self->{expires};
return $self;
}

# "Remember the time he ate my goldfish?
Expand Down
43 changes: 17 additions & 26 deletions lib/Mojo/CookieJar.pm
Expand Up @@ -15,24 +15,21 @@ sub add {
my ($self, @cookies) = @_;

# Add cookies
my $size = $self->max_cookie_size;
for my $cookie (@cookies) {
my $name = $cookie->name;
my $value = $cookie->value;
my $domain = $cookie->domain;
my $path = $cookie->path;

# Convert max age to expires
$cookie->expires($cookie->max_age + time) if $cookie->max_age;

# Default to session cookie
$cookie->max_age(0) unless $cookie->expires || $cookie->max_age;
if (my $age = $cookie->max_age) { $cookie->expires($age + time) }

# Check cookie size
next if length($value //= '') > $self->max_cookie_size;
next if length($cookie->value // '') > $size;

# Replace cookie
my $domain = $cookie->domain;
$domain =~ s/^\.//;
my $jar = $self->{jar}{$domain} ||= [];
my $path = $cookie->path;
my $name = $cookie->name;
my $jar = $self->{jar}{$domain} ||= [];
@$jar = (grep({$_->path ne $path || $_->name ne $name} @$jar), $cookie);
}

Expand Down Expand Up @@ -63,30 +60,24 @@ sub find {
my $path = $url->path->to_string || '/';
my @found;
while ($domain =~ /[^\.]+\.[^\.]+|localhost$/) {
next unless my $jar = $self->{jar}{$domain};
next unless my $old = $self->{jar}{$domain};

# Grab cookies
my @new;
for my $cookie (@$jar) {
my $new = $self->{jar}{$domain} = [];
for my $cookie (@$old) {

# Check if cookie has expired
my $expires = $cookie->expires;
my $session = defined $cookie->max_age && $cookie->max_age > 0 ? 1 : 0;
next if $expires && !$session && time > ($expires->epoch || 0);
push @new, $cookie;
next if $expires && time > ($expires->epoch || 0);
push @$new, $cookie;

# Taste cookie
next if $cookie->secure && $url->scheme ne 'https';
my $cpath = $cookie->path;
next unless $path =~ /^\Q$cpath/;
my $result = Mojo::Cookie::Request->new(
name => $cookie->name,
value => $cookie->value
);
push @found, $result;
next unless $path =~ /^\Q@{[$cookie->path]}/;
my $name = $cookie->name;
my $value = $cookie->value;
push @found, Mojo::Cookie::Request->new(name => $name, value => $value);
}

$self->{jar}{$domain} = \@new;
}

# Remove another part
Expand Down Expand Up @@ -128,7 +119,7 @@ L<Mojo::CookieJar> implements the following attributes.
my $max_cookie_size = $jar->max_cookie_size;
$jar = $jar->max_cookie_size(4096);
Maximum size of cookies in bytes.
Maximum cookie size in bytes, defaults to C<4096>.
=head1 METHODS
Expand Down
23 changes: 13 additions & 10 deletions lib/Mojo/UserAgent.pm
Expand Up @@ -132,32 +132,32 @@ sub _cache {
my ($self, $name, $id) = @_;

# Enqueue
my $cache = $self->{cache} ||= [];
my $old = $self->{cache} ||= [];
if ($id) {
my $max = $self->max_connections;
$self->_remove(shift(@$cache)->[1]) while @$cache > $max;
push @$cache, [$name, $id] if $max;
$self->_remove(shift(@$old)->[1]) while @$old > $max;
push @$old, [$name, $id] if $max;
return;
}

# Dequeue
my $found;
my $loop = $self->_loop;
my ($result, @cache);
for my $cached (@$cache) {
my $new = $self->{cache} = [];
for my $cached (@$old) {

# Search for id/name and remove corrupted connections
if (!$result && ($cached->[1] eq $name || $cached->[0] eq $name)) {
if (!$found && ($cached->[1] eq $name || $cached->[0] eq $name)) {
my $stream = $loop->stream($cached->[1]);
if ($stream && !$stream->is_readable) { $result = $cached->[1] }
if ($stream && !$stream->is_readable) { $found = $cached->[1] }
else { $loop->remove($cached->[1]) }
}

# Requeue
else { push @cache, $cached }
else { push @$new, $cached }
}
$self->{cache} = \@cache;

return $result;
return $found;
}

sub _cleanup {
Expand Down Expand Up @@ -688,6 +688,9 @@ environment variable or C<10>.
Cookie jar to use for this user agents requests, defaults to a
L<Mojo::CookieJar> object.
# Disable cookie jar
$ua->cookie_jar(0);
=head2 C<http_proxy>
my $proxy = $ua->http_proxy;
Expand Down
36 changes: 15 additions & 21 deletions t/mojo/cookiejar.t
Expand Up @@ -145,17 +145,13 @@ $jar->add(
path => '/foo',
name => 'foo',
value => 'bar'
)
);
$jar->add(
),
Mojo::Cookie::Response->new(
domain => 'www.kraih.com',
path => '/',
name => 'just',
value => 'works'
)
);
$jar->add(
),
Mojo::Cookie::Response->new(
domain => 'kraih.com',
path => '/foo',
Expand All @@ -168,14 +164,21 @@ is $cookies[0]->name, 'foo', 'right name';
is $cookies[0]->value, 'bar', 'right value';
is $cookies[1], undef, 'no second cookie';

# Expired cookie
# Expired cookies
$jar = Mojo::CookieJar->new;
$jar->add(
Mojo::Cookie::Response->new(
domain => 'kraih.com',
path => '/foo',
name => 'foo',
value => 'bar'
),
Mojo::Cookie::Response->new(
domain => 'labs.kraih.com',
path => '/',
name => 'baz',
value => '24',
max_age => -1
)
);
my $expired = Mojo::Cookie::Response->new(
Expand All @@ -184,8 +187,7 @@ my $expired = Mojo::Cookie::Response->new(
name => 'baz',
value => '23'
);
$expired->expires(time - 1);
$jar->add($expired);
$jar->add($expired->expires(time - 1));
@cookies = $jar->find(Mojo::URL->new('http://labs.kraih.com/foo'));
is $cookies[0]->name, 'foo', 'right name';
is $cookies[0]->value, 'bar', 'right value';
Expand All @@ -199,9 +201,7 @@ $jar->add(
path => '/foo',
name => 'foo',
value => 'bar'
)
);
$jar->add(
),
Mojo::Cookie::Response->new(
domain => 'labs.kraih.com',
path => '/',
Expand All @@ -225,17 +225,13 @@ $jar->add(
path => '/',
name => 'foo',
value => 'bar'
)
);
$jar->add(
),
Mojo::Cookie::Response->new(
domain => '.labs.kraih.com',
path => '/',
name => 'baz',
value => 'yada'
)
);
$jar->add(
),
Mojo::Cookie::Response->new(
domain => '.kraih.com',
path => '/',
Expand All @@ -260,9 +256,7 @@ $jar->add(
path => '/foo',
name => 'foo',
value => 'bar1'
)
);
$jar->add(
),
Mojo::Cookie::Response->new(
domain => 'kraih.com',
path => '/foo',
Expand Down
13 changes: 12 additions & 1 deletion t/mojolicious/group_lite_app.t
Expand Up @@ -8,10 +8,11 @@ BEGIN {
$ENV{MOJO_REACTOR} = 'Mojo::Reactor::Poll';
}

use Test::More tests => 195;
use Test::More tests => 201;

# "Let's see how crazy I am now, Nixon. The correct answer is very."
use Mojo::ByteStream 'b';
use Mojo::CookieJar;
use Mojolicious::Lite;
use Test::Mojo;

Expand Down Expand Up @@ -257,6 +258,16 @@ my $broken = "\$Version=1; mojolicious=$session--$hmac; \$Path=/";
$t->get_ok('/bridge2stash' => {Cookie => $broken})->status_is(200)
->content_is("stash too!!!!!!!\n");

# GET /bridge2stash (without cookie jar)
$t->ua->cookie_jar(0);
$t->get_ok('/bridge2stash' => {'X-Flash' => 1})->status_is(200)
->content_is("stash too!!!!!!!\n");

# GET /bridge2stash (again without cookie jar)
$t->get_ok('/bridge2stash' => {'X-Flash' => 1})->status_is(200)
->content_is("stash too!!!!!!!\n");
$t->ua->cookie_jar(Mojo::CookieJar->new);

# GET /bridge2stash (fresh start)
$t->reset_session;
$t->get_ok('/bridge2stash' => {'X-Flash' => 1})->status_is(200)
Expand Down

0 comments on commit 7488633

Please sign in to comment.