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
|
use Mojo::Base -strict;
BEGIN { $ENV{MOJO_REACTOR} = 'Mojo::Reactor::Poll' }
use Test::More;
use Mojo::File 'tempdir';
use IO::Socket::UNIX;
use FindBin;
use lib "$FindBin::Bin/lib";
plan skip_all => 'set TEST_UNIX to enable this test (developer only!)'
unless $ENV{TEST_UNIX} || $ENV{TEST_ALL};
my $dir = tempdir;
my $dummy = $dir->child('dummy.sock')->to_string;
plan skip_all => 'UNIX domain socket support required for this test!'
unless IO::Socket::UNIX->new(Listen => 1, Local => $dummy);
use Mojo::Server::Daemon;
use Mojo::TestConnectProxy;
use Mojo::UserAgent;
use Mojo::Util 'url_escape';
use Mojolicious::Lite;
# Silence
app->log->level('fatal');
get '/' => sub {
my $c = shift;
$c->render(text => $c->req->url);
};
get '/info' => sub {
my $c = shift;
my $local_address = $c->tx->local_address // 'None';
my $local_port = $c->tx->local_port // 'None';
my $remote_address = $c->tx->remote_address // 'None';
my $remote_port = $c->tx->remote_port // 'None';
$c->render(text => "$local_address:$local_port:$remote_address:$remote_port");
};
websocket '/echo' => sub {
my $c = shift;
$c->on(message =>
sub { shift->send($c->req->url->to_abs->host . ': ' . shift)->finish });
};
# UNIX domain socket server
my $test = $dir->child('test.sock');
my $encoded = url_escape "$test";
ok !$ENV{MOJO_REUSE}, 'environment is clean';
my $daemon = Mojo::Server::Daemon->new(
app => app,
listen => ["http+unix://$encoded"],
silent => 1
)->start;
ok -S $test, 'UNIX domain socket exists';
my $fd = fileno $daemon->ioloop->acceptor($daemon->acceptors->[0])->handle;
like $ENV{MOJO_REUSE}, qr/^unix:\Q$test\E:\Q$fd\E/,
'file descriptor can be reused';
# Root
my $ua = Mojo::UserAgent->new(ioloop => $daemon->ioloop);
my $tx = $ua->get("http+unix://$encoded/");
ok !$tx->kept_alive, 'connection was not kept alive';
is $tx->res->code, 200, 'right status';
is $tx->res->body, '/', 'right content';
$tx = $ua->get("http+unix://$encoded/");
ok $tx->kept_alive, 'connection was kept alive';
is $tx->res->code, 200, 'right status';
is $tx->res->body, '/', 'right content';
# Connection information
$tx = $ua->get("http+unix://$encoded/info");
is $tx->res->code, 200, 'right status';
is $tx->res->body, 'None:None:None:None', 'right content';
# WebSocket
my $result;
$ua->websocket(
"ws+unix://$encoded/echo" => sub {
my ($ua, $tx) = @_;
$tx->on(finish => sub { Mojo::IOLoop->stop });
$tx->on(message => sub { shift->finish; $result = shift });
$tx->send('roundtrip works!');
}
);
Mojo::IOLoop->start;
is $result, "$test: roundtrip works!", 'right result';
# WebSocket again
$result = undef;
$ua->websocket(
"ws+unix://$encoded/echo" => sub {
my ($ua, $tx) = @_;
$tx->on(finish => sub { Mojo::IOLoop->stop });
$tx->on(message => sub { shift->finish; $result = shift });
$tx->send('roundtrip works!');
}
);
Mojo::IOLoop->start;
is $result, "$test: roundtrip works!", 'right result';
# WebSocket with proxy
my $proxy = $dir->child('proxy.sock');
my $encoded_proxy = url_escape $proxy;
my $id = Mojo::TestConnectProxy::proxy({path => "$proxy"}, {path => "$test"});
$result = undef;
$ua->proxy->http("http+unix://$encoded_proxy");
$ua->websocket(
'ws://example.com/echo' => sub {
my ($ua, $tx) = @_;
$tx->on(finish => sub { Mojo::IOLoop->stop });
$tx->on(message => sub { shift->finish; $result = shift });
$tx->send('roundtrip works!');
}
);
Mojo::IOLoop->start;
is $result, 'example.com: roundtrip works!', 'right result';
Mojo::IOLoop->remove($id);
# Proxy
$ua->proxy->http("http+unix://$encoded");
$tx = $ua->get('http://example.com');
ok !$tx->kept_alive, 'connection was not kept alive';
is $tx->res->code, 200, 'right status';
is $tx->res->body, 'http://example.com', 'right content';
$tx = $ua->get('http://example.com');
ok $tx->kept_alive, 'connection was kept alive';
is $tx->res->code, 200, 'right status';
is $tx->res->body, 'http://example.com', 'right content';
# Cleanup
undef $daemon;
ok !$ENV{MOJO_REUSE}, 'environment is clean';
done_testing();
|