Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

perrin (4270)

  (email not shown publicly)

Perrin is a contributor to various Perl-related projects like mod_perl, Template Toolkit, and Class::DBI. He is a frequent speaker at OSCON, YAPC, and ApacheCon, and a contributor to several perl-related books.

Journal of perrin (4270)

Wednesday April 26, 2006
03:10 PM

Test::WWW::Mechanize tricks

[ #29455 ]

Sometimes I want to test if my application sent the right redirect, but I don't want to follow it. It might be to a page that isn't accessible from my dev system, or is hosted by someone else and I don't want to hit it every time I run the test suite.

To do this with Test::WWW::Mechanize, I use this code:

my $mech = Test::WWW::Mechanize->new(autocheck => 0);
$mech->requests_redirectable([]);    # don't follow redirects
is($mech->status, 302);
$mech->content_contains($url_to_redirect_to, 'sent to correct URL');

Another thing I want to check is if the request set a cookie. A simple way to do that:

my $resp = $mech->get($uri);
ok($resp->header('Set-Cookie'), 'has cookie header');
like($mech->cookie_jar->as_string(), qr/$COOKIE_NAME/,
     'cookie was accepted');

Since the LWP cookie handling does the same checks that browsers do for domain and path, this can help catch problems with your cookie headers.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.