| 1 |
use strict; |
| 2 |
use warnings; |
| 3 |
use Furl::HTTP; |
| 4 |
use Test::TCP; |
| 5 |
use Test::Requires qw(Plack::Request HTTP::Body), 'Plack'; |
| 6 |
use Plack::Loader; |
| 7 |
use Test::More; |
| 8 |
use Plack::Request; |
| 9 |
use Test::Requires qw(Plack::Request HTTP::Body), 'HTTP::Proxy'; |
| 10 |
use Socket qw(AF_INET); |
| 11 |
|
| 12 |
plan tests => (10*2 + 8)*3; |
| 13 |
|
| 14 |
my $verbose = 1; |
| 15 |
{ |
| 16 |
package Test::HTTP::Proxy; |
| 17 |
use parent qw(HTTP::Proxy); |
| 18 |
use HTTP::Daemon; |
| 19 |
|
| 20 |
sub new { |
| 21 |
my $self = shift; |
| 22 |
my %args = @_; |
| 23 |
my %daemon_args = ( |
| 24 |
LocalAddr => '127.0.0.1', |
| 25 |
LocalPort => $args{port}, |
| 26 |
ReuseAddr => 1, |
| 27 |
Family => Socket::AF_INET, |
| 28 |
); |
| 29 |
my $daemon = HTTP::Daemon->new(%daemon_args); |
| 30 |
$self->SUPER::new(@_, daemon => $daemon); |
| 31 |
} |
| 32 |
sub log { |
| 33 |
my($self, $level, $prefix, $msg) = @_; |
| 34 |
::note "$prefix: $msg" if $verbose; |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
{ |
| 39 |
package Test::UserAgent; |
| 40 |
use parent qw(LWP::UserAgent); |
| 41 |
use Test::More; |
| 42 |
|
| 43 |
sub real_httpd_port { |
| 44 |
my ($self, $port) = @_; |
| 45 |
$self->{httpd_port} = $port if defined $port; |
| 46 |
return $self->{httpd_port}; |
| 47 |
} |
| 48 |
|
| 49 |
sub simple_request { |
| 50 |
my ($self, $req, @args) = @_; |
| 51 |
my $uri = $req->uri; |
| 52 |
my $host = $req->header('Host'); |
| 53 |
|
| 54 |
if ($self->real_httpd_port) { |
| 55 |
# test for URL with a default port |
| 56 |
like $uri.q(), qr!^http://[^:]+/!, |
| 57 |
'No port number in the request line'; |
| 58 |
unlike $host, qr!:!, |
| 59 |
'No port number in Host header'; |
| 60 |
|
| 61 |
# replace the port number to correctly connect to the test server |
| 62 |
$uri->port($self->real_httpd_port); |
| 63 |
} else { |
| 64 |
# test for URL with non-default port |
| 65 |
|
| 66 |
like $uri.q(), qr!^http://[^/]+:[0-9]+/!, |
| 67 |
'A port number in the request line'; |
| 68 |
like $host, qr/:[0-9]+$/, |
| 69 |
'A port number in Host header'; |
| 70 |
} |
| 71 |
|
| 72 |
return $self->SUPER::simple_request($req, @args); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
my $via = "VIA!VIA!VIA!"; |
| 77 |
|
| 78 |
my $httpd = Test::TCP->new(code => sub { |
| 79 |
my $httpd_port = shift; |
| 80 |
Plack::Loader->auto(port => $httpd_port)->run(sub { |
| 81 |
my $env = shift; |
| 82 |
|
| 83 |
my $req = Plack::Request->new($env); |
| 84 |
is $req->path, '/foo'; |
| 85 |
is $req->header('X-Foo'), "ppp"; |
| 86 |
like $req->header('User-Agent'), qr/\A Furl::HTTP /xms; |
| 87 |
my $content = "Hello, foo"; |
| 88 |
return [ 200, |
| 89 |
[ 'Content-Length' => length($content) ], |
| 90 |
[ $content ] |
| 91 |
]; |
| 92 |
}); |
| 93 |
}); |
| 94 |
|
| 95 |
sub client (%) { |
| 96 |
my (%args) = @_; |
| 97 |
for (1..3) { # run some times for testing keep-alive. |
| 98 |
my $furl = Furl::HTTP->new(proxy => $args{proxy}); |
| 99 |
my ( undef, $code, $msg, $headers, $content ) = |
| 100 |
$furl->request( |
| 101 |
url => $args{request}, |
| 102 |
headers => [ "X-Foo" => "ppp" ] |
| 103 |
); |
| 104 |
is $code, 200, "request()"; |
| 105 |
is $msg, "OK"; |
| 106 |
is Furl::HTTP::_header_get($headers, 'Content-Length'), 10; |
| 107 |
is Furl::HTTP::_header_get($headers, 'Via'), $args{via}; |
| 108 |
is $content, 'Hello, foo' |
| 109 |
or do{ require Devel::Peek; Devel::Peek::Dump($content) }; |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
sub test_agent () { |
| 114 |
return Test::UserAgent->new( |
| 115 |
env_proxy => 1, |
| 116 |
keep_alive => 2, |
| 117 |
parse_head => 0, |
| 118 |
); |
| 119 |
} |
| 120 |
|
| 121 |
local $ENV{'HTTP_PROXY'} = ''; |
| 122 |
|
| 123 |
# Request target with non-default port |
| 124 |
|
| 125 |
test_tcp( |
| 126 |
client => sub { |
| 127 |
my $proxy_port = shift; |
| 128 |
my $httpd_port = $httpd->port; |
| 129 |
client( |
| 130 |
proxy => "http://127.0.0.1:$proxy_port", |
| 131 |
request => "http://127.0.0.1:$httpd_port/foo", |
| 132 |
via => '1.0 VIA!VIA!VIA!', |
| 133 |
); |
| 134 |
}, |
| 135 |
server => sub { # proxy server |
| 136 |
my $proxy_port = shift; |
| 137 |
my $proxy = Test::HTTP::Proxy->new(port => $proxy_port, via => $via); |
| 138 |
$proxy->agent(test_agent); |
| 139 |
$proxy->start(); |
| 140 |
}, |
| 141 |
); |
| 142 |
|
| 143 |
# Request target with default port |
| 144 |
|
| 145 |
test_tcp( |
| 146 |
client => sub { |
| 147 |
my $proxy_port = shift; |
| 148 |
my $httpd_port = $httpd->port; |
| 149 |
client( |
| 150 |
proxy => "http://127.0.0.1:$proxy_port", |
| 151 |
request => "http://127.0.0.1/foo", # default port |
| 152 |
via => '1.0 VIA!VIA!VIA!', |
| 153 |
); |
| 154 |
}, |
| 155 |
server => sub { # proxy server |
| 156 |
my $proxy_port = shift; |
| 157 |
my $proxy = Test::HTTP::Proxy->new(port => $proxy_port, via => $via); |
| 158 |
$proxy->agent(test_agent); |
| 159 |
$proxy->agent->real_httpd_port($httpd->port); |
| 160 |
$proxy->start(); |
| 161 |
}, |
| 162 |
); |
| 163 |
|
| 164 |
# SSL over proxy |
| 165 |
|
| 166 |
test_tcp( |
| 167 |
client => sub { |
| 168 |
# emulate CONNECT for SSL proxying without a real SSL connection |
| 169 |
no warnings 'redefine'; |
| 170 |
local *Furl::HTTP::connect_ssl_over_proxy = sub { |
| 171 |
my ($self, $proxy_host, $proxy_port, $host, $port, $timeout_at, $proxy_authorization) = @_; |
| 172 |
my $sock = $self->connect($proxy_host, $proxy_port, $timeout_at); |
| 173 |
my $p = "CONNECT $host:$port HTTP/1.0\015\012Server: $host\015\012"; |
| 174 |
$p .= "\015\012"; |
| 175 |
$self->write_all($sock, $p, $timeout_at) or fail; |
| 176 |
|
| 177 |
# read the entire response of CONNECT method |
| 178 |
my $buf = ''; |
| 179 |
while ($buf !~ qr!(?:\015\012){2}!) { |
| 180 |
my $read = $self->read_timeout( |
| 181 |
$sock, \$buf, $self->{bufsize}, length($buf), $timeout_at |
| 182 |
); |
| 183 |
defined $read or fail; |
| 184 |
$read != 0 or fail; |
| 185 |
} |
| 186 |
|
| 187 |
$sock; |
| 188 |
}; |
| 189 |
|
| 190 |
my $proxy_port = shift; |
| 191 |
my $httpd_port = $httpd->port; |
| 192 |
client( |
| 193 |
proxy => "http://127.0.0.1:$proxy_port", |
| 194 |
request => "https://127.0.0.1:$httpd_port/foo", |
| 195 |
# no via since the request goes directly to the origin server |
| 196 |
); |
| 197 |
}, |
| 198 |
server => sub { # proxy server |
| 199 |
my $proxy_port = shift; |
| 200 |
my $proxy = Test::HTTP::Proxy->new(port => $proxy_port, via => $via); |
| 201 |
$proxy->start(); |
| 202 |
}, |
| 203 |
); |