Link/Tester.pm

filter: Statementstmt 90.7% 78 / 86 Branchbran 65.7% 25 / 38 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 86.6% 13 / 15 Podpod 22.2% 2 / 9 totaltotal 75.9% 120 / 158 CC 22 SCAR 20.5
CC
22
Cov
81%
apply_response
15.6
verbose
6.0
unsupported
4.3
CRAP 7.7
« Link/Reporter/Compile.pm ↑ Summary Link/Reporter/Text.pm »
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
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
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
BranchTrueFalseTotal
if defined $verb 0 0 0
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
BranchTrueFalseTotal
unless ref $response 0 46 46
103 46 ▶ confess "non numeric response code" . $response->code() unless
Branch: 1 branch
BranchTrueFalseTotal
unless $response->code =~ /[1-9][0-9]+/ 0 46 46
104 $response->code() =~ m/[1-9][0-9]+/;
105 CASE: {
106 46 ▶ robot_lockout($response) && do {
Branch: 1 branch
BranchTrueFalseTotal
robot_lockout($response) and do { print STDERR "checking disallowed, signalling link\n" if $main::verbose; $link->disallowed; last } 3 43 46
107 3 ▶ print STDERR "checking disallowed, signalling link\n"
Branch: 1 branch
BranchTrueFalseTotal
if $main::verbose 0 3 3
108 if $::verbose;
109 3 $link->disallowed();
110 3 last;
111 };
112 43 ▶ unsupported($response) && do {
Branch: 1 branch
BranchTrueFalseTotal
unsupported($response) and do { print STDERR "checking disallowed, signalling link\n" if $main::verbose; $link->unsupported; last } 14 29 43
113 14 ▶ print STDERR "checking disallowed, signalling link\n"
Branch: 1 branch
BranchTrueFalseTotal
if $main::verbose 0 14 14
114 if $::verbose;
115 14 $link->unsupported();
116 14 last;
117 };
118 29 ▶ $response->is_error() && do {
Branch: 1 branch
BranchTrueFalseTotal
$response->is_error and do { print STDERR "response was an error, signalling link\n" if $main::verbose; $link->failed_test; last } 17 12 29
119 17 ▶ print STDERR "response was an error, signalling link\n"
Branch: 1 branch
BranchTrueFalseTotal
if $main::verbose 0 17 17
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
BranchTrueFalseTotal
$response->is_success and do { print STDERR "response was success, signalling link\n" if $main::verbose; $link->passed_test; last } 12 0 12
125 12 ▶ print STDERR "response was success, signalling link\n"
Branch: 1 branch
BranchTrueFalseTotal
if $main::verbose 0 12 12
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
BranchTrueFalseTotal
$response->is_redirect and die "Redirects shouldn't get through to here\n" 0 0 0
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
BranchTrueFalseTotal
@redirects ? : 6 40 46
138 46 ▶ $link->redirects( \@redirects ) if @redirects;
Branch: 1 branch
BranchTrueFalseTotal
if @redirects 6 40 46
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
BranchTrueFalseTotal
$response->code == 403 or return 0 3 43 46
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
BranchTrueFalseTotal
defined $message or return 1 2 1 3
151 2 ▶ $message=~ /robots\.txt/ and return 1;
Branch: 1 branch
BranchTrueFalseTotal
$message =~ /robots\.txt/ and return 1 2 0 2
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
BranchTrueFalseTotal
$response->code == 498 and return 1 14 29 43
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
BranchTrueFalseTotal
$response->code == 400 and $response->message =~ /Library does not allow method/ and return 1 0 29 29
Condition33%1/3
$response->code == 400 and $response->message =~ /Library does not allow method/
!ll&&!rl&&r
2900
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/
ABresult
0X0
100
111
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.
« Link/Reporter/Compile.pm ↑ Summary Link/Reporter/Text.pm »