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 no warnings 'uninitialized'; 21 local $ENV{all_proxy}; 22 local $ENV{ALL_PROXY}; 23 local $ENV{http_proxy} = $proxy; 24 my $c = HTTP::Tiny->new(); 25 ok(!defined $c->http_proxy); 26} 27 28# trailing / is optional 29for my $proxy ("http://localhost:8080/", "http://localhost:8080"){ 30 local $ENV{http_proxy} = $proxy; 31 my $c = HTTP::Tiny->new(); 32 is($c->http_proxy, $proxy); 33} 34 35# http_proxy must be http://<host>:<port> format 36{ 37 local $ENV{http_proxy} = "localhost:8080"; 38 eval { 39 my $c = HTTP::Tiny->new(); 40 }; 41 like($@, qr{http_proxy URL must be in format http\[s\]://\[auth\@\]<host>:<port>/}); 42} 43 44# Explicitly disable proxy 45{ 46 local $ENV{all_proxy} = "http://localhost:8080"; 47 local $ENV{http_proxy} = "http://localhost:8080"; 48 local $ENV{https_proxy} = "http://localhost:8080"; 49 my $c = HTTP::Tiny->new( 50 proxy => undef, 51 http_proxy => undef, 52 https_proxy => undef, 53 ); 54 ok(!defined $c->proxy, "proxy => undef disables ENV proxy"); 55 ok(!defined $c->http_proxy, "http_proxy => undef disables ENV proxy"); 56 ok(!defined $c->https_proxy, "https_proxy => undef disables ENV proxy"); 57} 58 59# case variations 60for my $var ( qw/http_proxy https_proxy all_proxy/ ) { 61 my $proxy = "http://localhost:8080"; 62 for my $s ( uc($var), lc($var) ) { 63 local $ENV{$s} = $proxy; 64 my $c = HTTP::Tiny->new(); 65 my $m = ($s =~ /all/i) ? 'proxy' : lc($s); 66 is( $c->$m, $proxy, "set $m from $s" ); 67 } 68} 69 70# ignore HTTP_PROXY with REQUEST_METHOD 71{ 72 # in case previous clean-up failed for some reason 73 delete local @ENV{'http_proxy', 'https_proxy', 'all_proxy', 74 'HTTP_PROXY', 'HTTPS_PROXY', 'ALL_PROXY'}; 75 76 local $ENV{HTTP_PROXY} = "http://localhost:8080"; 77 local $ENV{REQUEST_METHOD} = 'GET'; 78 my $c = HTTP::Tiny->new(); 79 ok(!defined $c->http_proxy, 80 "http_proxy not set from HTTP_PROXY if REQUEST_METHOD set"); 81 82} 83 84# allow CGI_HTTP_PROXY with REQUEST_METHOD 85{ 86 local $ENV{HTTP_PROXY} = "http://localhost:8080"; 87 local $ENV{CGI_HTTP_PROXY} = "http://localhost:9090"; 88 local $ENV{REQUEST_METHOD} = 'GET'; 89 my $c = HTTP::Tiny->new(); 90 is($c->http_proxy, "http://localhost:9090", 91 "http_proxy set from CGI_HTTP_PROXY if REQUEST_METHOD set"); 92} 93 94done_testing(); 95