File: 66htmlparser.t

package info (click to toggle)
libhttp-proxy-perl 0.304-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 720 kB
  • sloc: perl: 2,576; makefile: 4
file content (59 lines) | stat: -rw-r--r-- 1,777 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
use strict;
use Test::More;

BEGIN {
    if ( eval "use HTML::Parser; 1;" ) {
        plan tests => 5;
    }
    else {
        plan skip_all => 'HTML::Parser not installed';
    }
}

use HTTP::Proxy;
use HTTP::Proxy::BodyFilter::htmlparser;

my @results = (
    [
        '<h1>Test</h1>\n<p align="left">foo<br> <i>bar</i></p>',
        '<h1>Test</h1>\n<p align="left">foo<br> <i>bar</i></p>',
        { start => 4, end => 3 }
    ],
    [
        '<h1>Test</h1>\n<p align="left">foo<br> <i>bar</i></p>',
        '<h1></h1><p align="left"><br><i></i></p>',
        { start => 4, end => 3 }
    ],
);

my $filter;
my $count;

# bad initialisation
eval { $filter = HTTP::Proxy::BodyFilter::htmlparser->new("foo"); };
like( $@, qr/^First parameter must be a HTML::Parser/, "Test constructor" );

my $p = HTML::Parser->new;
$p->handler( start          => \&start,          "self,text" );
$p->handler( end            => \&end,            "self,text" );
$p->handler( start_document => \&start_document, "" );

# the handlers
sub start_document { $count = {} }
sub start { $count->{start}++; $_[0]->{output} .= $_[1] }
sub end   { $count->{end}++;   $_[0]->{output} .= $_[1] }

# read-only filter
my $data = shift @results;
$filter = HTTP::Proxy::BodyFilter::htmlparser->new($p);
$filter->filter( \$data->[0], undef, undef, undef );
is_deeply( $data->[0], $data->[1], "Data not modified" );
is_deeply( $data->[2], $count, "Correct number of start and end events" );

# read-write filter (yeah, it's the same)
$data = shift @results;
$filter = HTTP::Proxy::BodyFilter::htmlparser->new( $p, rw => 1 );
$filter->filter( \$data->[0], undef, undef, undef );
is_deeply( $data->[0], $data->[1], "Data modified" );
is_deeply( $data->[2], $count, "Correct number of start and end events" );