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 52 53 54 55 56 57 58 59 60
|
use strict;
use Test::More;
use t::Utils;
use LWP::UserAgent;
use HTTP::Proxy;
# here are all the requests the client will try
my @requests = (
[ 'www.mongueurs.net', '/', 200 ],
[ 'httpd.apache.org', '/docs', 301 ],
[ 'www.google.com', '/testing/', 404 ],
[ 'www.error.zzz', '/', 500 ],
);
if( $^O eq 'MSWin32' ) {
plan skip_all => "This test fails on MSWin32. HTTP::Proxy is usable on Win32 with maxchild => 0";
exit;
}
# we skip the tests if the network is not available
my $web_ok = web_ok();
plan tests => @requests + 2;
# this is to work around tests in forked processes
my $test = Test::Builder->new;
$test->use_numbers(0);
$test->no_ending(1);
my $proxy = HTTP::Proxy->new( port => 9990, maxconn => @requests * $web_ok + 1 );
$proxy->init; # required to access the url later
# fork a HTTP proxy
my $pid = fork_proxy(
$proxy,
sub {
is( $proxy->conn, @requests * $web_ok + 1,
"Served the correct number of requests" );
}
);
# no Host: header
my $content = bare_request( '/', HTTP::Headers->new(), $proxy );
my ($code) = $content =~ m!^HTTP/\d+\.\d+ (\d\d\d) !g;
is( $code, 400, "Relative URL and no Host: Bad Request" );
SKIP: {
skip "Web does not seem to work", scalar @requests unless $web_ok;
for (@requests) {
$content = bare_request(
$_->[1], HTTP::Headers->new( Host => $_->[0]), $proxy
);
($code) = $content =~ m!^HTTP/\d+\.\d+ (\d\d\d) !g;
is( $code, $_->[2], "Got an answer (@{[$code]})" );
}
}
# make sure the kid is dead
wait;
|