1use Test::More tests => 4; 2use strict; 3use HTTP::Proxy; 4use HTTP::Proxy::BodyFilter::simple; 5use t::Utils; 6 7# test configuration 8my $test = Test::Builder->new; 9$test->use_numbers(0); 10$test->no_ending(1); 11 12# create the filter 13my $sub = sub { 14 my ( $self, $dataref, $message, $protocol, $buffer ) = @_; 15 $$dataref =~ s/test/foo/g; 16}; 17 18my $filter = HTTP::Proxy::BodyFilter::simple->new($sub); 19 20# create the proxy 21my $proxy = HTTP::Proxy->new( 22 port => 0, 23 max_clients => 0, 24 max_keep_alive_requests => 1, 25 max_connections => 1, 26); 27$proxy->init; 28$proxy->agent->protocols_allowed(undef); 29$proxy->push_filter( response => $filter, scheme => 'file', mime => 'text/*' ); 30my $url = $proxy->url; 31 32# fork the proxy 33my @pids; 34{ 35 $^W = 0; # warning due to the absence of a host in the file URL 36 push @pids, fork_proxy($proxy); 37} 38 39# check that the correct transformation is applied 40my $ua = LWP::UserAgent->new(); 41$ua->proxy( file => $url ); 42my $response = $ua->request( HTTP::Request->new( GET => 'file:t/test.html' ) ); 43 44my $file; 45{ 46 local $/ = undef; 47 open F, "t/test.html" or diag "Unable to open t/test.html"; 48 $file = <F>; 49 close F; 50} 51$file =~ s/test/foo/g; 52is( $response->content, $file, "The proxy applied the transformation" ); 53 54# push another filter 55$sub = sub { 56 my ( $self, $dataref, $message, $protocol, $buffer ) = @_; 57 $$dataref =~ s/Test/Bar/g; 58}; 59$filter = HTTP::Proxy::BodyFilter::simple->new( 60 filter => $sub, 61 begin => sub { ok( 1, "begin() called" ) }, 62 end => sub { ok( 1, "end() called" ) }, 63); 64$proxy->push_filter( response => $filter, scheme => 'file', mime => 'text/*' ); 65 66# fork the modified proxy 67push @pids, fork_proxy($proxy); 68 69$response = $ua->request( HTTP::Request->new( GET => 'file:t/test.html' ) ); 70$file =~ s/Test/Bar/g; 71is( $response->content, $file, "The proxy applied two transformations" ); 72 73# wait for kids 74wait for @pids; 75 76