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