1use strict; 2use Test::More; 3use HTTP::Proxy; 4use HTTP::Proxy::BodyFilter::htmlparser; 5 6if ( eval "use HTML::Parser; 1;" ) { 7 plan tests => 5; 8} 9else { 10 plan skip_all => 'HTML::Parser not installed'; 11} 12 13my @results = ( 14 [ 15 '<h1>Test</h1>\n<p align="left">foo<br> <i>bar</i></p>', 16 '<h1>Test</h1>\n<p align="left">foo<br> <i>bar</i></p>', 17 { start => 4, end => 3 } 18 ], 19 [ 20 '<h1>Test</h1>\n<p align="left">foo<br> <i>bar</i></p>', 21 '<h1></h1><p align="left"><br><i></i></p>', 22 { start => 4, end => 3 } 23 ], 24); 25 26my $filter; 27my $count; 28 29# bad initialisation 30eval { $filter = HTTP::Proxy::BodyFilter::htmlparser->new("foo"); }; 31like( $@, qr/^First parameter must be a HTML::Parser/, "Test constructor" ); 32 33my $p = HTML::Parser->new; 34$p->handler( start => \&start, "self,text" ); 35$p->handler( end => \&end, "self,text" ); 36$p->handler( start_document => \&start_document, "" ); 37 38# the handlers 39sub start_document { $count = {} } 40sub start { $count->{start}++; $_[0]->{output} .= $_[1] } 41sub end { $count->{end}++; $_[0]->{output} .= $_[1] } 42 43# read-only filter 44my $data = shift @results; 45$filter = HTTP::Proxy::BodyFilter::htmlparser->new($p); 46$filter->filter( \$data->[0], undef, undef, undef ); 47is_deeply( $data->[0], $data->[1], "Data not modified" ); 48is_deeply( $data->[2], $count, "Correct number of start and end events" ); 49 50# read-write filter (yeah, it's the same) 51$data = shift @results; 52$filter = HTTP::Proxy::BodyFilter::htmlparser->new( $p, rw => 1 ); 53$filter->filter( \$data->[0], undef, undef, undef ); 54is_deeply( $data->[0], $data->[1], "Data modified" ); 55is_deeply( $data->[2], $count, "Correct number of start and end events" ); 56 57