| 1 | package WWW::Link::Tester; | ||||||||||||||||||||||||||||
| 2 | 4 | $REVISION=q$Revision: 1.14 $ ; $VERSION = sprintf ( "%d.%02d", $REVISION =~ /(\d+).(\d+)/ ); | |||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | =head1 NAME | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | WWW::Link::Tester - base class for link testers. | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | use WWW::Link::Tester | ||||||||||||||||||||||||||||
| 11 | $ua=create_a_user_agent(); | ||||||||||||||||||||||||||||
| 12 | my $tester = new WWW::Link::Tester, $ua; | ||||||||||||||||||||||||||||
| 13 | $link=get_a_link_object(); | ||||||||||||||||||||||||||||
| 14 | $tester->test_link($link); | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | This class acts as a base for constructing link testing classes. It | ||||||||||||||||||||||||||||
| 19 | provides methods that are useful within those classes. | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | =cut | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 4 | ▶ | use URI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 4 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | 4 | ▶ | use Exporter; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | 4 | @ISA=qw(Exporter); | |||||||||||||||||||||||||||
| 27 | 4 | @EXPORT=qw(RC_PROTOCOL_UNSUPPORTED MSG_PROTOCOL_UNSUPPORTED RC_REDIRECT_LIMIT_EXCEEDED MSG_REDIRECT_LIMIT_EXCEEDED); | |||||||||||||||||||||||||||
| 28 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | 4 | ▶ | use HTTP::Status; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | sub MSG_PROTOCOL_UNSUPPORTED () {"Unsupported protocol";} | ||||||||||||||||||||||||||||
| 33 | sub RC_PROTOCOL_UNSUPPORTED () {498;} | ||||||||||||||||||||||||||||
| 34 | sub MSG_REDIRECT_LIMIT_EXCEEDED () {"Too Many Redirects";} | ||||||||||||||||||||||||||||
| 35 | sub RC_REDIRECT_LIMIT_EXCEEDED () {499;} | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 1 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 38 | 1 | my $proto = shift; | |||||||||||||||||||||||||||
| 39 | 1 | ▶ | my $class = ref($proto) || $proto; | ||||||||||||||||||||||||||
|
Condition33%1/3
ref $proto || $proto
MC/DC0%0/2
ref $proto || $proto
ref $proto
$proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
| |||||||||||||||||||||||||||||
| 40 | 1 | my $self = {}; | |||||||||||||||||||||||||||
| 41 | 1 | $self->{"user_agent"}=shift; | |||||||||||||||||||||||||||
| 42 | 1 | bless $self, $class; | |||||||||||||||||||||||||||
| 43 | 1 | $self->config(); | |||||||||||||||||||||||||||
| 44 | 1 | return $self; | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 16 | ▶ | sub test_link { | ||||||||||||||||||||||||||
|
Subroutine
test_link: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 48 | 16 | my $self=shift; | |||||||||||||||||||||||||||
| 49 | 16 | my $link=shift; | |||||||||||||||||||||||||||
| 50 | 16 | my ($response, @redirects) = $self->get_response($link); | |||||||||||||||||||||||||||
| 51 | 16 | $self->handle_response($link,$response,@redirects); | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 0 | ▶ | sub get_response { | ||||||||||||||||||||||||||
|
Subroutine
get_response: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 55 | 0 | confess "get_response must be implemented in the child class"; | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 0 | ▶ | sub verbose { | ||||||||||||||||||||||||||
|
Subroutine
verbose: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 59 | 0 | my $self=shift; | |||||||||||||||||||||||||||
| 60 | 0 | my $verb=shift; | |||||||||||||||||||||||||||
| 61 | 0 | ▶ | $self->{verbose} = $verb if defined $verb; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 0 | return $self->{verbose}; | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | =head2 handle_response | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | handle_response is normally just the same as apply response. The | ||||||||||||||||||||||||||||
| 69 | extra level of indirection can be used where some responses aren't | ||||||||||||||||||||||||||||
| 70 | meant to directly affect the link. | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | =cut | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 16 | ▶ | sub handle_response { | ||||||||||||||||||||||||||
|
Subroutine
handle_response: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 75 | 16 | my $self=shift; | |||||||||||||||||||||||||||
| 76 | 16 | my $link=shift; | |||||||||||||||||||||||||||
| 77 | 16 | my $response=shift; | |||||||||||||||||||||||||||
| 78 | 16 | my @redirects=@_; | |||||||||||||||||||||||||||
| 79 | 16 | $self->apply_response($link, $response, @redirects); | |||||||||||||||||||||||||||
| 80 | 16 | my $now=time; | |||||||||||||||||||||||||||
| 81 | 16 | $link->store_response($response,$now, ref $self); | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | =head2 apply_response | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | We have a response which should be used to affect the state of the | ||||||||||||||||||||||||||||
| 87 | link. This should only be called at the end of a chain of redirects, | ||||||||||||||||||||||||||||
| 88 | not for each member in the chain. | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | =cut | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 46 | ▶ | sub apply_response { | ||||||||||||||||||||||||||
|
Subroutine
apply_response: called CC=14 SCAR=27.5
Pod
documented
| |||||||||||||||||||||||||||||
| 93 | 46 | my $self=shift; | |||||||||||||||||||||||||||
| 94 | 46 | my $link=shift; | |||||||||||||||||||||||||||
| 95 | 46 | my $response=shift; | |||||||||||||||||||||||||||
| 96 | 46 | my @redirects=@_; | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 46 | my $verbose=$self->{"verbose"}; | |||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 46 | my $mode=$self->{mode}; | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 46 | ▶ | confess "response wasn't an object" unless ref $response; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | 46 | ▶ | confess "non numeric response code" . $response->code() unless | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | $response->code() =~ m/[1-9][0-9]+/; | ||||||||||||||||||||||||||||
| 105 | CASE: { | ||||||||||||||||||||||||||||
| 106 | 46 | ▶ | robot_lockout($response) && do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | 3 | ▶ | print STDERR "checking disallowed, signalling link\n" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | if $::verbose; | ||||||||||||||||||||||||||||
| 109 | 3 | $link->disallowed(); | |||||||||||||||||||||||||||
| 110 | 3 | last; | |||||||||||||||||||||||||||
| 111 | }; | ||||||||||||||||||||||||||||
| 112 | 43 | ▶ | unsupported($response) && do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | 14 | ▶ | print STDERR "checking disallowed, signalling link\n" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | if $::verbose; | ||||||||||||||||||||||||||||
| 115 | 14 | $link->unsupported(); | |||||||||||||||||||||||||||
| 116 | 14 | last; | |||||||||||||||||||||||||||
| 117 | }; | ||||||||||||||||||||||||||||
| 118 | 29 | ▶ | $response->is_error() && do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | 17 | ▶ | print STDERR "response was an error, signalling link\n" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | if $::verbose; | ||||||||||||||||||||||||||||
| 121 | 17 | $link->failed_test(); #someone should come look | |||||||||||||||||||||||||||
| 122 | 17 | last; | |||||||||||||||||||||||||||
| 123 | }; | ||||||||||||||||||||||||||||
| 124 | 12 | ▶ | $response->is_success() && do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | 12 | ▶ | print STDERR "response was success, signalling link\n" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | if $::verbose; | ||||||||||||||||||||||||||||
| 127 | 12 | $link->passed_test(); | |||||||||||||||||||||||||||
| 128 | 12 | last; | |||||||||||||||||||||||||||
| 129 | }; | ||||||||||||||||||||||||||||
| 130 | #a redirect should eiter be terminiated with a success or should be | ||||||||||||||||||||||||||||
| 131 | #treated as a failure if we don't find the end of a chain of | ||||||||||||||||||||||||||||
| 132 | #redirects. | ||||||||||||||||||||||||||||
| 133 | 0 | ▶ | $response->is_redirect() && | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | die "Redirects shouldn't get through to here\n"; | ||||||||||||||||||||||||||||
| 135 | 0 | $self->ambiguous_test($link); | |||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | 46 | ▶ | @redirects ? $link->found_redirected() : $link->not_redirected(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | 46 | ▶ | $link->redirects( \@redirects ) if @redirects; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 5 | ▶ | sub config { | ||||||||||||||||||||||||||
|
Subroutine
config: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 142 | 5 | shift->{"max_redirects"}=15; | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 46 | ▶ | sub robot_lockout { | ||||||||||||||||||||||||||
|
Subroutine
robot_lockout: called CC=4 SCAR=14.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 146 | 46 | my $response=shift; | |||||||||||||||||||||||||||
| 147 | 46 | ▶ | $response->code() == RC_FORBIDDEN or return 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 148 | 3 | my $message = $response->message(); | |||||||||||||||||||||||||||
| 149 | #FIXME; this is because the Complex tester looses this informaition! | ||||||||||||||||||||||||||||
| 150 | 3 | ▶ | defined $message or return 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 151 | 2 | ▶ | $message=~ /robots\.txt/ and return 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | 0 | return 0; | |||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 43 | ▶ | sub unsupported { | ||||||||||||||||||||||||||
|
Subroutine
unsupported: called CC=4 SCAR=14.6
Pod
undocumented
| |||||||||||||||||||||||||||||
| 156 | 43 | my $response=shift; | |||||||||||||||||||||||||||
| 157 | 43 | ▶ | $response->code() == RC_PROTOCOL_UNSUPPORTED | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 158 | and return 1; | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | #I'm not sure I like the following special case. | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 29 | ▶ | $response->code == 400 and | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$response->code == 400 and $response->message =~ /Library does not allow method/
MC/DC0%0/2
$response->code == 400 and $response->message =~ /Library does not allow method/
$response->code == 400
$response->message =~ /Library does not allow method/
Truth table33%1/3
A and B
A= $response->code == 400B= $response->message =~ /Library does not allow method/
| |||||||||||||||||||||||||||||
| 163 | $response-> message =~ m/Library does not allow method/ | ||||||||||||||||||||||||||||
| 164 | and return 1; | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 29 | return 0; | |||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 4 | 1; #kEEp rEqUIrE HaPpY. | |||||||||||||||||||||||||||