Link/Tester/Simple.pm

filter: Statementstmt 90.9% 60 / 66 Branchbran 66.6% 12 / 18 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 8 / 8 Podpod 0.0% 0 / 2 totaltotal 81.8% 81 / 99 CC 11 SCAR 19.0
CC
11
Cov
84%
get_response
11.0
new
2.0
BEGIN
1.0
CRAP 6.7
« Link/Reporter/Text.pm ↑ Summary Link/Reporter/HTML.pm »
1 package WWW::Link::Tester::Simple;
2 3 $REVISION=q$Revision: 1.9 $ ; $VERSION = sprintf ( "%d.%02d", $REVISION =~ /(\d+).(\d+)/ );
3  
4 =head1 NAME
5  
6 WWW::Link::Tester::Simple - a link tester that uses the LWP head method
7  
8 =head1 SYNOPSIS
9  
10 use WWW::Link::Test::Simple
11 $ua=create_a_user_agent();
12 $link=get_a_link_object();
13 WWW::Link::Test::Simple::test_link($ua, $link);
14 WWW::Link::Tester::Simple::Test($url)
15  
16 =head1 DESCRIPTION
17  
18 This is a simple Link Testing module which accepts a url and returns a
19 status based on the result returned by the LWP useragent.
20  
21 The link is tested and then given information about what was
22 discovered. The link then records this information for future use..
23  
24 =head1 METHODS
25  
26 =head2 test_link
27  
28 This function tests a link by going out to the world and checking it
29 and then telling the associated link object what happened.
30  
31 =cut
32  
33 3 ▶use HTTP::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 3 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 3 ▶use WWW::Link::Tester;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 3 @ISA="WWW::Link::Tester";
37 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41 4 ▶sub new {
Subroutine new: called CC=2 SCAR=7.1
Pod undocumented
42 4 my $proto = shift;
43 4 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
040
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
44 4 my $self = {};
45 4 $self->{"user_agent"}=shift;
46 4 bless $self, $class;
47 4 $self->config();
48 4 return $self;
49 }
50  
51 30 ▶sub get_response {
Subroutine get_response: called CC=10 SCAR=24.0
Pod undocumented
52 30 my $self=shift;
53 30 my $link=shift;
54 30 my $user_agent=$self->{"user_agent"};
55 30 my $url=$link->url();
56 30 my $supported;
57  
58 30 my $verbose=$self->{"verbose"};
59  
60 30 my $urlo=new URI($url);
61 30 my $proto=$urlo->scheme();
62  
63 30 eval { $supported = $user_agent->is_protocol_supported($proto) ; };
64  
65 #if it get's really upset, is_protocol_supported sometimes dies. We
66 #just treat this as an unsupported link.
67  
68 30 ▶ $@ && do {
Branch: 1 branch
BranchTrueFalseTotal
$@ and do { warn $@; $supported = 0 } 0 30 30
69 0 warn $@;
70 0 $supported=0;
71 };
72  
73 30 ▶ $supported or do {
Branch: 1 branch
BranchTrueFalseTotal
$supported or do { my $response = "HTTP::Response"->new(498, "Unsupported protocol"); return $response } 24 6 30
74 6 my $response=new HTTP::Response ( RC_PROTOCOL_UNSUPPORTED,
75 MSG_PROTOCOL_UNSUPPORTED );
76 6 return $response;
77 };
78  
79 24 my $request=new HTTP::Request ('HEAD',$url);
80  
81 24 ▶ print STDERR "sending request\n" if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 24 24
82 24 my $response=$user_agent->simple_request($request);
83 24 ▶ print STDERR "got response\n" if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 24 24
84 #warn on client error
85  
86 24 ▶ if ($self->{warn_access}) { # warn about links where we can't access it
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"warn_access"}) 0 24 24
87 # but someone might
88 0 print STDERR "didn't have authorisation\n";
89 }
90  
91 24 my @redirects;
92 24 ▶ REDIRECT: while ($response->is_redirect()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_redirect) 32 22 54
93 32 my $loc = $response->headers->header('Location');
94 32 ▶ (defined $loc) || do {
Branch: 1 branch
BranchTrueFalseTotal
defined $loc or do { carp("redirect with no location!"); $response = "HTTP::Response"->new(499, "Too Many Redirects"); last REDIRECT } 32 0 32
95 0 carp "redirect with no location!";
96 0 $response=new HTTP::Response (RC_REDIRECT_LIMIT_EXCEEDED,
97 MSG_REDIRECT_LIMIT_EXCEEDED);
98 0 last REDIRECT;
99 };
100 32 ▶ print STDERR "have a redirect: " . $loc . "\n" if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 32 32
101  
102 32 push @redirects, $response;
103  
104 32 ▶ (@redirects == $self->{"max_redirects"}) && do {
Branch: 1 branch
BranchTrueFalseTotal
@redirects == $self->{"max_redirects"} and do { $response = "HTTP::Response"->new(499, "Too Many Redirects"); last } 2 30 32
105 2 $response=new HTTP::Response (RC_REDIRECT_LIMIT_EXCEEDED,
106 MSG_REDIRECT_LIMIT_EXCEEDED);
107 2 last;
108 };
109 30 my $request=new HTTP::Request ('HEAD',$loc);
110 30 $response=$user_agent->simple_request($request);
111 }
112 24 return $response, @redirects;
113 }
114  
115  
116 3 1; #kEEp rEqUIrE HaPpY.
117  
118  
« Link/Reporter/Text.pm ↑ Summary Link/Reporter/HTML.pm »