| 1 | package TAP::Parser::SourceHandler::Validator::W3C::HTML; | ||||||||||||||||||||
| 2 | { | ||||||||||||||||||||
| 3 | 1 | $TAP::Parser::SourceHandler::Validator::W3C::HTML::VERSION = '0.02'; | |||||||||||||||||||
| 4 | } | ||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | 1 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | # ABSTRACT: TAP source handler for validating HTML via W3C validator | ||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | 1 | ▶ | use LWP::UserAgent; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | 1 | ▶ | use Test::Builder; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | 1 | ▶ | use URI; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | 1 | ▶ | use TAP::Parser::IteratorFactory; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | 1 | ▶ | use WebService::Validator::HTML::W3C; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 16 | 1 | ▶ | use WWW::Robot; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | 1 | ▶ | use base 'TAP::Parser::SourceHandler'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | 1 | TAP::Parser::IteratorFactory->register_handler(__PACKAGE__); | |||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | 1 | ▶ | use constant VALIDATOR => 'http://validator.w3.org/check'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | # options and their defaults | ||||||||||||||||||||
| 25 | 1 | ▶ | my $crawl = $ENV{TEST_W3C_HTML_CRAWL} || 0; | ||||||||||||||||||
|
Condition50%1/2
$ENV{'TEST_W3C_HTML_CRAWL'} || 0
MC/DC0%0/1
$ENV{'TEST_W3C_HTML_CRAWL'} || 0
$ENV{'TEST_W3C_HTML_CRAWL'}
Truth table50%1/2
A
A= $ENV{'TEST_W3C_HTML_CRAWL'}
| |||||||||||||||||||||
| 26 | 1 | ▶ | my $validator_uri = $ENV{TEST_W3C_HTML_VALIDATOR_URI} || VALIDATOR; | ||||||||||||||||||
|
Condition50%1/2
$ENV{'TEST_W3C_HTML_VALIDATOR_URI'} || 'http://validator.w3.org/check'
MC/DC0%0/1
$ENV{'TEST_W3C_HTML_VALIDATOR_URI'} || 'http://validator.w3.org/check'
$ENV{'TEST_W3C_HTML_VALIDATOR_URI'}
Truth table50%1/2
A
A= $ENV{'TEST_W3C_HTML_VALIDATOR_URI'}
| |||||||||||||||||||||
| 27 | 1 | ▶ | my $timeout = $ENV{TEST_W3C_HTML_TIMEOUT} || 5; | ||||||||||||||||||
|
Condition50%1/2
$ENV{'TEST_W3C_HTML_TIMEOUT'} || 5
MC/DC0%0/1
$ENV{'TEST_W3C_HTML_TIMEOUT'} || 5
$ENV{'TEST_W3C_HTML_TIMEOUT'}
Truth table50%1/2
A
A= $ENV{'TEST_W3C_HTML_TIMEOUT'}
| |||||||||||||||||||||
| 28 | 1 | ▶ | my $use_children = $ENV{TEST_W3C_HTML_CHILDREN} || 0; | ||||||||||||||||||
|
Condition50%1/2
$ENV{'TEST_W3C_HTML_CHILDREN'} || 0
MC/DC0%0/1
$ENV{'TEST_W3C_HTML_CHILDREN'} || 0
$ENV{'TEST_W3C_HTML_CHILDREN'}
Truth table50%1/2
A
A= $ENV{'TEST_W3C_HTML_CHILDREN'}
| |||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | # given a type of test, attempt to parse it and let TAP::Harness know | ||||||||||||||||||||
| 31 | # that we can handle it if it is a valid HTTP URI. | ||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | sub can_handle | ||||||||||||||||||||
| 34 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
can_handle: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||
| 35 | 0 | my $self = shift; | |||||||||||||||||||
| 36 | 0 | my $source = shift; | |||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | 0 | my $meta = $source->meta; | |||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | 0 | ▶ | return 0 unless $meta->{is_scalar}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | # If the extension is htm or html, there's no reason to believe | ||||||||||||||||||||
| 43 | # the content is not markup. If the first line contains the text | ||||||||||||||||||||
| 44 | # "html", it could be markup. If it's any other file, we don't | ||||||||||||||||||||
| 45 | # know what to do with it. | ||||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | 0 | ▶ | if ($meta->{is_file}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 48 | 0 | my $file = $meta->{file}; | |||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | 0 | ▶ | return 0.9 if $file->{lc_ext} =~ /^\.html?$/; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 51 | 0 | ▶ | return 0.5 if $file->{shebang} =~ /html/i; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 52 | 0 | return 0; | |||||||||||||||||||
| 53 | } | ||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | # If it's not a file, try to parse it as a URI. If it's a valid | ||||||||||||||||||||
| 56 | # URI, we'll accept the burden of handling it. | ||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | 0 | my $name = ${ $source->raw }; | |||||||||||||||||||
| 59 | 0 | my $uri = new URI ${ $source->raw }; | |||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | 0 | ▶ | return 1 if $uri->isa('URI::http'); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | 0 | return 0; | |||||||||||||||||||
| 64 | } | ||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | sub make_iterator | ||||||||||||||||||||
| 67 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
make_iterator: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 68 | 0 | my $self = shift; | |||||||||||||||||||
| 69 | 0 | my $source = shift; | |||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | # TODO: Asynchronous validation with forking iterator... | ||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | 0 | my $tap = $self->_check_source($source); | |||||||||||||||||||
| 74 | 0 | my $iter = new TAP::Parser::Iterator::Array [ split /\n+/, $tap ]; | |||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | 0 | return $iter; | |||||||||||||||||||
| 77 | } | ||||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | sub _check_source | ||||||||||||||||||||
| 80 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
_check_source: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 81 | 0 | my $self = shift; | |||||||||||||||||||
| 82 | 0 | my $source = shift; | |||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | 0 | my $meta = $source->meta; | |||||||||||||||||||
| 85 | 0 | my $raw = ${ $source->raw }; | |||||||||||||||||||
| 86 | 0 | my $builder = Test::Builder->create; | |||||||||||||||||||
| 87 | 0 | my $buffer = ''; | |||||||||||||||||||
| 88 | 0 | my $trash = ''; | |||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | 0 | $builder->output(\$buffer); | |||||||||||||||||||
| 91 | 0 | $builder->failure_output(\$trash); | |||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | 0 | ▶ | if ($meta->{is_file}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 94 | 0 | $self->_check_file($builder, $raw) | |||||||||||||||||||
| 95 | } else { | ||||||||||||||||||||
| 96 | 0 | $self->_check_uri($builder, new URI ${ $source->raw }); | |||||||||||||||||||
| 97 | } | ||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | 0 | $builder->done_testing; | |||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | 0 | return $buffer; | |||||||||||||||||||
| 102 | } | ||||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | sub _check_file | ||||||||||||||||||||
| 105 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
_check_file: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 106 | 0 | my $self = shift; | |||||||||||||||||||
| 107 | 0 | my $test = shift; | |||||||||||||||||||
| 108 | 0 | my $file = shift; | |||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | 0 | my $io = new IO::File; | |||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | 0 | ▶ | $io->open($file) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 113 | or $self->_croak("error opening HTML source file '$file': $!"); | ||||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | 0 | $self->_check_content($test, $file, join '', $io->getlines); | |||||||||||||||||||
| 116 | } | ||||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | sub _check_uri | ||||||||||||||||||||
| 119 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
_check_uri: not called CC=6 SCAR=37.4
| |||||||||||||||||||||
| 120 | 0 | my $self = shift; | |||||||||||||||||||
| 121 | 0 | my $builder = shift; | |||||||||||||||||||
| 122 | 0 | my $root = shift; | |||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | # Setup the user agent. | ||||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | 0 | my $ua = new LWP::UserAgent; | |||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | 0 | $ua->timeout($timeout); | |||||||||||||||||||
| 129 | |||||||||||||||||||||
| 130 | # Setup the spider, using any attributes from the environment. I'm | ||||||||||||||||||||
| 131 | # not a huge fan of passing stuff in from the environment, but it's | ||||||||||||||||||||
| 132 | # the way things are typically done inside of test harnesses... | ||||||||||||||||||||
| 133 | |||||||||||||||||||||
| 134 | 0 | my $spider = new WWW::Robot USERAGENT => $ua; | |||||||||||||||||||
| 135 | 0 | ▶ | my $spattrs = { map { /^TEST_W3C_HTML_SPIDER_(.*)/ ? ($1 => $ENV{$_}) : () } keys %ENV }; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | 0 | ▶ | $spattrs->{NAME} ||= __PACKAGE__; # default | ||||||||||||||||||
|
Condition0%0/2
$spattrs->{"NAME"} ||= __PACKAGE__
MC/DC0%0/1
$spattrs->{"NAME"} ||= __PACKAGE__
$spattrs->{"NAME"}
Truth table0%0/2
A
A= $spattrs->{"NAME"}
| |||||||||||||||||||||
| 138 | 0 | ▶ | $spattrs->{VERSION} ||= '1.0'; # default | ||||||||||||||||||
|
Condition0%0/2
$spattrs->{"VERSION"} ||= "1.0"
MC/DC0%0/1
$spattrs->{"VERSION"} ||= "1.0"
$spattrs->{"VERSION"}
Truth table0%0/2
A
A= $spattrs->{"VERSION"}
| |||||||||||||||||||||
| 139 | 0 | ▶ | $spattrs->{EMAIL} ||= 'root@localhost'; # default | ||||||||||||||||||
|
Condition0%0/2
$spattrs->{"EMAIL"} ||= "root\@localhost"
MC/DC0%0/1
$spattrs->{"EMAIL"} ||= "root\@localhost"
$spattrs->{"EMAIL"}
Truth table0%0/2
A
A= $spattrs->{"EMAIL"}
| |||||||||||||||||||||
| 140 | |||||||||||||||||||||
| 141 | 0 | $spider->setAttribute($_ => $spattrs->{$_}) foreach keys %$spattrs; | |||||||||||||||||||
| 142 | |||||||||||||||||||||
| 143 | # Setup the required hooks. I planned on using the invoke-after-get | ||||||||||||||||||||
| 144 | # hook, but the absence of an invoke-on-* handler makes WWW::Robot | ||||||||||||||||||||
| 145 | # croak. So, we'll just use the same damn handler for both content | ||||||||||||||||||||
| 146 | # and error... | ||||||||||||||||||||
| 147 | |||||||||||||||||||||
| 148 | 0 | ▶ | $spider->addHook('follow-url-test' => sub { $self->_spider_follow_url_test($root, @_) }); | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 149 | 0 | ▶ | $spider->addHook('invoke-on-contents' => sub { $self->_spider_handle_response($builder, @_) }); | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 150 | 0 | ▶ | $spider->addHook('invoke-on-get-error' => sub { $self->_spider_handle_response($builder, @_) }); | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 151 | |||||||||||||||||||||
| 152 | # DO EET | ||||||||||||||||||||
| 153 | |||||||||||||||||||||
| 154 | 0 | $spider->run($root); | |||||||||||||||||||
| 155 | } | ||||||||||||||||||||
| 156 | |||||||||||||||||||||
| 157 | sub _spider_follow_url_test | ||||||||||||||||||||
| 158 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
_spider_follow_url_test: not called CC=5 SCAR=34.0
| |||||||||||||||||||||
| 159 | 0 | my $self = shift; | |||||||||||||||||||
| 160 | 0 | my $root = shift; | |||||||||||||||||||
| 161 | 0 | my $robot = shift; | |||||||||||||||||||
| 162 | 0 | my $hook = shift; | |||||||||||||||||||
| 163 | 0 | my $uri = shift; | |||||||||||||||||||
| 164 | |||||||||||||||||||||
| 165 | 0 | ▶ | return 1 if $uri eq $root; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 166 | |||||||||||||||||||||
| 167 | 0 | my $rel = $uri->rel($root); | |||||||||||||||||||
| 168 | |||||||||||||||||||||
| 169 | 0 | ▶ | return 0 if not $crawl; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 170 | 0 | ▶ | return 0 if $rel->scheme; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 171 | 0 | ▶ | return 0 if $rel =~ /^\./; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 172 | 0 | return 1; | |||||||||||||||||||
| 173 | } | ||||||||||||||||||||
| 174 | |||||||||||||||||||||
| 175 | sub _spider_handle_response | ||||||||||||||||||||
| 176 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
_spider_handle_response: not called CC=6 SCAR=37.4
| |||||||||||||||||||||
| 177 | 0 | my $self = shift; | |||||||||||||||||||
| 178 | 0 | my $builder = shift; | |||||||||||||||||||
| 179 | 0 | my $robot = shift; | |||||||||||||||||||
| 180 | 0 | my $hook = shift; | |||||||||||||||||||
| 181 | 0 | my $uri = shift; | |||||||||||||||||||
| 182 | 0 | my $res = shift; | |||||||||||||||||||
| 183 | |||||||||||||||||||||
| 184 | # XXX: We probably need to deal with redirects... | ||||||||||||||||||||
| 185 | |||||||||||||||||||||
| 186 | 0 | ▶ | my $test = $use_children ? $builder->child($uri) : $builder; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 187 | |||||||||||||||||||||
| 188 | 0 | ▶ | $test->plan('no_plan') if $use_children; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 189 | |||||||||||||||||||||
| 190 | 0 | $test->ok($res->is_success, "fetch content for $uri"); | |||||||||||||||||||
| 191 | |||||||||||||||||||||
| 192 | 0 | ▶ | $test->note('ERROR: ' . $res->status_line) if $res->is_error; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 193 | 0 | ▶ | $self->_check_content($test, $uri, $res->content) if $res->is_success; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 194 | |||||||||||||||||||||
| 195 | 0 | ▶ | $test->finalize if $use_children; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 196 | } | ||||||||||||||||||||
| 197 | |||||||||||||||||||||
| 198 | sub _check_content | ||||||||||||||||||||
| 199 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
_check_content: not called CC=5 SCAR=34.0
| |||||||||||||||||||||
| 200 | 0 | my $self = shift; | |||||||||||||||||||
| 201 | 0 | my $test = shift; | |||||||||||||||||||
| 202 | 0 | my $source = shift; | |||||||||||||||||||
| 203 | 0 | my $content = shift; | |||||||||||||||||||
| 204 | |||||||||||||||||||||
| 205 | 0 | my $validator = new WebService::Validator::HTML::W3C | |||||||||||||||||||
| 206 | validator_uri => $validator_uri, | ||||||||||||||||||||
| 207 | detailed => 1; | ||||||||||||||||||||
| 208 | |||||||||||||||||||||
| 209 | 0 | my $checked = $validator->validate(string => $content); | |||||||||||||||||||
| 210 | |||||||||||||||||||||
| 211 | 0 | ▶ | $test->note('ERROR: ' . $validator->validator_error) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 212 | if not $checked; | ||||||||||||||||||||
| 213 | |||||||||||||||||||||
| 214 | 0 | $test->ok($checked, "validate content from $source"); | |||||||||||||||||||
| 215 | 0 | ▶ | $test->ok($checked && $validator->is_valid, "markup is valid for $source"); | ||||||||||||||||||
|
Condition0%0/3
$checked && $validator->is_valid
MC/DC0%0/2
$checked && $validator->is_valid
$checked
$validator->is_valid
Truth table0%0/3
A && B
A= $checkedB= $validator->is_valid
| |||||||||||||||||||||
| 216 | |||||||||||||||||||||
| 217 | 0 | ▶ | my $errors = $checked ? $validator->errors : []; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 218 | |||||||||||||||||||||
| 219 | $test->ok(0, sprintf "L%d:C%d %s", $_->line, $_->col, $_->msg) | ||||||||||||||||||||
| 220 | 0 | foreach @$errors; | |||||||||||||||||||
| 221 | |||||||||||||||||||||
| 222 | 0 | return $checked; | |||||||||||||||||||
| 223 | } | ||||||||||||||||||||
| 224 | |||||||||||||||||||||
| 225 | 1 | 1; | |||||||||||||||||||
| 226 | |||||||||||||||||||||
| 227 | __END__ | ||||||||||||||||||||