1use Test::More;
2use HTTP::Proxy qw( :log );
3
4my $proxy;
5
6$proxy = HTTP::Proxy->new;
7
8#
9# default values
10#
11
12my %meth = (
13    agent           => undef,
14    chunk           => 4096,
15    daemon          => undef,
16    host            => 'localhost',
17    logfh           => *main::STDERR,
18    max_connections => 0,
19    max_keep_alive_requests => 10,
20    port            => 8080,
21    request         => undef,
22    response        => undef,
23    hop_headers     => undef,
24    logmask         => 0,
25    x_forwarded_for => 1,
26    conn            => 0,
27    client_socket   => undef,
28    # loop is not used/internal for now
29);
30
31plan tests => 15 + keys %meth;
32
33for my $key ( sort keys %meth ) {
34    no strict 'refs';
35    is( $proxy->$key(), $meth{$key}, "$key has the correct default" );
36}
37
38like( $proxy->via(), qr!\(HTTP::Proxy/$HTTP::Proxy::VERSION\)$!,
39      "via has the correct default");
40
41# test deprecated accessors
42$proxy = HTTP::Proxy->new( maxserve => 127,  maxconn => 255 );
43is( $proxy->max_keep_alive_requests, 127, "deprecated maxserve");
44is( $proxy->max_connections, 255, "deprecated maxconn");
45
46#
47# test generated accessors (they're all the same)
48#
49
50is( $proxy->port(8888), $meth{port}, "Set return the previous value" );
51is( $proxy->port, 8888, "Set works" );
52
53#
54# other accessors
55#
56
57$proxy->max_clients( 666 );
58is( $proxy->engine->max_clients, 666, "max_clients correctly delegated" );
59
60# check the url() method
61$proxy->port(0);
62
63# this spits a (normal) warning, but we clean it away
64{
65    local *OLDERR;
66
67    # swap errputs
68    open OLDERR, ">&STDERR" or die "Could not duplicate STDERR: $!";
69    close STDERR;
70
71    # the actual test
72    is( $proxy->url, undef, "We do not have a url yet" );
73
74    # put things back to normal
75    close STDERR;
76    open STDERR, ">&OLDERR" or die "Could not duplicate OLDERR: $!";
77    close OLDERR;
78}
79
80$proxy->_init_daemon;
81ok( $proxy->url =~ '^$http://' . $proxy->host . ':\d+/$', "url looks good" );
82
83# check the timeout
84$proxy->_init_agent;
85is( $proxy->agent->timeout, 60, "Default agent timeout of 60 secs" );
86is( $proxy->timeout(120), 60, "timeout() returns the old value" );
87is( $proxy->agent->timeout, 120, "New agent timeout value of 120 secs" );
88
89#
90# the known_methods() method
91#
92my @all  = $proxy->known_methods();
93my @http = $proxy->known_methods('HTTP');
94is_deeply(
95    \@http,
96    [ $proxy->known_methods('http') ],
97    'known_methods() is case insensitive'
98);
99my %dav   = map { $_ => 1 } $proxy->known_methods('webdav');
100my %delta = map { $_ => 1 } $proxy->known_methods('DelTaV');
101is( scalar grep( { $dav{$_} } @http ), scalar @http, 'WebDAV contains HTTP' );
102is( scalar grep( { $delta{$_} } keys %dav ),
103    scalar keys %dav,
104    'DeltaV contains WebDAV'
105);
106my %all = ( %dav, %delta, map { $_ => 1 } @http );
107is_deeply(
108    [ sort keys %all ],
109    [ sort @all ],
110    'know_methods() returns all methods'
111);
112
113