script/mech-dump

filter: Statementstmt 58.9% 43 / 73 Branchbran 46.1% 12 / 26 Conditioncond 16.6% 1 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 44.4% 8 / 18 Podpod n/a totaltotal 50.3% 64 / 127 CC 1 SCAR 4.4
CC
1
Cov
53%
__ANON__
2.0
__ANON__
2.0
__ANON__
2.0
CRAP 1.6
« lib/WWW/Mechanize/Image.pm ↑ Summary lib/WWW/Mechanize/Link.pm »
1 #!/usr/local/bin/perl -w
2  
3 =head1 NAME
4  
5 mech-dump - Dumps information about a web page
6  
7 =cut
8  
9 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use WWW::Mechanize ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Pod::Usage;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 ▶use HTTP::Cookies;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 my @actions;
17 my $absolute;
18  
19 1 my $user;
20 1 my $pass;
21 1 my $agent;
22 1 my $agent_alias;
23 1 my $cookie_filename;
24  
25 GetOptions(
26 'user=s' => \$user,
27 'password=s' => \$pass,
28 0 ▶ headers => sub { push( @actions, \&dump_headers ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
29 1 ▶ forms => sub { push( @actions, \&dump_forms ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
30 0 ▶ links => sub { push( @actions, \&dump_links ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
31 0 ▶ images => sub { push( @actions, \&dump_images ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
32 0 ▶ all => sub { push( @actions, \&dump_headers, \&dump_forms, \&dump_links, \&dump_images ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
33 0 ▶ text => sub { push( @actions, \&dump_text ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
34 absolute => \$absolute,
35 'agent=s' => \$agent,
36 'agent-alias=s' => \$agent_alias,
37 'cookie-file=s' => \$cookie_filename,
38 0 ▶ help => sub { pod2usage(1); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
39 1 ▶) or pod2usage(2);
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions("user=s", \$user, "password=s", \$pass, "headers", sub { push @actions, \&main::dump_headers; } , "forms", sub { push @actions, \&main::dump_forms; } , "links", sub { push @actions, \&main::dump_links; } , "images", sub { push @actions, \&main::dump_images; } , "all", sub { push @actions, \&main::dump_headers, \&main::dump_forms, \&main::dump_links, \&main::dump_images; } , "text", sub { push @actions, \&main::dump_text; } , "absolute", \$absolute, "agent=s", \$agent, "agent-alias=s", \$agent_alias, "cookie-file=s", \$cookie_filename, "help", sub { pod2usage(1); } ) or pod2usage(2) 0 1 1
40  
41 =head1 SYNOPSIS
42  
43 mech-dump [options] [file|url]
44  
45 Options:
46  
47 --headers Dump HTTP response headers
48 --forms Dump table of forms (default action)
49 --links Dump table of links
50 --images Dump table of images
51 --all Dump all four of the above, in that order
52  
53 --text Dumps the textual part of the web page
54  
55 --user=user Set the username
56 --password=pass Set the password
57 --cookie-file=filename Set the filename to use for persistent cookies
58  
59 --agent=agent Specify the UserAgent to pass
60 --agent-alias=alias
61 Specify the alias for the UserAgent to pass.
62 Pick one of:
63 * Windows IE 6
64 * Windows Mozilla
65 * Mac Safari
66 * Mac Mozilla
67 * Linux Mozilla
68 * Linux Konqueror
69  
70 --absolute Show URLs as absolute, even if relative in the page
71 --help Show this message
72  
73 The order of the options specified is relevant. Repeated options
74 get repeated dumps.
75  
76 =cut
77  
78 1 ▶my $uri = shift or die "Must specify a URL or file to check. See --help for details.\n";
Branch: 1 branch
BranchTrueFalseTotal
my $uri = shift @ARGV or die "Must specify a URL or file to check. See --help for details.\n" 0 1 1
79 1 ▶if ( -e $uri ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $uri) 0 1 1
80 0 require URI::file;
81 0 $uri = URI::file->new_abs( $uri )->as_string;
82 }
83  
84 1 ▶@actions = (\&dump_forms) unless @actions;
Branch: 1 branch
BranchTrueFalseTotal
unless @actions 0 1 1
85  
86 1 my $mech = WWW::Mechanize->new();
87 1 ▶if ( defined $agent ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $agent) { } 0 1 1
elsif (defined $agent_alias) { } 0 1 1
88 0 $mech->agent( $agent );
89 }
90 elsif ( defined $agent_alias ) {
91 0 $mech->agent_alias( $agent_alias );
92 }
93 1 ▶if ( defined $cookie_filename ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cookie_filename) { } 0 1 1
94 0 my $cookies = HTTP::Cookies->new( file => $cookie_filename, autosave => 1, ignore_discard => 1 );
95 0 $cookies->load() ;
96 0 $mech->cookie_jar($cookies);
97 }
98 else {
99 1 $mech->cookie_jar(undef) ;
100 }
101  
102 1 $mech->env_proxy();
103 1 my $response = $mech->get( $uri );
104 1 ▶if (!$response->is_success and defined ($response->www_authenticate)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $response->is_success and defined $response->www_authenticate) 0 1 1
Condition33%1/3
not $response->is_success and defined $response->www_authenticate
!ll&&!rl&&r
100
MC/DC0%0/2
not $response->is_success and defined $response->www_authenticate
not $response->is_success defined $response->www_authenticate
Truth table33%1/3
not(A) and B
A= not $response->is_successB= defined $response->www_authenticate
ABresult
0X0
100
111
105 0 ▶ if (!defined $user or !defined $pass) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $user and defined $pass) 0 0 0
Condition0%0/3
defined $user and defined $pass
!ll&&!rl&&r
000
MC/DC0%0/2
defined $user and defined $pass
defined $user defined $pass
Truth table0%0/3
A and B
A= defined $userB= defined $pass
ABresult
0X0
100
111
106 0 die("Page requires username and password, but none specified.\n");
107 }
108 0 $mech->credentials($user,$pass);
109 0 $response = $mech->get( $uri );
110 0 ▶ $response->is_success or die "Can't fetch $uri with username and password\n", $response->status_line, "\n";
Branch: 1 branch
BranchTrueFalseTotal
$response->is_success or die "Can't fetch $uri with username and password\n", $response->status_line, "\n" 0 0 0
111 }
112 1 ▶$mech->is_html or die qq{$uri returns type "}, $mech->ct, qq{", not "text/html"\n};
Branch: 1 branch
BranchTrueFalseTotal
$mech->is_html or die qq[$uri returns type "], $mech->ct, "\", not \"text/html\"\n" 0 1 1
113  
114 1 ▶while ( my $action = shift @actions ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $action = shift @actions) 1 1 2
115 1 $action->( $mech );
116 1 ▶ print "\n" if @actions;
Branch: 1 branch
BranchTrueFalseTotal
if @actions 0 1 1
117 }
118  
119  
120 sub dump_headers {
121 0 ▶ my $mech = shift;
Subroutine dump_headers: not called CC=1 SCAR=6.9
122 0 $mech->dump_headers( undef );
123 0 return;
124 }
125  
126 sub dump_forms {
127 1 ▶ my $mech = shift;
Subroutine dump_forms: called CC=1 SCAR=0.0
128 1 $mech->dump_forms( undef, $absolute );
129 1 return;
130 }
131  
132 sub dump_links {
133 0 ▶ my $mech = shift;
Subroutine dump_links: not called CC=1 SCAR=6.9
134 0 $mech->dump_links( undef, $absolute );
135 0 return;
136 }
137  
138 sub dump_images {
139 0 ▶ my $mech = shift;
Subroutine dump_images: not called CC=1 SCAR=6.9
140 0 $mech->dump_images( undef, $absolute );
141 0 return;
142 }
143  
144 sub dump_text {
145 0 ▶ my $mech = shift;
Subroutine dump_text: not called CC=1 SCAR=6.9
146 0 $mech->dump_text();
147 0 return;
148 }
149  
150 =head1 SEE ALSO
151  
152 L<WWW::Mechanize>
« lib/WWW/Mechanize/Image.pm ↑ Summary lib/WWW/Mechanize/Link.pm »