| 1 | package Test::WWW::Simple; | ||||||||||||||||||
| 2 | |||||||||||||||||||
| 3 | 25 | ▶ | use strict; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 4 | 25 | ▶ | use warnings; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 5 | |||||||||||||||||||
| 6 | 25 | our $VERSION = '0.41'; | |||||||||||||||||
| 7 | |||||||||||||||||||
| 8 | 25 | ▶ | use Test::Builder; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 9 | 25 | ▶ | use Test::LongString; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 10 | 25 | ▶ | use Test::More; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 11 | 25 | ▶ | use WWW::Mechanize::Pluggable; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 12 | |||||||||||||||||||
| 13 | 25 | my $Test = Test::Builder->new; # The Test:: singleton | |||||||||||||||||
| 14 | 25 | my $Mech = WWW::Mechanize::Pluggable->new(autocheck => 0); | |||||||||||||||||
| 15 | # The Mech user agent and support methods | ||||||||||||||||||
| 16 | 25 | my $cache_results = 0; # default to not caching Mech fetches | |||||||||||||||||
| 17 | 25 | our $last_url; # last URL fetched successfully by Mech | |||||||||||||||||
| 18 | 25 | my %page_cache; # saves pages for %%cache; we probably | |||||||||||||||||
| 19 | # will want to change this over to a | ||||||||||||||||||
| 20 | # tied hash later to allow for disk caching | ||||||||||||||||||
| 21 | # instead of just memory caching. | ||||||||||||||||||
| 22 | my %status_cache; # ditto | ||||||||||||||||||
| 23 | |||||||||||||||||||
| 24 | 25 | $Test::WWW::display_length = 40; # length for display in error messages | |||||||||||||||||
| 25 | |||||||||||||||||||
| 26 | 32 | ▶ | sub import { | ||||||||||||||||
|
Subroutine
import: called CC=6 SCAR=18.4
| |||||||||||||||||||
| 27 | 32 | my ($class, %args) = @_; | |||||||||||||||||
| 28 | 32 | my $caller = caller; | |||||||||||||||||
| 29 | 25 | ▶ | no strict 'refs'; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 30 | 32 | *{$caller.'::page_like_full'} = \&page_like_full; | |||||||||||||||||
| 31 | 32 | *{$caller.'::page_unlike_full'} = \&page_unlike_full; | |||||||||||||||||
| 32 | 32 | *{$caller.'::text_like'} = \&text_like; | |||||||||||||||||
| 33 | 32 | *{$caller.'::text_unlike'} = \&text_unlike; | |||||||||||||||||
| 34 | 32 | *{$caller.'::page_like'} = \&page_like; | |||||||||||||||||
| 35 | 32 | *{$caller.'::page_unlike'} = \&page_unlike; | |||||||||||||||||
| 36 | 32 | *{$caller.'::user_agent'} = \&user_agent; | |||||||||||||||||
| 37 | 32 | *{$caller.'::cache'} = \&cache; | |||||||||||||||||
| 38 | 32 | *{$caller.'::no_cache'} = \&no_cache; | |||||||||||||||||
| 39 | 32 | *{$caller.'::mech'} = \&mech; | |||||||||||||||||
| 40 | 32 | *{$caller.'::last_test'} = \&last_test; | |||||||||||||||||
| 41 | |||||||||||||||||||
| 42 | 32 | $Test->exported_to($caller); | |||||||||||||||||
| 43 | |||||||||||||||||||
| 44 | # Check the 'use' arguments to see if we have either | ||||||||||||||||||
| 45 | # 'agent', 'agent_string', or 'no_agent'. | ||||||||||||||||||
| 46 | # | ||||||||||||||||||
| 47 | # If we have none of these, assume 'Windows IE 6'. | ||||||||||||||||||
| 48 | 32 | ▶ | if (defined $args{agent}) { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 49 | # This is a string suitable for passing to agent_alias. | ||||||||||||||||||
| 50 | 0 | my $alias = $args{agent}; | |||||||||||||||||
| 51 | 0 | ▶ | if (grep { /^$alias\z/ } $Mech->known_agent_aliases()) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 52 | 0 | $Mech->agent_alias($alias); | |||||||||||||||||
| 53 | } | ||||||||||||||||||
| 54 | else { | ||||||||||||||||||
| 55 | 0 | die "'$alias' is not a valid WWW::Mechanize user agent alias\n"; | |||||||||||||||||
| 56 | } | ||||||||||||||||||
| 57 | } | ||||||||||||||||||
| 58 | elsif (defined $args{agent_string}) { | ||||||||||||||||||
| 59 | 0 | $Mech->agent('agent_string'); | |||||||||||||||||
| 60 | } | ||||||||||||||||||
| 61 | elsif(!defined $args{no_agent}) { | ||||||||||||||||||
| 62 | 31 | $Mech->agent_alias('Windows IE 6'); | |||||||||||||||||
| 63 | } | ||||||||||||||||||
| 64 | else { | ||||||||||||||||||
| 65 | # No action; no_agent was defined, | ||||||||||||||||||
| 66 | # so leave the user agent as "WWW::Mechanize/version". | ||||||||||||||||||
| 67 | } | ||||||||||||||||||
| 68 | |||||||||||||||||||
| 69 | 32 | ▶ | if (defined $args{tests}) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 70 | 8 | plan tests => $args{tests}; | |||||||||||||||||
| 71 | } | ||||||||||||||||||
| 72 | } | ||||||||||||||||||
| 73 | |||||||||||||||||||
| 74 | 0 | ▶ | sub _clean_text { | ||||||||||||||||
|
Subroutine
_clean_text: not called CC=1 SCAR=6.9
| |||||||||||||||||||
| 75 | 0 | my $page = $Mech->content(format=>'text'); | |||||||||||||||||
| 76 | 0 | $page =~ s/\xa0/ /g; | |||||||||||||||||
| 77 | 0 | return $page; | |||||||||||||||||
| 78 | } | ||||||||||||||||||
| 79 | |||||||||||||||||||
| 80 | 2 | ▶ | sub text_like($$;$) { | ||||||||||||||||
|
Subroutine
text_like: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||
| 81 | 2 | my($url, $regex, $comment) = @_; | |||||||||||||||||
| 82 | 2 | my ($state, $content, $status_line) = _fetch($url); | |||||||||||||||||
| 83 | 2 | ▶ | $state | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 84 | ? like_string(_clean_text(), $regex, $comment) | ||||||||||||||||||
| 85 | : fail "Fetch of $url failed: ".$status_line; | ||||||||||||||||||
| 86 | } | ||||||||||||||||||
| 87 | |||||||||||||||||||
| 88 | 0 | ▶ | sub text_unlike($$;$) { | ||||||||||||||||
|
Subroutine
text_unlike: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||
| 89 | 0 | my($url, $regex, $comment) = @_; | |||||||||||||||||
| 90 | 0 | my ($state, $content, $status_line) = _fetch($url); | |||||||||||||||||
| 91 | 0 | ▶ | $state | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 92 | ? unlike_string(_clean_text(), $regex, $comment) | ||||||||||||||||||
| 93 | : fail "Fetch of $url failed: ".$status_line; | ||||||||||||||||||
| 94 | } | ||||||||||||||||||
| 95 | |||||||||||||||||||
| 96 | 35 | ▶ | sub page_like($$;$) { | ||||||||||||||||
|
Subroutine
page_like: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||
| 97 | 35 | my($url, $regex, $comment) = @_; | |||||||||||||||||
| 98 | 35 | my ($state, $content, $status_line) = _fetch($url); | |||||||||||||||||
| 99 | 35 | ▶ | $state | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 100 | ? like_string($content, $regex, $comment) | ||||||||||||||||||
| 101 | : fail "Fetch of $url failed: ".$status_line; | ||||||||||||||||||
| 102 | } | ||||||||||||||||||
| 103 | |||||||||||||||||||
| 104 | 8 | ▶ | sub page_unlike($$;$) { | ||||||||||||||||
|
Subroutine
page_unlike: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||
| 105 | 8 | my($url, $regex, $comment) = @_; | |||||||||||||||||
| 106 | 8 | my ($state, $content, $status_line) = _fetch($url); | |||||||||||||||||
| 107 | 8 | ▶ | $state | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 108 | ? unlike_string($content, $regex, $comment) | ||||||||||||||||||
| 109 | : fail "Fetch of $url failed: ".$status_line; | ||||||||||||||||||
| 110 | } | ||||||||||||||||||
| 111 | |||||||||||||||||||
| 112 | 3 | ▶ | sub page_like_full($$;$) { | ||||||||||||||||
|
Subroutine
page_like_full: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||
| 113 | 3 | my($url, $regex, $comment) = @_; | |||||||||||||||||
| 114 | 3 | my ($state, $content, $status_line) = _fetch($url); | |||||||||||||||||
| 115 | 3 | ▶ | $state | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 116 | ? like($content, $regex, $comment) | ||||||||||||||||||
| 117 | : fail "Fetch of $url failed: ".$status_line; | ||||||||||||||||||
| 118 | } | ||||||||||||||||||
| 119 | |||||||||||||||||||
| 120 | 0 | ▶ | sub page_unlike_full($$;$) { | ||||||||||||||||
|
Subroutine
page_unlike_full: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||
| 121 | 0 | my($url, $regex, $comment) = @_; | |||||||||||||||||
| 122 | 0 | my ($state, $content, $status_line) = _fetch($url); | |||||||||||||||||
| 123 | 0 | ▶ | $state | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 124 | ? unlike($content, $regex, $comment) | ||||||||||||||||||
| 125 | : fail "Fetch of $url failed: ".$status_line; | ||||||||||||||||||
| 126 | } | ||||||||||||||||||
| 127 | |||||||||||||||||||
| 128 | 48 | ▶ | sub _fetch { | ||||||||||||||||
|
Subroutine
_fetch: called CC=4 SCAR=13.9
| |||||||||||||||||||
| 129 | 48 | my ($url, $comment) = @_; | |||||||||||||||||
| 130 | 48 | local $Test::Builder::Level = 2; | |||||||||||||||||
| 131 | 48 | my @results; | |||||||||||||||||
| 132 | |||||||||||||||||||
| 133 | 48 | ▶ | if ($cache_results) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 134 | 8 | ▶ | if (defined $page_cache{$url}) { | ||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||
| 135 | # in cache: return it. | ||||||||||||||||||
| 136 | 6 | @results = (1, $page_cache{$url}, $status_cache{$url}); | |||||||||||||||||
| 137 | } | ||||||||||||||||||
| 138 | elsif ($last_url eq $url) { | ||||||||||||||||||
| 139 | # "cached" in Mech object | ||||||||||||||||||
| 140 | @results = (1, | ||||||||||||||||||
| 141 | $page_cache{$url} = $Mech->content, | ||||||||||||||||||
| 142 | 0 | $status_cache{$url} = $Mech->response->status_line); | |||||||||||||||||
| 143 | } | ||||||||||||||||||
| 144 | else { | ||||||||||||||||||
| 145 | # not in cache - load and save the page (if any) | ||||||||||||||||||
| 146 | 2 | $Mech->get($url); | |||||||||||||||||
| 147 | @results = ($Mech->success, | ||||||||||||||||||
| 148 | $page_cache{$url} = $Mech->content, | ||||||||||||||||||
| 149 | 2 | $status_cache{$url} = $Mech->response->status_line); | |||||||||||||||||
| 150 | } | ||||||||||||||||||
| 151 | } | ||||||||||||||||||
| 152 | else { | ||||||||||||||||||
| 153 | # not caching. Just grab it. | ||||||||||||||||||
| 154 | 40 | $Mech->get($url); | |||||||||||||||||
| 155 | 40 | @results = ($Mech->success, $Mech->content, $Mech->response->status_line); | |||||||||||||||||
| 156 | } | ||||||||||||||||||
| 157 | 48 | $last_url = $_[0]; | |||||||||||||||||
| 158 | 48 | $page_cache{$url} = $results[1]; | |||||||||||||||||
| 159 | 48 | $status_cache{$url} = $results[2]; | |||||||||||||||||
| 160 | 48 | @results; | |||||||||||||||||
| 161 | } | ||||||||||||||||||
| 162 | |||||||||||||||||||
| 163 | 3 | ▶ | sub _trimmed_url { | ||||||||||||||||
|
Subroutine
_trimmed_url: called CC=2 SCAR=6.9
| |||||||||||||||||||
| 164 | 3 | my $url = shift; | |||||||||||||||||
| 165 | 3 | ▶ | length($url) > $Test::WWW::display_length | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 166 | ? substr($url,0,$Test::WWW::display_length) . "..." | ||||||||||||||||||
| 167 | : $url; | ||||||||||||||||||
| 168 | } | ||||||||||||||||||
| 169 | |||||||||||||||||||
| 170 | 7 | ▶ | sub user_agent { | ||||||||||||||||
|
Subroutine
user_agent: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||
| 171 | 7 | ▶ | my $agent = shift || "Windows IE 6"; | ||||||||||||||||
|
Condition100%2/2
shift() || 'Windows IE 6'
MC/DC100%1/1
shift() || 'Windows IE 6'
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||
| 172 | 7 | $Mech->agent_alias($agent); | |||||||||||||||||
| 173 | } | ||||||||||||||||||
| 174 | |||||||||||||||||||
| 175 | 5 | ▶ | sub mech { | ||||||||||||||||
|
Subroutine
mech: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||
| 176 | 5 | my ($self) = @_; | |||||||||||||||||
| 177 | 5 | return $Mech; | |||||||||||||||||
| 178 | } | ||||||||||||||||||
| 179 | |||||||||||||||||||
| 180 | 1 | ▶ | sub last_test { | ||||||||||||||||
|
Subroutine
last_test: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||
| 181 | 1 | my($self) = @_; | |||||||||||||||||
| 182 | 1 | return ($Test->details)[-1]; | |||||||||||||||||
| 183 | } | ||||||||||||||||||
| 184 | |||||||||||||||||||
| 185 | 4 | ▶ | sub cache (;$) { | ||||||||||||||||
|
Subroutine
cache: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||
| 186 | 4 | my $comment = shift; | |||||||||||||||||
| 187 | 4 | ▶ | $Test->note($comment) if defined $comment; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 188 | 4 | $last_url = ""; | |||||||||||||||||
| 189 | 4 | $cache_results = 1; | |||||||||||||||||
| 190 | 4 | 1; | |||||||||||||||||
| 191 | } | ||||||||||||||||||
| 192 | |||||||||||||||||||
| 193 | 5 | ▶ | sub no_cache (;$) { | ||||||||||||||||
|
Subroutine
no_cache: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||
| 194 | 5 | my $comment = shift; | |||||||||||||||||
| 195 | 5 | ▶ | $Test->note($comment) if defined $comment; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 196 | 5 | $last_url = ""; | |||||||||||||||||
| 197 | 5 | $cache_results = 0; | |||||||||||||||||
| 198 | 5 | 1; | |||||||||||||||||
| 199 | } | ||||||||||||||||||
| 200 | |||||||||||||||||||
| 201 | |||||||||||||||||||
| 202 | 25 | 1; | |||||||||||||||||
| 203 | |||||||||||||||||||
| 204 | __END__ | ||||||||||||||||||