1#!perl
2
3use strict;
4use warnings;
5
6use File::Basename;
7use Test::More 0.88;
8
9use lib 't';
10use Util qw[ monkey_patch ];
11use HTTP::Tiny;
12
13BEGIN {
14    monkey_patch();
15}
16
17
18# Require a true value
19for my $proxy (undef, "", 0){
20    local $ENV{all_proxy} = undef;
21    local $ENV{ALL_PROXY} = undef;
22    local $ENV{http_proxy} = $proxy;
23    my $c = HTTP::Tiny->new();
24    ok(!defined $c->http_proxy);
25}
26
27# trailing / is optional
28for my $proxy ("http://localhost:8080/", "http://localhost:8080"){
29    local $ENV{http_proxy} = $proxy;
30    my $c = HTTP::Tiny->new();
31    is($c->http_proxy, $proxy);
32}
33
34# http_proxy must be http://<host>:<port> format
35{
36    local $ENV{http_proxy} = "localhost:8080";
37    eval {
38        my $c = HTTP::Tiny->new();
39    };
40    like($@, qr{http_proxy URL must be in format http\[s\]://\[auth\@\]<host>:<port>/});
41}
42
43# Explicitly disable proxy
44{
45    local $ENV{all_proxy} = "http://localhost:8080";
46    local $ENV{http_proxy} = "http://localhost:8080";
47    local $ENV{https_proxy} = "http://localhost:8080";
48    my $c = HTTP::Tiny->new(
49        proxy => undef,
50        http_proxy => undef,
51        https_proxy => undef,
52    );
53    ok(!defined $c->proxy, "proxy => undef disables ENV proxy");
54    ok(!defined $c->http_proxy, "http_proxy => undef disables ENV proxy");
55    ok(!defined $c->https_proxy, "https_proxy => undef disables ENV proxy");
56}
57
58# case variations
59for my $var ( qw/http_proxy https_proxy all_proxy/ ) {
60    my $proxy = "http://localhost:8080";
61    for my $s ( uc($var), lc($var) ) {
62        local $ENV{$s} = $proxy;
63        my $c = HTTP::Tiny->new();
64        my $m = ($s =~ /all/i) ? 'proxy' : lc($s);
65        is( $c->$m, $proxy, "set $m from $s" );
66    }
67}
68
69# ignore HTTP_PROXY with REQUEST_METHOD
70{
71    # in case previous clean-up failed for some reason
72    delete local @ENV{'http_proxy', 'https_proxy', 'all_proxy',
73                      'HTTP_PROXY', 'HTTPS_PROXY', 'ALL_PROXY'};
74
75    local $ENV{HTTP_PROXY} = "http://localhost:8080";
76    local $ENV{REQUEST_METHOD} = 'GET';
77    my $c = HTTP::Tiny->new();
78    ok(!defined $c->http_proxy,
79        "http_proxy not set from HTTP_PROXY if REQUEST_METHOD set");
80
81}
82
83done_testing();
84