1use strict;
2use Test::More tests => 11;
3use HTTP::Proxy;
4use HTTP::Proxy::BodyFilter;
5use HTTP::Proxy::HeaderFilter;
6
7# test the basic filter methods
8my $proxy = HTTP::Proxy->new( port => 0 );
9
10# test the errors
11eval { $proxy->push_filter( 1 ); };
12like( $@, qr/^Odd number of arguments/, "Bad number of parameter" );
13
14eval { $proxy->push_filter( response => 1 ); };
15like( $@, qr/^Not a Filter reference for filter queue/, "Bad parameter" );
16
17eval { $proxy->push_filter( typo => sub { } ); };
18like( $@, qr/^'typo' is not a filter stack/, "Unknown filter stack" );
19
20eval { $proxy->push_filter( mime => 'text', response => sub { } ); };
21like( $@, qr/^Invalid MIME/, "Bad MIME type" );
22
23eval { $proxy->push_filter( method => 'FOO', response => sub { } ); };
24like( $@, qr/^Invalid method: FOO/, "Invalid method: " );
25
26eval { $proxy->push_filter( scheme => 'rstp', response => sub { } ); };
27like( $@, qr/^Unsupported scheme/, "Unsupported scheme" );
28
29# test correct working
30my $filter = HTTP::Proxy::HeaderFilter->new;
31eval { $proxy->push_filter( response => $filter ); };
32is( $@, '', "Accept a HeaderFilter");
33
34{
35  package Foo;
36  use base qw( HTTP::Proxy::HeaderFilter );
37}
38$filter = Foo->new;
39eval { $proxy->push_filter( response => $filter ); };
40is( $@, '', "Accept an object derived from HeaderFilter");
41
42# test multiple match criteria
43eval {
44    $proxy->push_filter(
45        response => $filter,
46        mime     => 'text/*',
47        scheme   => 'http',
48        method   => 'GET'
49    );
50};
51is( $@, "", "Support several match criteria" );
52
53# test pushing multiple filters at once
54# this test breaks encapsulation
55$proxy = HTTP::Proxy->new( port => 0 );
56$filter = HTTP::Proxy::BodyFilter->new;
57my $filter2 = HTTP::Proxy::BodyFilter->new;
58
59$proxy->push_filter( response => $filter, response => $filter2 );
60is( $proxy->{body}{response}{filters}[0][1], $filter, "First filter");
61is( $proxy->{body}{response}{filters}[1][1], $filter2, "Second filter");
62
63