1# good place for web client tests:
2# http://diveintomark.org/tests/client/http/
3
4use strict;
5my @url;
6my $tests;
7
8BEGIN {
9    @url = (
10        [ '200.xml', 200 ],
11        [ '220.xml', 220 ],
12        [ '320.xml', 320 ],
13        [ '420.xml', 420 ],
14        [ '520.xml', 520 ],
15        [ '301.xml', 301,  200 ],
16        [ '302.xml', 302,  200 ],
17        [ '307.xml', 307,  200 ],
18        [ '400.xml', 400 ],
19        [ '403.xml', 403 ],
20        [ '404.xml', 404 ],
21        [ '410.xml', 410 ],
22        [ '500.xml', 500 ],
23        [ '300.xml', 300,  200 ],
24        [ '200_basic_auth.xml',  401, 200 ], # these tests actually
25        [ '200_digest_auth.xml', 401, 200 ], # do 401, then 401/200
26        #['200_gzip.xml'],
27        #['etag.xml'],
28        #['lastmodified.xml'],
29    );
30    $tests += @$_ - 1 for @url;
31}
32
33use Test::More;
34use HTTP::Proxy;
35use HTTP::Request::Common;
36use t::Utils;
37
38my $base = 'http://diveintomark.org/tests/client/http';
39
40plan skip_all => "$base seems to have stopped working";
41plan tests => $tests;
42
43SKIP:
44{
45    skip "$base is not available", $tests unless web_ok( $base );
46
47    # $tests + 2, because of the duplicate 401
48    my $proxy = HTTP::Proxy->new(
49        port                    => 0,
50        max_keep_alive_requests => $tests + 2,
51        max_connections         => 1,
52    );
53    $proxy->init;
54
55    # the auto-authenticating client
56    {
57        package MyUA;
58        use base qw( LWP::UserAgent );
59
60        my %credentials = (
61            "Use test/basic" => [ "test", "basic" ],
62            "DigestTest"     => [ "test", "digest" ],
63        );
64
65        sub get_basic_credentials {
66            my($self, $realm, $uri) = @_;
67            return @{$credentials{$realm}};
68        }
69    }
70
71    my $ua = MyUA->new( keep_alive => 1 );
72    $ua->proxy( http => $proxy->url );
73
74    # fork the proxy
75    my $pid = fork_proxy($proxy);
76
77    # check all those pages
78    for (@url) {
79        my ( $doc, $status, $status2, $realm, $user, $pass ) = @$_;
80        my $res = $ua->simple_request( GET "$base/$doc" );
81        is( $res->code, $status, "$doc => $status" );
82
83        # redirection
84        if ( $res->is_redirect && $status2 ) {
85            $res = $ua->simple_request( GET $res->header('Location') );
86            is( $res->code, $status2, "$doc => $status2 (redirect)" );
87        }
88
89        # authentication
90        if ( $res->code == 401 ) {
91            # this request is actually two requests (401/200)
92            $res = $ua->request( GET "$base/$doc" );
93            is( $res->code, $status2, "$doc => $status2 (auth)" );
94        }
95    }
96
97    # wait for the proxy
98    wait;
99}
100