1use strict;
2use Test::More tests => 28;
3use HTTP::Proxy;
4
5# objects
6my $proxy  = HTTP::Proxy->new( port => 0 );
7$proxy->init;    # needed to setup the filter stacks
8my $filter = HTTP::Proxy::HeaderFilter::standard->new;
9
10# a few hacks because we aren't actually connected
11$filter->proxy($proxy);
12
13{
14    package MockSocket;
15    use vars qw( @ISA );
16    @ISA = qw( IO::Socket::INET );
17    # needed by HTTP::Proxy::HeaderFilter::standard
18    sub peerhost { "1.2.3.4"; }
19}
20$proxy->{client_socket} = MockSocket->new();
21
22# the dummy request
23my $req = HTTP::Request->new( GET => 'http://www.example.com/' );
24$req->header(
25    Proxy_Connection => 'Keep-Alive',
26    Connection       => 'Foo, Bar',
27    Foo              => 'foofoo',
28    Bar              => 'barbar',
29    User_Agent       => 'Foo/1.0'
30);
31
32$filter->filter( $req->headers, $req );
33
34# hop-by-hop
35is( $proxy->hop_headers->header('proxy-connection'),
36    'Keep-Alive', "Hop-by-hop Proxy-Connection" );
37is( $proxy->hop_headers->header('connection'),
38    'Foo, Bar', "Hop-by-hop Connection" );
39is( $proxy->hop_headers->header('Foo'), 'foofoo', "Hop-by-hop Foo" );
40is( $proxy->hop_headers->header('Bar'), 'barbar', "Hop-by-hop Bar" );
41
42# end-to-end
43is( $req->header('user-agent'), 'Foo/1.0', "End-to-end User-Agent" );
44is( $req->header('proxy-connection'), undef, "Connection header removed" );
45is( $req->header('connection'),       undef, "Connection header removed" );
46is( $req->header('Foo'),              undef, "Connection header removed" );
47is( $req->header('Bar'),              undef, "Connection header removed" );
48
49# yet another test
50$req = HTTP::Request->new( GET => 'http://www.example.com/' );
51$req->push_header( Proxy_Connection => 'Keep-Alive' );
52$req->push_header( Connection       => 'Foo' );
53$req->push_header( Connection       => 'Bar' );
54$req->push_header( Foo              => 'foofoo' );
55$req->push_header( Bar              => 'barbar' );
56$req->push_header( User_Agent       => 'Foo/1.0' );
57
58$filter->filter( $req->headers, $req );
59
60# hop-by-hop
61is( $proxy->hop_headers->header('proxy-connection'),
62    'Keep-Alive', "Hop-by-hop Proxy-Connection" );
63is( $proxy->hop_headers->header('connection'),
64    'Foo, Bar', "Hop-by-hop Connection" );
65is( $proxy->hop_headers->header('Foo'), 'foofoo', "Hop-by-hop Foo" );
66is( $proxy->hop_headers->header('Bar'), 'barbar', "Hop-by-hop Bar" );
67
68# end-to-end
69is( $req->header('user-agent'), 'Foo/1.0', "End-to-end User-Agent" );
70is( $req->header('proxy-connection'), undef, "Connection header removed" );
71is( $req->header('connection'),       undef, "Connection header removed" );
72is( $req->header('Foo'),              undef, "Connection header removed" );
73is( $req->header('Bar'),              undef, "Connection header removed" );
74
75# a final test
76$req = HTTP::Request->new( GET => 'http://www.example.com/' );
77$req->push_header( Proxy_Connection => 'Keep-Alive' );
78$req->push_header( Connection       => 'Foo, Bar' );
79$req->push_header( Connection       => 'Baz' );
80$req->push_header( Foo              => 'foofoo' );
81$req->push_header( Bar              => 'barbar' );
82$req->push_header( Baz              => 'bazbaz' );
83$req->push_header( User_Agent       => 'Foo/1.0' );
84
85$filter->filter( $req->headers, $req );
86
87# hop-by-hop
88is( $proxy->hop_headers->header('proxy-connection'),
89    'Keep-Alive', "Hop-by-hop Proxy-Connection" );
90is( $proxy->hop_headers->header('connection'),
91    'Foo, Bar, Baz', "Hop-by-hop Connection" );
92is( $proxy->hop_headers->header('Foo'), 'foofoo', "Hop-by-hop Foo" );
93is( $proxy->hop_headers->header('Bar'), 'barbar', "Hop-by-hop Bar" );
94is( $proxy->hop_headers->header('Baz'), 'bazbaz', "Hop-by-hop Baz" );
95
96# end-to-end
97is( $req->header('user-agent'), 'Foo/1.0', "End-to-end User-Agent" );
98is( $req->header('proxy-connection'), undef, "Connection header removed" );
99is( $req->header('connection'),       undef, "Connection header removed" );
100is( $req->header('Foo'),              undef, "Connection header removed" );
101is( $req->header('Bar'),              undef, "Connection header removed" );
102
103