Skip to content

Instantly share code, notes, and snippets.

@spiros
Created September 3, 2012 12:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save spiros/3609136 to your computer and use it in GitHub Desktop.
Save spiros/3609136 to your computer and use it in GitHub Desktop.
Test::Mojo methods for cookie checking
=head2 has_cookie
Returns 1 if cookie is found irrespective of value.
$rv = $t->has_cookie( $name, $url );
=cut
sub has_cookie{
my $self = shift;
my $cookie_name = shift;
my $url = shift;
return 0 unless ( defined $cookie_name && $cookie_name );
my @ah_cookies =
$self->ua->cookie_jar->find( Mojo::URL->new( $url ));
return 0
if ( ! @ah_cookies || scalar(@ah_cookies) == 0);
## Array contains a bunch of Mojo::Cookie::Request objects
foreach my $k ( @ah_cookies ) {
my $found_cookie_name = $k->name;
return 1 if ( $found_cookie_name eq $cookie_name );
}
return 0;
}
=head2 get_cookie_value
Returns the value of the cookie and undef on error.
my $value = $t->get_cookie_value( $name, $url );
=cut
sub get_cookie_value {
my $self = shift;
my $cookie_name = shift;
my $url = shift;
return 0 unless ( defined $cookie_name && $cookie_name );
my @ah_cookies =
$self->ua->cookie_jar->find( Mojo::URL->new( $url ));
return 0
if ( ! @ah_cookies || scalar(@ah_cookies) == 0);
## Array contains a bunch of Mojo::Cookie::Request objects
foreach my $k ( @ah_cookies ) {
my $found_cookie_name = $k->name;
if ( $found_cookie_name eq $cookie_name ) {
return $k->value;
}
}
return undef;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment