1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
|
use Mojo::Base -strict;
BEGIN {
$ENV{MOJO_PROXY} = 0;
$ENV{MOJO_REACTOR} = 'Mojo::Reactor::Poll';
}
use Test::More;
use Mojo::IOLoop::TLS;
plan skip_all => 'set TEST_ONLINE to enable this test (developer only!)'
unless $ENV{TEST_ONLINE};
plan skip_all => 'IO::Socket::SSL 1.94+ required for this test!'
unless Mojo::IOLoop::TLS->can_tls;
use IO::Socket::INET;
use Mojo::IOLoop;
use Mojo::Transaction::HTTP;
use Mojo::UserAgent;
use Mojolicious::Lite;
use ojo;
get '/remote_address' => sub {
my $c = shift;
$c->render(text => $c->tx->remote_address);
};
# Make sure user agents dont taint the ioloop
my $loop = Mojo::IOLoop->singleton;
my $ua = Mojo::UserAgent->new;
my ($id, $code);
$ua->get(
'http://metacpan.org' => sub {
my ($ua, $tx) = @_;
$id = $tx->connection;
$code = $tx->res->code;
$loop->stop;
}
);
$loop->start;
$ua = undef;
$loop->timer(0.25 => sub { shift->stop });
$loop->start;
ok !$loop->stream($id), 'loop not tainted';
is $code, 301, 'right status';
# Fresh user agent
$ua = Mojo::UserAgent->new;
# Local address
$ua->server->app(app);
my $sock = IO::Socket::INET->new(PeerAddr => 'mojolicious.org', PeerPort => 80);
my $address = $sock->sockhost;
isnt $address, '127.0.0.1', 'different address';
$ua->local_address('127.0.0.1')->max_connections(0);
my $tx = $ua->get('/remote_address');
ok !$ua->ioloop->stream($tx->connection), 'connection is not active';
is $tx->res->body, '127.0.0.1', 'right address';
$ua->local_address($address);
is $ua->get('/remote_address')->res->body, $address, 'right address';
# Fresh user agent
$ua = Mojo::UserAgent->new;
# Connection refused
my $port = Mojo::IOLoop::Server->generate_port;
$tx = $ua->build_tx(GET => "http://127.0.0.1:$port");
$ua->start($tx);
ok $tx->is_finished, 'transaction is finished';
ok $tx->error, 'has error';
# Connection refused (IPv4)
$tx = $ua->build_tx(GET => "http://127.0.0.1:$port");
$ua->start($tx);
ok $tx->is_finished, 'transaction is finished';
ok $tx->error, 'has error';
# Connection refused (IPv6)
$tx = $ua->build_tx(GET => "http://[::1]:$port");
$ua->start($tx);
ok $tx->is_finished, 'transaction is finished';
ok $tx->error, 'has error';
# Host does not exist
$tx = $ua->build_tx(GET => 'http://cdeabcdeffoobarnonexisting.com');
$ua->start($tx);
ok $tx->is_finished, 'transaction is finished';
ok $tx->error, 'has error';
# Fresh user agent again
$ua = Mojo::UserAgent->new;
# Keep-alive
$ua->get('http://mojolicious.org' => sub { Mojo::IOLoop->singleton->stop });
Mojo::IOLoop->singleton->start;
my $kept_alive;
$ua->get(
'http://mojolicious.org' => sub {
my ($ua, $tx) = @_;
Mojo::IOLoop->singleton->stop;
$kept_alive = $tx->kept_alive;
}
);
Mojo::IOLoop->singleton->start;
ok $kept_alive, 'connection was kept alive';
# Nested keep-alive
my @kept_alive;
$ua->get(
'http://mojolicious.org' => sub {
my ($ua, $tx) = @_;
push @kept_alive, $tx->kept_alive;
$ua->get(
'http://mojolicious.org' => sub {
my ($ua, $tx) = @_;
push @kept_alive, $tx->kept_alive;
$ua->get(
'http://mojolicious.org' => sub {
my ($ua, $tx) = @_;
push @kept_alive, $tx->kept_alive;
Mojo::IOLoop->singleton->stop;
}
);
}
);
}
);
Mojo::IOLoop->singleton->start;
is_deeply \@kept_alive, [1, 1, 1], 'connections kept alive';
# Fresh user agent again
$ua = Mojo::UserAgent->new;
# Custom non-keep-alive request
$tx = Mojo::Transaction::HTTP->new;
$tx->req->method('GET');
$tx->req->url->parse('http://metacpan.org');
$tx->req->headers->connection('close');
$ua->start($tx);
ok $tx->is_finished, 'transaction is finished';
is $tx->res->code, 301, 'right status';
like $tx->res->headers->connection, qr/close/i, 'right "Connection" header';
# One-liner
is g('mojolicious.org')->code, 200, 'right status';
is h('mojolicious.org')->code, 200, 'right status';
is h('mojolicious.org')->body, '', 'no content';
is p('mojolicious.org/lalalala')->code, 404, 'right status';
is g('http://mojolicious.org')->code, 200, 'right status';
is p('http://mojolicious.org')->code, 404, 'right status';
my $res = p('https://metacpan.org/search' => form => {q => 'mojolicious'});
like $res->body, qr/Mojolicious/, 'right content';
is $res->code, 200, 'right status';
# Simple requests
$tx = $ua->get('metacpan.org');
is $tx->req->method, 'GET', 'right method';
is $tx->req->url, 'http://metacpan.org', 'right url';
is $tx->res->code, 301, 'right status';
$tx = $ua->get('http://google.com');
is $tx->req->method, 'GET', 'right method';
is $tx->req->url, 'http://google.com', 'right url';
is $tx->res->code, 302, 'right status';
# Simple keep-alive requests
$tx = $ua->get('https://www.wikipedia.org');
is $tx->req->method, 'GET', 'right method';
is $tx->req->url, 'https://www.wikipedia.org', 'right url';
is $tx->req->body, '', 'no content';
is $tx->res->code, 200, 'right status';
ok $tx->keep_alive, 'connection will be kept alive';
ok !$tx->kept_alive, 'connection was not kept alive';
$tx = $ua->get('https://www.wikipedia.org');
is $tx->req->method, 'GET', 'right method';
is $tx->req->url, 'https://www.wikipedia.org', 'right url';
is $tx->res->code, 200, 'right status';
ok $tx->keep_alive, 'connection will be kept alive';
ok $tx->kept_alive, 'connection was kept alive';
$tx = $ua->get('https://www.wikipedia.org');
is $tx->req->method, 'GET', 'right method';
is $tx->req->url, 'https://www.wikipedia.org', 'right url';
is $tx->res->code, 200, 'right status';
ok $tx->keep_alive, 'connection will be kept alive';
ok $tx->kept_alive, 'connection was kept alive';
# Simple HTTPS request
$tx = $ua->get('https://metacpan.org');
is $tx->req->method, 'GET', 'right method';
is $tx->req->url, 'https://metacpan.org', 'right url';
is $tx->res->code, 200, 'right status';
# Fresh user agent again
$ua = Mojo::UserAgent->new;
# Simple keep-alive form POST
$tx = $ua->post('https://metacpan.org/search' => form => {q => 'mojolicious'});
is $tx->req->method, 'POST', 'right method';
is $tx->req->url, 'https://metacpan.org/search', 'right url';
is $tx->req->headers->content_length, 13, 'right content length';
is $tx->req->body, 'q=mojolicious', 'right content';
like $tx->res->body, qr/Mojolicious/, 'right content';
is $tx->res->code, 200, 'right status';
ok $tx->keep_alive, 'connection will be kept alive';
$tx = $ua->post('https://metacpan.org/search' => form => {q => 'mojolicious'});
is $tx->req->method, 'POST', 'right method';
is $tx->req->url, 'https://metacpan.org/search', 'right url';
is $tx->req->headers->content_length, 13, 'right content length';
is $tx->req->body, 'q=mojolicious', 'right content';
like $tx->res->body, qr/Mojolicious/, 'right content';
is $tx->res->code, 200, 'right status';
ok $tx->kept_alive, 'connection was kept alive';
ok $tx->local_address, 'has local address';
ok $tx->local_port > 0, 'has local port';
ok $tx->original_remote_address, 'has original remote address';
ok $tx->remote_address, 'has remote address';
ok $tx->remote_port > 0, 'has remote port';
# Simple request with redirect
$ua->max_redirects(3);
$tx = $ua->get('http://wikipedia.org/wiki/Perl');
$ua->max_redirects(0);
is $tx->req->method, 'GET', 'right method';
is $tx->req->url, 'https://en.wikipedia.org/wiki/Perl', 'right url';
is $tx->res->code, 200, 'right status';
is $tx->previous->req->method, 'GET', 'right method';
is $tx->previous->req->url, 'https://www.wikipedia.org/wiki/Perl', 'right url';
is $tx->previous->res->code, 301, 'right status';
is $tx->redirects->[-1]->req->method, 'GET', 'right method';
is $tx->redirects->[-1]->req->url, 'https://www.wikipedia.org/wiki/Perl',
'right url';
is $tx->redirects->[-1]->res->code, 301, 'right status';
# Connect timeout (non-routable address)
$tx = $ua->connect_timeout(0.5)->get('192.0.2.1');
ok $tx->is_finished, 'transaction is finished';
is $tx->error->{message}, 'Connect timeout', 'right error';
$ua->connect_timeout(3);
# Request timeout (non-routable address)
$tx = $ua->request_timeout(0.5)->get('192.0.2.1');
ok $tx->is_finished, 'transaction is finished';
is $tx->error->{message}, 'Request timeout', 'right error';
done_testing();
|