1use strict;
2use Test::More;
3use LWP::UserAgent;
4use HTTP::Proxy;
5use t::Utils;    # some helper functions for the server
6
7if( $^O eq 'MSWin32' ) {
8    plan skip_all => "This test fails on MSWin32. HTTP::Proxy is usable on Win32 with maxchild => 0";
9    exit;
10}
11
12plan tests => 4;
13
14my $test = Test::Builder->new;
15my @pids;
16
17# this is a rather long test suite just to test that
18# $proxy->via() works ok
19
20# this is to work around tests in forked processes
21$test->use_numbers(0);
22$test->no_ending(1);
23
24# create a HTTP::Daemon (on an available port)
25my $server = server_start();
26
27# create and fork the proxy
28# the proxy itself will not fork
29my $proxy = HTTP::Proxy->new( port => 0, max_connections => 1, max_clients => 0 );
30$proxy->init;    # required to access the url later
31$proxy->agent->no_proxy( URI->new( $server->url )->host );
32push @pids, fork_proxy($proxy);
33
34# fork the HTTP server
35my $pid = fork;
36die "Unable to fork web server" if not defined $pid;
37
38if ( $pid == 0 ) {
39
40    # the answer method
41    my $answer1 = sub {
42        my ( $req, $data ) = @_;
43        isnt( $req->headers->header('Via'), undef, "Server says Via: header added" );
44        return HTTP::Response->new(
45            200, 'OK',
46            HTTP::Headers->new( 'Content-Type' => 'text/plain' ),
47            "Headers checked."
48        );
49    };
50    my $answer2 = sub {
51        my ( $req, $data ) = @_;
52        is( $req->headers->header('Via'), undef, "Server says no Via: header added" );
53        return HTTP::Response->new(
54            200, 'OK',
55            HTTP::Headers->new( 'Content-Type' => 'text/plain' ),
56            "Headers checked."
57        );
58    };
59
60    # let's return some files when asked for them
61    server_next( $server, $answer1 );
62    server_next( $server, $answer2 );
63
64    exit 0;
65}
66
67push @pids, $pid;
68
69# run a client
70my ( $req, $res );
71my $ua = LWP::UserAgent->new;
72$ua->proxy( http => $proxy->url );
73
74# send a Proxy-Connection header
75$req = HTTP::Request->new( GET => $server->url );
76$res = $ua->simple_request($req);
77isnt( $res->headers->header('Via'), undef, "Client says Via: header added" );
78
79# create and fork the proxy
80$proxy->via('');
81push @pids, fork_proxy($proxy);
82$res = $ua->simple_request($req);
83is( $res->headers->header('Via'), undef, "Client says no Via: header added" );
84
85
86# make sure both kids are dead
87wait for @pids;
88
89