1use strict;
2use Test::More;
3use t::Utils;
4use LWP::UserAgent;
5use HTTP::Proxy;
6
7# here are all the requests the client will try
8my @requests = (
9
10    # host, path, expected code, dns should fail
11    [ 'www.mongueurs.net', '/',         200 ],
12    [ 'httpd.apache.org',  '/docs',     301 ],
13    [ 'www.google.com',    '/testing/', 404 ],
14    [ 'www.error.zzz', '/', '5..', 1 ],
15);
16
17if ( $^O eq 'MSWin32' ) {
18    plan skip_all =>
19        "This test fails on MSWin32. HTTP::Proxy is usable on Win32 with maxchild => 0";
20    exit;
21}
22
23# we skip the tests if the network is not available
24my $web_ok = web_ok();
25plan tests => @requests + 2;
26
27# this is to work around tests in forked processes
28my $test = Test::Builder->new;
29$test->use_numbers(0);
30$test->no_ending(1);
31
32my $proxy = HTTP::Proxy->new(
33    port            => 0,
34    max_connections => @requests * $web_ok + 1,
35);
36$proxy->init;    # required to access the url later
37
38# fork a HTTP proxy
39my $pid = fork_proxy(
40    $proxy,
41    sub {
42        is( $proxy->conn,
43            @requests * $web_ok + 1,
44            "Served the correct number of requests"
45        );
46    }
47);
48
49# no Host: header
50my $content = bare_request( '/', HTTP::Headers->new(), $proxy );
51my ($code) = $content =~ m!^HTTP/\d+\.\d+ (\d\d\d) !g;
52is( $code, 400, "Relative URL and no Host: Bad Request" );
53
54SKIP: {
55    skip "Web does not seem to work", scalar @requests unless $web_ok;
56
57    for (@requests) {
58        my ( $host, $path, $status, $dns_fail ) = @$_;
59        $dns_fail &&= defined +( gethostbyname $host )[4];
60
61    SKIP: {
62            if ($dns_fail) {
63                $content = bare_request( '/',
64                    HTTP::Headers->new( Host => 'localhost' ), $proxy );
65                skip "Our DNS shouldn't resolve $host", 1;
66            }
67            else {
68                $content = bare_request( $path,
69                    HTTP::Headers->new( Host => $host ), $proxy );
70                ($code) = $content =~ m!^HTTP/\d+\.\d+ (\d\d\d) !g;
71                like( $code, qr/^$status$/, "Got an answer ($code)" );
72            }
73        }
74    }
75}
76
77# make sure the kid is dead
78wait;
79
80