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 tests => $tests;
34use HTTP::Proxy;
35use HTTP::Request::Common;
36use t::Utils;
37
38my $base = 'http://diveintomark.org/tests/client/http';
39
40SKIP:
41{
42    skip "$base is not available", $tests unless web_ok( $base );
43
44    # $tests + 2, because of the duplicate 401
45    my $proxy = HTTP::Proxy->new(
46        port                    => 0,
47        max_keep_alive_requests => $tests + 2,
48        max_connections         => 1,
49    );
50    $proxy->init;
51
52    # the auto-authenticating client
53    {
54        package MyUA;
55        use base qw( LWP::UserAgent );
56
57        my %credentials = (
58            "Use test/basic" => [ "test", "basic" ],
59            "DigestTest"     => [ "test", "digest" ],
60        );
61
62        sub get_basic_credentials {
63            my($self, $realm, $uri) = @_;
64            return @{$credentials{$realm}};
65        }
66    }
67
68    my $ua = MyUA->new( keep_alive => 1 );
69    $ua->proxy( http => $proxy->url );
70
71    # fork the proxy
72    my $pid = fork_proxy($proxy);
73
74    # check all those pages
75    for (@url) {
76        my ( $doc, $status, $status2, $realm, $user, $pass ) = @$_;
77        my $res = $ua->simple_request( GET "$base/$doc" );
78        is( $res->code, $status, "$doc => $status" );
79
80        # redirection
81        if ( $res->is_redirect && $status2 ) {
82            $res = $ua->simple_request( GET $res->header('Location') );
83            is( $res->code, $status2, "$doc => $status2 (redirect)" );
84        }
85
86        # authentication
87        if ( $res->code == 401 ) {
88            # this request is actually two requests (401/200)
89            $res = $ua->request( GET "$base/$doc" );
90            is( $res->code, $status2, "$doc => $status2 (auth)" );
91        }
92    }
93
94    # wait for the proxy
95    wait;
96}
97