document updated 12 years ago, on Feb 20, 2012
injecting a cookie
To act as if the previous response had returned another cookie, use this:
# Add a cookie to the cookie_jar, using the context of the last response ($mech->response()).
#
# The arguments are exactly that of CGI::Cookie->new(), they're passed directly through.
sub set_cookie {
my $mech = shift;
my @cookie_new = @_;
my $cookie = CGI::Cookie->new(@cookie_new);
# pretend as if this cookie-header had come in on the last response
$mech->response()->headers()->push_header('Set-Cookie' => $cookie->as_string);
# pull in that new header
$mech->cookie_jar()->extract_cookies( $mech->response() );
}