line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Test::WWW::Simple; |
2
|
|
|
|
|
|
|
|
3
|
15
|
|
|
15
|
|
1728463
|
use strict; |
|
15
|
|
|
|
|
41
|
|
|
15
|
|
|
|
|
548
|
|
4
|
15
|
|
|
15
|
|
76
|
use warnings; |
|
15
|
|
|
|
|
27
|
|
|
15
|
|
|
|
|
669
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = '0.34'; |
7
|
|
|
|
|
|
|
|
8
|
15
|
|
|
15
|
|
82
|
use Test::Builder; |
|
15
|
|
|
|
|
27
|
|
|
15
|
|
|
|
|
320
|
|
9
|
15
|
|
|
15
|
|
13333
|
use Test::LongString; |
|
15
|
|
|
|
|
35241
|
|
|
15
|
|
|
|
|
83
|
|
10
|
15
|
|
|
15
|
|
2492
|
use Test::More; |
|
15
|
|
|
|
|
7486
|
|
|
15
|
|
|
|
|
112
|
|
11
|
15
|
|
|
15
|
|
28077
|
use WWW::Mechanize::Pluggable; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
my $Test = Test::Builder->new; # The Test:: singleton |
14
|
|
|
|
|
|
|
my $Mech = WWW::Mechanize::Pluggable->new(autocheck => 0); |
15
|
|
|
|
|
|
|
# The Mech user agent and support methods |
16
|
|
|
|
|
|
|
my $cache_results = 0; # default to not caching Mech fetches |
17
|
|
|
|
|
|
|
our $last_url; # last URL fetched successfully by Mech |
18
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
$Test::WWW::display_length = 40; # length for display in error messages |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
sub import { |
27
|
|
|
|
|
|
|
my ($class, %args) = @_; |
28
|
|
|
|
|
|
|
my $caller = caller; |
29
|
|
|
|
|
|
|
no strict 'refs'; |
30
|
|
|
|
|
|
|
*{$caller.'::page_like_full'} = \&page_like_full; |
31
|
|
|
|
|
|
|
*{$caller.'::page_unlike_full'} = \&page_unlike_full; |
32
|
|
|
|
|
|
|
*{$caller.'::text_like'} = \&text_like; |
33
|
|
|
|
|
|
|
*{$caller.'::text_unlike'} = \&text_unlike; |
34
|
|
|
|
|
|
|
*{$caller.'::page_like'} = \&page_like; |
35
|
|
|
|
|
|
|
*{$caller.'::page_unlike'} = \&page_unlike; |
36
|
|
|
|
|
|
|
*{$caller.'::user_agent'} = \&user_agent; |
37
|
|
|
|
|
|
|
*{$caller.'::cache'} = \&cache; |
38
|
|
|
|
|
|
|
*{$caller.'::no_cache'} = \&no_cache; |
39
|
|
|
|
|
|
|
*{$caller.'::mech'} = \&mech; |
40
|
|
|
|
|
|
|
*{$caller.'::last_test'} = \&last_test; |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
$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
|
|
|
|
|
|
|
if (defined $args{agent}) { |
49
|
|
|
|
|
|
|
# This is a string suitable for passing to agent_alias. |
50
|
|
|
|
|
|
|
my $alias = $args{agent}; |
51
|
|
|
|
|
|
|
if (grep { /^$alias\z/ } $Mech->known_agent_aliases()) { |
52
|
|
|
|
|
|
|
$Mech->agent_alias($alias); |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
else { |
55
|
|
|
|
|
|
|
die "'$alias' is not a valid WWW::Mechanize user agent alias\n"; |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
elsif (defined $args{agent_string}) { |
59
|
|
|
|
|
|
|
$Mech->agent('agent_string'); |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
elsif(!defined $args{no_agent}) { |
62
|
|
|
|
|
|
|
$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
|
|
|
|
|
|
|
if (defined $args{tests}) { |
70
|
|
|
|
|
|
|
plan tests => $args{tests}; |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
sub _clean_text { |
75
|
|
|
|
|
|
|
my $page = $Mech->content(format=>'text'); |
76
|
|
|
|
|
|
|
$page =~ s/\xa0/ /g; |
77
|
|
|
|
|
|
|
return $page; |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
sub text_like($$;$) { |
81
|
|
|
|
|
|
|
my($url, $regex, $comment) = @_; |
82
|
|
|
|
|
|
|
my ($state, $content, $status_line) = _fetch($url); |
83
|
|
|
|
|
|
|
$state |
84
|
|
|
|
|
|
|
? like_string(_clean_text(), $regex, $comment) |
85
|
|
|
|
|
|
|
: fail "Fetch of $url failed: ".$status_line; |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
sub text_unlike($$;$) { |
89
|
|
|
|
|
|
|
my($url, $regex, $comment) = @_; |
90
|
|
|
|
|
|
|
my ($state, $content, $status_line) = _fetch($url); |
91
|
|
|
|
|
|
|
$state |
92
|
|
|
|
|
|
|
? unlike_string(_clean_text(), $regex, $comment) |
93
|
|
|
|
|
|
|
: fail "Fetch of $url failed: ".$status_line; |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
sub page_like($$;$) { |
97
|
|
|
|
|
|
|
my($url, $regex, $comment) = @_; |
98
|
|
|
|
|
|
|
my ($state, $content, $status_line) = _fetch($url); |
99
|
|
|
|
|
|
|
$state |
100
|
|
|
|
|
|
|
? like_string($content, $regex, $comment) |
101
|
|
|
|
|
|
|
: fail "Fetch of $url failed: ".$status_line; |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
sub page_unlike($$;$) { |
105
|
|
|
|
|
|
|
my($url, $regex, $comment) = @_; |
106
|
|
|
|
|
|
|
my ($state, $content, $status_line) = _fetch($url); |
107
|
|
|
|
|
|
|
$state |
108
|
|
|
|
|
|
|
? unlike_string($content, $regex, $comment) |
109
|
|
|
|
|
|
|
: fail "Fetch of $url failed: ".$status_line; |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
sub page_like_full($$;$) { |
113
|
|
|
|
|
|
|
my($url, $regex, $comment) = @_; |
114
|
|
|
|
|
|
|
my ($state, $content, $status_line) = _fetch($url); |
115
|
|
|
|
|
|
|
$state |
116
|
|
|
|
|
|
|
? like($content, $regex, $comment) |
117
|
|
|
|
|
|
|
: fail "Fetch of $url failed: ".$status_line; |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
sub page_unlike_full($$;$) { |
121
|
|
|
|
|
|
|
my($url, $regex, $comment) = @_; |
122
|
|
|
|
|
|
|
my ($state, $content, $status_line) = _fetch($url); |
123
|
|
|
|
|
|
|
$state |
124
|
|
|
|
|
|
|
? unlike($content, $regex, $comment) |
125
|
|
|
|
|
|
|
: fail "Fetch of $url failed: ".$status_line; |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
sub _fetch { |
129
|
|
|
|
|
|
|
my ($url, $comment) = @_; |
130
|
|
|
|
|
|
|
local $Test::Builder::Level = 2; |
131
|
|
|
|
|
|
|
my @results; |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
if ($cache_results) { |
134
|
|
|
|
|
|
|
if (defined $page_cache{$url}) { |
135
|
|
|
|
|
|
|
# in cache: return it. |
136
|
|
|
|
|
|
|
@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
|
|
|
|
|
|
|
$status_cache{$url} = $Mech->response->status_line); |
143
|
|
|
|
|
|
|
} |
144
|
|
|
|
|
|
|
else { |
145
|
|
|
|
|
|
|
# not in cache - load and save the page (if any) |
146
|
|
|
|
|
|
|
$Mech->get($url); |
147
|
|
|
|
|
|
|
@results = ($Mech->success, |
148
|
|
|
|
|
|
|
$page_cache{$url} = $Mech->content, |
149
|
|
|
|
|
|
|
$status_cache{$url} = $Mech->response->status_line); |
150
|
|
|
|
|
|
|
} |
151
|
|
|
|
|
|
|
} |
152
|
|
|
|
|
|
|
else { |
153
|
|
|
|
|
|
|
# not caching. Just grab it. |
154
|
|
|
|
|
|
|
$Mech->get($url); |
155
|
|
|
|
|
|
|
@results = ($Mech->success, $Mech->content, $Mech->response->status_line); |
156
|
|
|
|
|
|
|
} |
157
|
|
|
|
|
|
|
$last_url = $_[0]; |
158
|
|
|
|
|
|
|
$page_cache{$url} = $results[1]; |
159
|
|
|
|
|
|
|
$status_cache{$url} = $results[2]; |
160
|
|
|
|
|
|
|
@results; |
161
|
|
|
|
|
|
|
} |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
sub _trimmed_url { |
164
|
|
|
|
|
|
|
my $url = shift; |
165
|
|
|
|
|
|
|
length($url) > $Test::WWW::display_length |
166
|
|
|
|
|
|
|
? substr($url,0,$Test::WWW::display_length) . "..." |
167
|
|
|
|
|
|
|
: $url; |
168
|
|
|
|
|
|
|
} |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
sub user_agent { |
171
|
|
|
|
|
|
|
my $agent = shift || "Windows IE 6"; |
172
|
|
|
|
|
|
|
$Mech->agent_alias($agent); |
173
|
|
|
|
|
|
|
} |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
sub mech { |
176
|
|
|
|
|
|
|
my ($self) = @_; |
177
|
|
|
|
|
|
|
return $Mech; |
178
|
|
|
|
|
|
|
} |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
sub last_test { |
181
|
|
|
|
|
|
|
my($self) = @_; |
182
|
|
|
|
|
|
|
return ($Test->details)[-1]; |
183
|
|
|
|
|
|
|
} |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
sub cache (;$) { |
186
|
|
|
|
|
|
|
my $comment = shift; |
187
|
|
|
|
|
|
|
$Test->diag($comment) if defined $comment; |
188
|
|
|
|
|
|
|
$last_url = ""; |
189
|
|
|
|
|
|
|
$cache_results = 1; |
190
|
|
|
|
|
|
|
1; |
191
|
|
|
|
|
|
|
} |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
sub no_cache (;$) { |
194
|
|
|
|
|
|
|
my $comment = shift; |
195
|
|
|
|
|
|
|
$Test->diag($comment) if defined $comment; |
196
|
|
|
|
|
|
|
$last_url = ""; |
197
|
|
|
|
|
|
|
$cache_results = 0; |
198
|
|
|
|
|
|
|
1; |
199
|
|
|
|
|
|
|
} |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
1; |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
__END__ |