File: 61simple.t

package info (click to toggle)
libhttp-proxy-perl 0.304-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster
  • size: 724 kB
  • sloc: perl: 2,576; makefile: 4
file content (56 lines) | stat: -rw-r--r-- 1,796 bytes parent folder | download | duplicates (7)
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
use Test::More tests => 14;
use strict;
use HTTP::Proxy::BodyFilter::simple;

my ( $filter, $sub );

# error checking
eval { $filter = HTTP::Proxy::BodyFilter::simple->new() };
like( $@, qr/^Constructor called without argument/, "Need at least one arg" );

eval { $filter = HTTP::Proxy::BodyFilter::simple->new("foo") };
like( $@, qr/^Single parameter must be a CODE reference/, "Single coderef" );

eval { $filter = HTTP::Proxy::BodyFilter::simple->new( filter => "foo" ) };
like( $@, qr/^Parameter to filter must be a CODE reference/, "Need coderef" );

eval { $filter = HTTP::Proxy::BodyFilter::simple->new( typo => sub { } ); };
like( $@, qr/Unkown method typo/, "Incorrect method name" );

for (qw( filter begin end )) {
    eval {
        $filter = HTTP::Proxy::BodyFilter::simple->new( $_ => sub { } );
    };
    is( $@, '', "Accept $_" );
}

$sub = sub {
    my ( $self, $dataref, $message, $protocol, $buffer ) = @_;
    $$dataref =~ s/foo/bar/g;
};

$filter = HTTP::Proxy::BodyFilter::simple->new($sub);
is( $filter->can('filter'), $sub, "filter() runs the correct filter" );
ok( $filter->will_modify(), 'will_modify() defaults to true' );

# will_modify()
$filter = HTTP::Proxy::BodyFilter::simple->new( filter => $sub,
    will_modify => 42 );
is( $filter->will_modify(), 42, 'will_modify() returns the given data' );

# test the filter
for (
    [ "\nfoo\n", "", "\nbar\n", "" ],
    HTTP::Proxy::BodyFilter::simple->new( end => sub {} ),
    [ "\nfoo\n", "", "\nfoo\n", "" ],
  )
{
    $filter = $_, next if ref $_ eq 'HTTP::Proxy::BodyFilter::simple';

    my ( $data, $buffer ) = @$_[ 0, 1 ];
    $filter->filter( \$data, undef, undef,
        ( defined $buffer ? \$buffer : undef ) );
    is( $data,   $_->[2], "Correct data" );
    is( $buffer, $_->[3], "Correct buffer" );
}