1use strict;
2use warnings;
3use Test::More;
4use HTTP::Proxy;
5use HTTP::Proxy::BodyFilter::complete;
6use HTTP::Proxy::BodyFilter::simple;
7
8my @data = (
9    'miny hollers the let tiger catch meeny a he him',
10    'joy beamish flame gyre o blade came callay jaws vorpal',
11    'xvi vigor nvi Bvi trived Elvis levee viper e3 PVIC',
12    'Wizzle Hunny_Bee Alexander_Beetle Owl Woozle Eeyore Backson',
13    'necessitatibus lorem aperiam facere consequuntur incididunt similique'
14);
15my $full = join '', @data;
16
17plan tests => 1 + @data;
18
19# some variables
20my $proxy = HTTP::Proxy->new( port => 0 );
21$proxy->push_filter(
22    response => HTTP::Proxy::BodyFilter::complete->new(),
23    response => HTTP::Proxy::BodyFilter::simple->new(
24        sub {
25            my ( $self, $dataref, $message, $protocol, $buffer ) = @_;
26            if ( defined $buffer ) {
27                is( $$dataref, '', 'Empty chunk of data' );
28            }
29            else {
30                is( $$dataref, $full, 'Full data in one big chunk' );
31            }
32        }
33    ),
34);
35
36# set up a fake request/response set
37my $res =
38  HTTP::Response->new( 200, 'OK',
39    HTTP::Headers->new( 'Content-Type' => 'text/html' ), 'dummy' );
40$res->request( HTTP::Request->new( GET => 'http://www.example.com/' ) );
41$proxy->request( $res->request );
42$proxy->response($res);
43
44# run the data through the filters
45$proxy->{body}{response}->select_filters($res);
46
47for my $data (@data) {
48    $proxy->{body}{response}->filter( \$data, $res, '' );
49}
50
51# finalize
52my $data = '';
53$proxy->{body}{response}->filter_last( \$data, $res, '' );
54
55