Skip to content

Commit 9b21afa

Browse files
simbabqueoalders
authored andcommitted
add autocheck() method #232
1 parent 9b7828d commit 9b21afa

File tree

3 files changed

+54
-12
lines changed

3 files changed

+54
-12
lines changed

Changes

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
Revision history for WWW::Mechanize
22

33
{{$NEXT}}
4+
[ENHANCEMENTS]
5+
- Add autocheck() to enable or disable autochecking at run time in
6+
addition to setting it at object creation (GH#232) (Julien Fiegehenn)
47

58
2.13 2022-07-29 09:44:46Z
69
[ENHANCEMENTS]

lib/WWW/Mechanize.pm

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -188,11 +188,12 @@ are errors, not warnings.
188188
The default value is ON, unless it's being subclassed, in which
189189
case it is OFF. This means that standalone L<WWW::Mechanize> instances
190190
have autocheck turned on, which is protective for the vast majority
191-
of Mech users who don't bother checking the return value of get()
192-
and post() and can't figure why their code fails. However, if
193-
L<WWW::Mechanize> is subclassed, such as for L<Test::WWW::Mechanize>
194-
or L<Test::WWW::Mechanize::Catalyst>, this may not be an appropriate
195-
default, so it's off.
191+
of Mech users who don't bother checking the return value of
192+
C<L<< get()|/"$mech->get( $uri )" >>>
193+
and C<L<< post()|/"$mech->post( $uri, content => $content )" >>> and can't
194+
figure why their code fails. However, if L<WWW::Mechanize> is subclassed, such
195+
as for L<Test::WWW::Mechanize> or L<Test::WWW::Mechanize::Catalyst>, this may
196+
not be an appropriate default, so it's off.
196197
197198
=item * C<< noproxy => [0|1] >>
198199
@@ -2656,6 +2657,28 @@ sub quiet {
26562657
return $self->{quiet};
26572658
}
26582659

2660+
=head2 $mech->autocheck(true/false)
2661+
2662+
Allows you to enable and disable autochecking.
2663+
2664+
Autocheck checks each request made to see if it was successful. This saves you
2665+
the trouble of manually checking yourself. Any errors found are errors, not
2666+
warnings. Please see C<L<< new|/"new()" >>> for more details.
2667+
2668+
$mech->autocheck(1); # turns on automatic request checking (the default)
2669+
$mech->autocheck(0); # turns off automatic request checking
2670+
$mech->autocheck(); # returns the current autocheck status
2671+
2672+
=cut
2673+
2674+
sub autocheck {
2675+
my $self = shift;
2676+
2677+
$self->{autocheck} = $_[0] if @_;
2678+
2679+
return $self->{autocheck};
2680+
}
2681+
26592682
=head2 $mech->stack_depth( $max_depth )
26602683
26612684
Get or set the page stack depth. Use this if you're doing a lot of page

t/autocheck.t

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,33 @@ use WWW::Mechanize ();
1010
my $bad_url = "file:///foo.foo.xx.random";
1111

1212
AUTOCHECK_OFF: {
13-
my $mech = WWW::Mechanize->new( autocheck => 0 );
14-
$mech->get( $bad_url );
15-
ok( !$mech->success, qq{Didn't fetch $bad_url, but didn't die, either} );
13+
my $mech = WWW::Mechanize->new( autocheck => 0 );
14+
ok( !$mech->autocheck, q{Autocheck is set to off via new()} );
15+
16+
$mech->get($bad_url);
17+
ok( !$mech->success, qq{Didn't fetch $bad_url, but didn't die, either} );
18+
19+
$mech->autocheck(1);
20+
ok( $mech->autocheck, q{Autocheck is now on} );
21+
like( exception { $mech->get($bad_url) },
22+
qr/Error GETing/,
23+
qq{... and couldn't fetch $bad_url, and died as a result} );
1624
}
1725

1826
AUTOCHECK_ON: {
19-
like(
20-
exception { WWW::Mechanize->new->get($bad_url) },
27+
my $mech = WWW::Mechanize->new;
28+
ok( $mech->autocheck, q{Autocheck is on by default} );
29+
30+
like( exception { $mech->get($bad_url) },
2131
qr/Error GETing/,
22-
qq{Couldn't fetch $bad_url, and died as a result}
23-
);
32+
qq{Couldn't fetch $bad_url, and died as a result} );
33+
34+
$mech->autocheck(0);
35+
ok( !$mech->autocheck, q{Autocheck is now off} );
36+
37+
$mech->get($bad_url);
38+
ok( !$mech->success,
39+
qq{... and didn't fetch $bad_url, but didn't die, either} );
2440
}
2541

2642
done_testing();

0 commit comments

Comments
 (0)