1use Test::More tests => 14; 2use strict; 3use HTTP::Proxy::BodyFilter::simple; 4 5my ( $filter, $sub ); 6 7# error checking 8eval { $filter = HTTP::Proxy::BodyFilter::simple->new() }; 9like( $@, qr/^Constructor called without argument/, "Need at least one arg" ); 10 11eval { $filter = HTTP::Proxy::BodyFilter::simple->new("foo") }; 12like( $@, qr/^Single parameter must be a CODE reference/, "Single coderef" ); 13 14eval { $filter = HTTP::Proxy::BodyFilter::simple->new( filter => "foo" ) }; 15like( $@, qr/^Parameter to filter must be a CODE reference/, "Need coderef" ); 16 17eval { $filter = HTTP::Proxy::BodyFilter::simple->new( typo => sub { } ); }; 18like( $@, qr/Unkown method typo/, "Incorrect method name" ); 19 20for (qw( filter begin end )) { 21 eval { 22 $filter = HTTP::Proxy::BodyFilter::simple->new( $_ => sub { } ); 23 }; 24 is( $@, '', "Accept $_" ); 25} 26 27$sub = sub { 28 my ( $self, $dataref, $message, $protocol, $buffer ) = @_; 29 $$dataref =~ s/foo/bar/g; 30}; 31 32$filter = HTTP::Proxy::BodyFilter::simple->new($sub); 33is( $filter->can('filter'), $sub, "filter() runs the correct filter" ); 34ok( $filter->will_modify(), 'will_modify() defaults to true' ); 35 36# will_modify() 37$filter = HTTP::Proxy::BodyFilter::simple->new( filter => $sub, 38 will_modify => 42 ); 39is( $filter->will_modify(), 42, 'will_modify() returns the given data' ); 40 41# test the filter 42for ( 43 [ "\nfoo\n", "", "\nbar\n", "" ], 44 HTTP::Proxy::BodyFilter::simple->new( end => sub {} ), 45 [ "\nfoo\n", "", "\nfoo\n", "" ], 46 ) 47{ 48 $filter = $_, next if ref $_ eq 'HTTP::Proxy::BodyFilter::simple'; 49 50 my ( $data, $buffer ) = @$_[ 0, 1 ]; 51 $filter->filter( \$data, undef, undef, 52 ( defined $buffer ? \$buffer : undef ) ); 53 is( $data, $_->[2], "Correct data" ); 54 is( $buffer, $_->[3], "Correct buffer" ); 55} 56 57