diff --git a/t/mojo/proxy.t b/t/mojo/proxy.t index d3f54b158d..7f67a8f094 100644 --- a/t/mojo/proxy.t +++ b/t/mojo/proxy.t @@ -4,7 +4,7 @@ use Test::More; use Mojo::UserAgent::Proxy; # Proxy detection -{ +subtest 'Proxy detection with uppercase variable names' => sub { my $proxy = Mojo::UserAgent::Proxy->new; local $ENV{HTTP_PROXY} = 'http://127.0.0.1'; local $ENV{HTTPS_PROXY} = 'http://127.0.0.1:8080'; @@ -19,10 +19,18 @@ use Mojo::UserAgent::Proxy; ok !$proxy->is_needed('dummy.mojolicious.org'), 'no proxy needed'; ok $proxy->is_needed('icious.org'), 'proxy needed'; ok $proxy->is_needed('localhost'), 'proxy needed'; - ($ENV{HTTP_PROXY}, $ENV{HTTPS_PROXY}, $ENV{NO_PROXY}) = (); +}; + +subtest 'Proxy detection with lowercase variable names' => sub { + local $ENV{HTTP_PROXY}; + local $ENV{HTTPS_PROXY}; + local $ENV{NO_PROXY}; + local $ENV{http_proxy} = 'proxy.example.com'; local $ENV{https_proxy} = 'tunnel.example.com'; local $ENV{no_proxy} = 'localhost,localdomain,foo.com,example.com'; + + my $proxy = Mojo::UserAgent::Proxy->new; $proxy->detect; is_deeply $proxy->not, ['localhost', 'localdomain', 'foo.com', 'example.com'], 'right list'; is $proxy->http, 'proxy.example.com', 'right proxy'; @@ -35,6 +43,6 @@ use Mojo::UserAgent::Proxy; ok !$proxy->is_needed('example.com'), 'no proxy needed'; ok !$proxy->is_needed('www.example.com'), 'no proxy needed'; ok $proxy->is_needed('www.example.com.com'), 'proxy needed'; -} +}; done_testing(); diff --git a/t/mojo/subprocess.t b/t/mojo/subprocess.t index 71521b23fe..c724c4dbd7 100644 --- a/t/mojo/subprocess.t +++ b/t/mojo/subprocess.t @@ -12,248 +12,264 @@ use Mojo::IOLoop::Subprocess; use Mojo::Promise; use Mojo::File qw(tempfile); -# Huge result -my ($fail, $result, @start); -my $subprocess = Mojo::IOLoop::Subprocess->new; -$subprocess->on(spawn => sub { push @start, shift->pid }); -$subprocess->run( - sub { shift->pid . $$ . ('x' x 100000) }, - sub { - my ($subprocess, $err, $two) = @_; - $fail = $err; - $result .= $two; - } -); -$result = $$; -ok !$subprocess->pid, 'no process id available yet'; -is $subprocess->exit_code, undef, 'no exit code'; -Mojo::IOLoop->start; -ok $subprocess->pid, 'process id available'; -is $subprocess->exit_code, 0, 'zero exit code'; -ok !$fail, 'no error'; -is $result, $$ . 0 . $subprocess->pid . ('x' x 100000), 'right result'; -is_deeply \@start, [$subprocess->pid], 'spawn event has been emitted once'; +subtest 'Huge result' => sub { + my ($fail, $result, @start); + my $subprocess = Mojo::IOLoop::Subprocess->new; + $subprocess->on(spawn => sub { push @start, shift->pid }); + $subprocess->run( + sub { shift->pid . $$ . ('x' x 100000) }, + sub { + my ($subprocess, $err, $two) = @_; + $fail = $err; + $result .= $two; + } + ); -# Custom event loop -($fail, $result) = (); -my $loop = Mojo::IOLoop->new; -$loop->subprocess( - sub {'♥'}, - sub { - my ($subprocess, $err, @results) = @_; - $fail = $err; - $result = \@results; - } -); -$loop->start; -ok !$fail, 'no error'; -is_deeply $result, ['♥'], 'right structure'; + $result = $$; + ok !$subprocess->pid, 'no process id available yet'; + is $subprocess->exit_code, undef, 'no exit code'; + Mojo::IOLoop->start; + ok $subprocess->pid, 'process id available'; + is $subprocess->exit_code, 0, 'zero exit code'; + ok !$fail, 'no error'; + is $result, $$ . 0 . $subprocess->pid . ('x' x 100000), 'right result'; + is_deeply \@start, [$subprocess->pid], 'spawn event has been emitted once'; +}; -# Multiple return values -($fail, $result) = (); -$subprocess = Mojo::IOLoop::Subprocess->new; -$subprocess->run( - sub { return '♥', [{two => 2}], 3 }, - sub { - my ($subprocess, $err, @results) = @_; - $fail = $err; - $result = \@results; - } -); -Mojo::IOLoop->start; -ok !$fail, 'no error'; -is_deeply $result, ['♥', [{two => 2}], 3], 'right structure'; +subtest 'Custom event loop' => sub { + my ($fail, $result); + my $loop = Mojo::IOLoop->new; + $loop->subprocess( + sub {'♥'}, + sub { + my ($subprocess, $err, @results) = @_; + $fail = $err; + $result = \@results; + } + ); + $loop->start; + ok !$fail, 'no error'; + is_deeply $result, ['♥'], 'right structure'; +}; -# Promises -$result = []; -$subprocess = Mojo::IOLoop::Subprocess->new; -is $subprocess->exit_code, undef, 'no exit code'; -$subprocess->run_p(sub { return '♥', [{two => 2}], 3 })->then(sub { $result = [@_] })->wait; -is_deeply $result, ['♥', [{two => 2}], 3], 'right structure'; -$fail = undef; -$subprocess = Mojo::IOLoop::Subprocess->new; -$subprocess->run_p(sub { die 'Whatever' })->catch(sub { $fail = shift })->wait; -is $subprocess->exit_code, 0, 'zero exit code'; -like $fail, qr/Whatever/, 'right error'; -$result = []; -Mojo::IOLoop->subprocess->run_p(sub { return '♥' })->then(sub { $result = [@_] })->wait; -is_deeply $result, ['♥'], 'right structure'; +subtest 'Multiple return values' => sub { + my ($fail, $result); + my $subprocess = Mojo::IOLoop::Subprocess->new; + $subprocess->run( + sub { return '♥', [{two => 2}], 3 }, + sub { + my ($subprocess, $err, @results) = @_; + $fail = $err; + $result = \@results; + } + ); + Mojo::IOLoop->start; + ok !$fail, 'no error'; + is_deeply $result, ['♥', [{two => 2}], 3], 'right structure'; +}; -# Event loop in subprocess -($fail, $result) = (); -$subprocess = Mojo::IOLoop::Subprocess->new; -$subprocess->run( - sub { - my $result; - Mojo::IOLoop->next_tick(sub { $result = 23 }); - Mojo::IOLoop->start; - return $result; - }, - sub { - my ($subprocess, $err, $twenty_three) = @_; - $fail = $err; - $result = $twenty_three; - } -); -Mojo::IOLoop->start; -ok !$fail, 'no error'; -is $result, 23, 'right result'; +subtest 'Promises' => sub { + my $result = []; + my $subprocess = Mojo::IOLoop::Subprocess->new; + is $subprocess->exit_code, undef, 'no exit code'; + $subprocess->run_p(sub { return '♥', [{two => 2}], 3 })->then(sub { $result = [@_] })->wait; + is_deeply $result, ['♥', [{two => 2}], 3], 'right structure'; + my $fail; + $subprocess = Mojo::IOLoop::Subprocess->new; + $subprocess->run_p(sub { die 'Whatever' })->catch(sub { $fail = shift })->wait; + is $subprocess->exit_code, 0, 'zero exit code'; + like $fail, qr/Whatever/, 'right error'; + $result = []; + Mojo::IOLoop->subprocess->run_p(sub { return '♥' })->then(sub { $result = [@_] })->wait; + is_deeply $result, ['♥'], 'right structure'; +}; -# Event loop in subprocess (already running event loop) -($fail, $result) = (); -Mojo::IOLoop->next_tick(sub { - Mojo::IOLoop->subprocess( +subtest 'Event loop in subprocess' => sub { + my ($fail, $result); + my $subprocess = Mojo::IOLoop::Subprocess->new; + $subprocess->run( sub { my $result; - my $promise = Mojo::Promise->new; - $promise->then(sub { $result = shift }); - Mojo::IOLoop->next_tick(sub { $promise->resolve(25) }); - $promise->wait; + Mojo::IOLoop->next_tick(sub { $result = 23 }); + Mojo::IOLoop->start; return $result; }, sub { - my ($subprocess, $err, $twenty_five) = @_; + my ($subprocess, $err, $twenty_three) = @_; $fail = $err; - $result = $twenty_five; + $result = $twenty_three; } ); -}); -Mojo::IOLoop->start; -ok !$fail, 'no error'; -is $result, 25, 'right result'; + Mojo::IOLoop->start; + ok !$fail, 'no error'; + is $result, 23, 'right result'; +}; -# Concurrent subprocesses -($fail, $result) = (); -my $promise = Mojo::IOLoop->subprocess->run_p(sub {1}); -my $promise2 = Mojo::IOLoop->subprocess->run_p(sub {2}); -Mojo::Promise->all($promise, $promise2)->then(sub { - $result = [map { $_->[0] } @_]; -})->catch(sub { $fail = shift })->wait; -ok !$fail, 'no error'; -is_deeply $result, [1, 2], 'right structure'; +subtest 'Event loop in subprocess (already running event loop)' => sub { + my ($fail, $result); + Mojo::IOLoop->next_tick(sub { + Mojo::IOLoop->subprocess( + sub { + my $result; + my $promise = Mojo::Promise->new; + $promise->then(sub { $result = shift }); + Mojo::IOLoop->next_tick(sub { $promise->resolve(25) }); + $promise->wait; + return $result; + }, + sub { + my ($subprocess, $err, $twenty_five) = @_; + $fail = $err; + $result = $twenty_five; + } + ); + }); + Mojo::IOLoop->start; + ok !$fail, 'no error'; + is $result, 25, 'right result'; +}; -# No result -($fail, $result) = (); -Mojo::IOLoop::Subprocess->new->run( - sub {return}, - sub { - my ($subprocess, $err, @results) = @_; - $fail = $err; - $result = \@results; - } -); -Mojo::IOLoop->start; -ok !$fail, 'no error'; -is_deeply $result, [], 'right structure'; +subtest 'Concurrent subprocesses' => sub { + my ($fail, $result); + my $promise = Mojo::IOLoop->subprocess->run_p(sub {1}); + my $promise2 = Mojo::IOLoop->subprocess->run_p(sub {2}); + Mojo::Promise->all($promise, $promise2)->then(sub { + $result = [map { $_->[0] } @_]; + })->catch(sub { $fail = shift })->wait; + ok !$fail, 'no error'; + is_deeply $result, [1, 2], 'right structure'; +}; -# Stream inherited from previous subprocesses -($fail, $result) = (); -my @promises; -my $me = $$; -for (0 .. 1) { - push @promises, my $promise = Mojo::Promise->new; - my $subprocess = Mojo::IOLoop::Subprocess->new; - $subprocess->run( - sub { 1 + 1 }, +subtest 'No result' => sub { + my ($fail, $result); + Mojo::IOLoop::Subprocess->new->run( + sub {return}, sub { - my ($subprocess, $err, $two) = @_; - $fail ||= $err; - push @$result, $two; - is $me, $$, 'we are the parent'; - $promise->resolve; + my ($subprocess, $err, @results) = @_; + $fail = $err; + $result = \@results; } ); -} -Mojo::Promise->all(@promises)->wait; -ok !$fail, 'no error'; -is_deeply $result, [2, 2], 'right structure'; + Mojo::IOLoop->start; + ok !$fail, 'no error'; + is_deeply $result, [], 'right structure'; +}; -# Exception -$fail = undef; -Mojo::IOLoop::Subprocess->new->run( - sub { die 'Whatever' }, - sub { - my ($subprocess, $err) = @_; - $fail = $err; +subtest 'Stream inherited from previous subprocesses' => sub { + my ($fail, $result); + my @promises; + my $me = $$; + for (0 .. 1) { + push @promises, my $promise = Mojo::Promise->new; + my $subprocess = Mojo::IOLoop::Subprocess->new; + $subprocess->run( + sub { 1 + 1 }, + sub { + my ($subprocess, $err, $two) = @_; + $fail ||= $err; + push @$result, $two; + is $me, $$, 'we are the parent'; + $promise->resolve; + } + ); } -); -Mojo::IOLoop->start; -like $fail, qr/Whatever/, 'right error'; + Mojo::Promise->all(@promises)->wait; + ok !$fail, 'no error'; + is_deeply $result, [2, 2], 'right structure'; +}; -# Non-zero exit status -$fail = undef; -$subprocess = Mojo::IOLoop::Subprocess->new; -$subprocess->run( - sub { exit 3 }, - sub { - my ($subprocess, $err) = @_; - $fail = $err; - } -); -Mojo::IOLoop->start; -is $subprocess->exit_code, 3, 'right exit code'; -like $fail, qr/offset 0/, 'right error'; +subtest 'Exception' => sub { + my $fail; + Mojo::IOLoop::Subprocess->new->run( + sub { die 'Whatever' }, + sub { + my ($subprocess, $err) = @_; + $fail = $err; + } + ); + Mojo::IOLoop->start; + like $fail, qr/Whatever/, 'right error'; +}; -# Serialization error -$fail = undef; -$subprocess = Mojo::IOLoop::Subprocess->new; -$subprocess->deserialize(sub { die 'Whatever' }); -$subprocess->run( - sub { 1 + 1 }, - sub { - my ($subprocess, $err) = @_; - $fail = $err; - } -); -Mojo::IOLoop->start; -like $fail, qr/Whatever/, 'right error'; +subtest 'Non-zero exit status' => sub { + my $fail; + my $subprocess = Mojo::IOLoop::Subprocess->new; + $subprocess->run( + sub { exit 3 }, + sub { + my ($subprocess, $err) = @_; + $fail = $err; + } + ); + Mojo::IOLoop->start; + is $subprocess->exit_code, 3, 'right exit code'; + like $fail, qr/offset 0/, 'right error'; +}; -# Progress -($fail, $result) = (undef, undef); -my @progress; -$subprocess = Mojo::IOLoop::Subprocess->new; -$subprocess->run( - sub { - my $s = shift; - $s->progress(20); - $s->progress({percentage => 45}); - $s->progress({percentage => 90}, {long_data => [1 .. 1e5]}); - 'yay'; - }, - sub { - my ($subprocess, $err, @res) = @_; - $fail = $err; - $result = \@res; - } -); -$subprocess->on( - progress => sub { - my ($subprocess, @args) = @_; - push @progress, \@args; - } -); -Mojo::IOLoop->start; -ok !$fail, 'no error'; -is_deeply $result, ['yay'], 'correct result'; -is_deeply \@progress, [[20], [{percentage => 45}], [{percentage => 90}, {long_data => [1 .. 1e5]}]], 'correct progress'; +subtest 'Serialization error' => sub { + my $fail; + my $subprocess = Mojo::IOLoop::Subprocess->new; + $subprocess->deserialize(sub { die 'Whatever' }); + $subprocess->run( + sub { 1 + 1 }, + sub { + my ($subprocess, $err) = @_; + $fail = $err; + } + ); + Mojo::IOLoop->start; + like $fail, qr/Whatever/, 'right error'; +}; -# Cleanup -($fail, $result) = (); -my $file = tempfile; -my $called = 0; -$subprocess = Mojo::IOLoop::Subprocess->new; -$subprocess->on(cleanup => sub { $file->spurt(shift->serialize->({test => ++$called})) }); -$subprocess->run( - sub {'Hello Mojo!'}, - sub { - my ($subprocess, $err, $hello) = @_; - $fail = $err; - $result = $hello; - } -); -Mojo::IOLoop->start; -is_deeply $subprocess->deserialize->($file->slurp), {test => 1}, 'cleanup event emitted once'; -ok !$fail, 'no error'; -is $result, 'Hello Mojo!', 'right result'; +subtest 'Progress' => sub { + my ($fail, $result); + my @progress; + my $subprocess = Mojo::IOLoop::Subprocess->new; + $subprocess->run( + sub { + my $s = shift; + $s->progress(20); + $s->progress({percentage => 45}); + $s->progress({percentage => 90}, {long_data => [1 .. 1e5]}); + 'yay'; + }, + sub { + my ($subprocess, $err, @res) = @_; + $fail = $err; + $result = \@res; + } + ); + $subprocess->on( + progress => sub { + my ($subprocess, @args) = @_; + push @progress, \@args; + } + ); + Mojo::IOLoop->start; + ok !$fail, 'no error'; + is_deeply $result, ['yay'], 'correct result'; + is_deeply \@progress, [[20], [{percentage => 45}], [{percentage => 90}, {long_data => [1 .. 1e5]}]], + 'correct progress'; +}; + +subtest 'Cleanup' => sub { + my ($fail, $result); + my $file = tempfile; + my $called = 0; + my $subprocess = Mojo::IOLoop::Subprocess->new; + $subprocess->on(cleanup => sub { $file->spurt(shift->serialize->({test => ++$called})) }); + $subprocess->run( + sub {'Hello Mojo!'}, + sub { + my ($subprocess, $err, $hello) = @_; + $fail = $err; + $result = $hello; + } + ); + Mojo::IOLoop->start; + is_deeply $subprocess->deserialize->($file->slurp), {test => 1}, 'cleanup event emitted once'; + ok !$fail, 'no error'; + is $result, 'Hello Mojo!', 'right result'; +}; done_testing(); diff --git a/t/mojo/user_agent_tls.t b/t/mojo/user_agent_tls.t index e1d94a5a61..8f974b927a 100644 --- a/t/mojo/user_agent_tls.t +++ b/t/mojo/user_agent_tls.t @@ -18,78 +18,84 @@ app->log->level('fatal'); get '/' => {text => 'works!'}; -# Web server with valid certificates -my $daemon = Mojo::Server::Daemon->new(app => app, ioloop => Mojo::IOLoop->singleton, silent => 1); -my $listen - = 'https://127.0.0.1' - . '?cert=t/mojo/certs/server.crt' - . '&key=t/mojo/certs/server.key' - . '&ca=t/mojo/certs/ca.crt&verify=0x03'; -my $port = $daemon->listen([$listen])->start->ports->[0]; +subtest 'Web server with valid certificates' => sub { + my $daemon = Mojo::Server::Daemon->new(app => app, ioloop => Mojo::IOLoop->singleton, silent => 1); + my $listen + = 'https://127.0.0.1' + . '?cert=t/mojo/certs/server.crt' + . '&key=t/mojo/certs/server.key' + . '&ca=t/mojo/certs/ca.crt&verify=0x03'; + my $port = $daemon->listen([$listen])->start->ports->[0]; -# No certificate -my $ua = Mojo::UserAgent->new(ioloop => Mojo::IOLoop->singleton); -my $tx = $ua->get("https://127.0.0.1:$port"); -ok $tx->error, 'has error'; -$tx = $ua->get("https://127.0.0.1:$port"); -ok $tx->error, 'has error'; -$tx = $ua->ca('t/mojo/certs/ca.crt')->get("https://127.0.0.1:$port"); -ok $tx->error, 'has error'; -$tx = $ua->get("https://127.0.0.1:$port"); -ok $tx->error, 'has error'; + subtest 'No certificate' => sub { + my $ua = Mojo::UserAgent->new(ioloop => Mojo::IOLoop->singleton); + my $tx = $ua->get("https://127.0.0.1:$port"); + ok $tx->error, 'has error'; + $tx = $ua->get("https://127.0.0.1:$port"); + ok $tx->error, 'has error'; + $tx = $ua->ca('t/mojo/certs/ca.crt')->get("https://127.0.0.1:$port"); + ok $tx->error, 'has error'; + $tx = $ua->get("https://127.0.0.1:$port"); + ok $tx->error, 'has error'; + }; -# Valid certificates -$ua->ca('t/mojo/certs/ca.crt')->cert('t/mojo/certs/client.crt')->key('t/mojo/certs/client.key'); -$tx = $ua->get("https://127.0.0.1:$port"); -ok !$tx->error, 'no error'; -is $tx->res->code, 200, 'right status'; -is $tx->res->body, 'works!', 'right content'; + subtest 'Valid certificates' => sub { + my $ua = Mojo::UserAgent->new(ioloop => Mojo::IOLoop->singleton); + $ua->ca('t/mojo/certs/ca.crt')->cert('t/mojo/certs/client.crt')->key('t/mojo/certs/client.key'); + my $tx = $ua->get("https://127.0.0.1:$port"); + ok !$tx->error, 'no error'; + is $tx->res->code, 200, 'right status'; + is $tx->res->body, 'works!', 'right content'; + }; -# Valid certificates (env) -$ua = Mojo::UserAgent->new(ioloop => $ua->ioloop); -{ - local $ENV{MOJO_CA_FILE} = 't/mojo/certs/ca.crt'; - local $ENV{MOJO_CERT_FILE} = 't/mojo/certs/client.crt'; - local $ENV{MOJO_KEY_FILE} = 't/mojo/certs/client.key'; - local $ENV{MOJO_INSECURE} = 0; - $tx = $ua->get("https://127.0.0.1:$port"); - is $ua->ca, 't/mojo/certs/ca.crt', 'right path'; - is $ua->cert, 't/mojo/certs/client.crt', 'right path'; - is $ua->key, 't/mojo/certs/client.key', 'right path'; - is $ua->insecure, 0, 'secure'; - ok !$tx->error, 'no error'; - is $tx->res->code, 200, 'right status'; - is $tx->res->body, 'works!', 'right content'; -} + subtest 'Valid certificates (env)' => sub { + my $ua = Mojo::UserAgent->new(ioloop => Mojo::IOLoop->singleton); + local $ENV{MOJO_CA_FILE} = 't/mojo/certs/ca.crt'; + local $ENV{MOJO_CERT_FILE} = 't/mojo/certs/client.crt'; + local $ENV{MOJO_KEY_FILE} = 't/mojo/certs/client.key'; + local $ENV{MOJO_INSECURE} = 0; + my $tx = $ua->get("https://127.0.0.1:$port"); + is $ua->ca, 't/mojo/certs/ca.crt', 'right path'; + is $ua->cert, 't/mojo/certs/client.crt', 'right path'; + is $ua->key, 't/mojo/certs/client.key', 'right path'; + is $ua->insecure, 0, 'secure'; + ok !$tx->error, 'no error'; + is $tx->res->code, 200, 'right status'; + is $tx->res->body, 'works!', 'right content'; + }; + + subtest 'Invalid certificate' => sub { + my $ua = Mojo::UserAgent->new(ioloop => Mojo::IOLoop->singleton); + $ua->cert('t/mojo/certs/bad.crt')->key('t/mojo/certs/bad.key'); + my $tx = $ua->get("https://127.0.0.1:$port"); + ok $tx->error, 'has error'; + }; +}; -# Invalid certificate -$ua = Mojo::UserAgent->new(ioloop => $ua->ioloop); -$ua->cert('t/mojo/certs/bad.crt')->key('t/mojo/certs/bad.key'); -$tx = $ua->get("https://127.0.0.1:$port"); -ok $tx->error, 'has error'; -# Web server with valid certificates and no verification -$daemon = Mojo::Server::Daemon->new(app => app, ioloop => Mojo::IOLoop->singleton, silent => 1); -$listen - = 'https://127.0.0.1' - . '?cert=t/mojo/certs/server.crt' - . '&key=t/mojo/certs/server.key' - . '&ca=t/mojo/certs/ca.crt' - . '&ciphers=AES256-SHA:ALL' - . '&verify=0x00' - . '&version=TLSv1'; -$port = $daemon->listen([$listen])->start->ports->[0]; +subtest 'Web server with valid certificates and no verification' => sub { + my $daemon = Mojo::Server::Daemon->new(app => app, ioloop => Mojo::IOLoop->singleton, silent => 1); + my $listen + = 'https://127.0.0.1' + . '?cert=t/mojo/certs/server.crt' + . '&key=t/mojo/certs/server.key' + . '&ca=t/mojo/certs/ca.crt' + . '&ciphers=AES256-SHA:ALL' + . '&verify=0x00' + . '&version=TLSv1'; + my $port = $daemon->listen([$listen])->start->ports->[0]; -# Invalid certificate -$ua = Mojo::UserAgent->new(ioloop => $ua->ioloop); -$ua->cert('t/mojo/certs/bad.crt')->key('t/mojo/certs/bad.key'); -$tx = $ua->get("https://127.0.0.1:$port"); -ok $tx->error, 'has error'; -$ua = Mojo::UserAgent->new(ioloop => $ua->ioloop, insecure => 1); -$ua->cert('t/mojo/certs/bad.crt')->key('t/mojo/certs/bad.key'); -$tx = $ua->get("https://127.0.0.1:$port"); -ok !$tx->error, 'no error'; -is $ua->ioloop->stream($tx->connection)->handle->get_cipher, 'AES256-SHA', 'AES256-SHA has been negotiatied'; -is $ua->ioloop->stream($tx->connection)->handle->get_sslversion, 'TLSv1', 'TLSv1 has been negotiatied'; + # Invalid certificate + my $ua = Mojo::UserAgent->new(ioloop => Mojo::IOLoop->singleton); + $ua->cert('t/mojo/certs/bad.crt')->key('t/mojo/certs/bad.key'); + my $tx = $ua->get("https://127.0.0.1:$port"); + ok $tx->error, 'has error'; + $ua = Mojo::UserAgent->new(ioloop => $ua->ioloop, insecure => 1); + $ua->cert('t/mojo/certs/bad.crt')->key('t/mojo/certs/bad.key'); + $tx = $ua->get("https://127.0.0.1:$port"); + ok !$tx->error, 'no error'; + is $ua->ioloop->stream($tx->connection)->handle->get_cipher, 'AES256-SHA', 'AES256-SHA has been negotiatied'; + is $ua->ioloop->stream($tx->connection)->handle->get_sslversion, 'TLSv1', 'TLSv1 has been negotiatied'; +}; done_testing(); diff --git a/t/mojo/user_agent_unix.t b/t/mojo/user_agent_unix.t index 2f8bb22957..18799e4f0e 100644 --- a/t/mojo/user_agent_unix.t +++ b/t/mojo/user_agent_unix.t @@ -51,76 +51,83 @@ 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'; + +subtest 'Root' => sub { + 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'; +}; + +subtest 'Connection information' => sub { + my $tx = $ua->get("http+unix://$encoded/info"); + is $tx->res->code, 200, 'right status'; + is $tx->res->body, 'None:None:None:None', 'right content'; +}; + +subtest 'WebSocket' => sub { + 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'; +}; + +subtest 'WebSocket again' => sub { + 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'; +}; + +subtest 'WebSocket with proxy' => sub { + my $proxy = $dir->child('proxy.sock'); + my $encoded_proxy = url_escape $proxy; + my $id = Mojo::TestConnectProxy::proxy({path => "$proxy"}, {path => "$test"}); + my $result; + $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); +}; + +subtest 'Proxy' => sub { + $ua->proxy->http("http+unix://$encoded"); + my $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;