1use strict;
2
3use Test::More tests => 4;
4
5BEGIN {
6    # already tested, but no harm done
7    use_ok( 'Net::SSL' );
8}
9
10my $url = 'https://rt.cpan.org/';
11
12my @prereq;
13
14eval q{ use LWP::UserAgent };
15push @prereq, "LWP::UserAgent" if $@;
16
17eval q{ use HTTP::Request };
18push @prereq, "HTTP::Request" if $@;
19
20my $network_tests;
21if (open IN, '<test.config') {
22    diag("config on $^O");
23    while (<IN>) {
24        chomp;
25        if (my ($key, $value) = ($_ =~ /\A(\S+)\s+(.*)/)) {
26            if ($key eq 'network_tests') {
27                $network_tests = $value;
28            }
29            elsif (grep {$key eq $_} qw(cc inc lib ssl)) {
30                diag("$key $value");
31            }
32        }
33    }
34    close IN;
35}
36
37my $PROXY_ADDR_PORT = 'localhost:3128';
38
39sub live_connect {
40    my $hr = shift;
41    local $ENV{HTTPS_PROXY} = $PROXY_ADDR_PORT;
42
43    # always true if we've been instructed to skip the attempt
44    return 1 unless $network_tests;
45
46    my $sock = Net::SSL->new(
47        PeerAddr => 'rt.cpan.org',
48        PeerPort => 443,
49        Timeout  => 10,
50    );
51
52    return defined($sock) ? 1 : 0;
53    # $sock will be garbage collected and the connection torn down
54}
55
56my $test_name = 'connect through proxy';
57Net::SSL::send_useragent_to_proxy(0);
58eval { live_connect( {chobb => 'schoenmaker'} ) };
59my $err = $@;
60if (length $err == 0) {
61    pass( $test_name );
62    $err = 0;
63}
64else {
65    if ($err =~ /^proxy connect failed: proxy connect to $PROXY_ADDR_PORT failed: / ) {
66        pass( "$test_name - no proxy available" );
67    }
68    else {
69        fail( "$test_name - untrapped error" );
70        diag($@);
71    }
72    $err = 1;
73}
74
75SKIP: {
76    skip( "no proxy found at $PROXY_ADDR_PORT", 1 )
77        if $err;
78
79    Net::SSL::send_useragent_to_proxy(1);
80    my $test_name = 'connect through proxy, forward user agent';
81    eval { live_connect( {chobb => 'schoenmaker'} ) };
82    $err = $@;
83
84    TODO: {
85        if ($network_tests) {
86            local $TODO = "caller stack walk broken (CPAN bug #4759)";
87            is( $err, '', "can forward useragent string to proxy" );
88        }
89        else {
90            pass("can forward useragent string to proxy (network tests disabled)" );
91        }
92    }
93}
94
95SKIP: {
96    my $nr_live_tests = 1;
97    skip( "Cannot load prerequisite modules @prereq", $nr_live_tests ) if @prereq;
98    skip( "Network tests disabled", $nr_live_tests ) unless $network_tests;
99
100    my $ua  = LWP::UserAgent->new;
101    $ua->agent('Crypt-SSLeay tester ');
102    my $req = HTTP::Request->new;
103    my $url = 'https://rt.cpan.org/';
104
105    $req->method('HEAD');
106    $req->uri($url);
107
108    my $test_name = 'HEAD https://rt.cpan.org/';
109    my $res;
110    eval { $res = $ua->request($req) };
111    if ($@) {
112        my $err = $@;
113        fail($test_name);
114        diag("eval error = [$err]");
115    }
116    elsif ($res->is_success) {
117        pass($test_name);
118    }
119    else {
120        fail($test_name);
121        diag("HTTP status = ", $res->status_line);
122        diag("This may not be the fault of the module, $url may be down");
123    }
124}
125