1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
|
use Test::More tests => 4;
use strict;
use t::Utils; use HTTP::Proxy;
use LWP::UserAgent;
use IO::Socket::INET;
# test CONNECT
my $test = Test::Builder->new;
# this is to work around tests in forked processes
$test->use_numbers(0);
$test->no_ending(1);
my $host = 'www.perl.org:22';
SKIP:
{
# check if we can actually connect
my $sock = IO::Socket::INET->new( PeerAddr => $host )
or skip "Direct connection to $host impossible", 4;
my $banner = <$sock>;
close $sock;
my $proxy = HTTP::Proxy->new( port => 0, max_connections => 1 );
$proxy->init; # required to access the url later
# fork a HTTP proxy
my $pid = fork_proxy(
$proxy,
sub {
ok( $proxy->conn == 1, "Served the correct number of requests" );
}
);
# run a client
my $ua = LWP::UserAgent->new;
$ua->proxy( https => $proxy->url );
my $req = HTTP::Request->new( CONNECT => "https://$host/" );
my $res = $ua->request($req);
$sock = $res->{client_socket};
my $read;
is( $res->code, 200, "The proxy accepts CONNECT requests" );
ok( $sock->sysread( $read, 100 ), "Read some data from the socket" );
is( $read, $banner, "CONNECTed to the TCP server" );
close $sock;
# make sure the kid is dead
wait;
}
|