From 6d106445a340f971f3ded420ce6dfd424881c0c9 Mon Sep 17 00:00:00 2001 From: Andrew Grangaard Date: Sat, 31 Oct 2020 21:07:50 -0700 Subject: [PATCH] Convert user_agent_socks.t to use subtests --- t/mojo/user_agent_socks.t | 128 ++++++++++++++++++++------------------ 1 file changed, 67 insertions(+), 61 deletions(-) diff --git a/t/mojo/user_agent_socks.t b/t/mojo/user_agent_socks.t index d8a1a19569..e7d4bcd661 100644 --- a/t/mojo/user_agent_socks.t +++ b/t/mojo/user_agent_socks.t @@ -99,67 +99,73 @@ Mojo::IOLoop->singleton->reactor->io( } ); -# Failed authentication with SOCKS proxy my $ua = Mojo::UserAgent->new(ioloop => Mojo::IOLoop->singleton, insecure => 1); -$ua->proxy->http("socks://foo:baz\@127.0.0.1:$port"); -my $tx = $ua->get('/'); -ok $tx->error, 'has error'; - -# Simple request with SOCKS proxy -$ua->proxy->http("socks://foo:bar\@127.0.0.1:$port"); -$tx = $ua->get('/'); -ok !$tx->error, 'no error'; -ok !$tx->kept_alive, 'kept connection not alive'; -ok $tx->keep_alive, 'keep connection alive'; -is $tx->res->code, 200, 'right status'; -is $tx->req->headers->proxy_authorization, undef, 'no "Proxy-Authorization" value'; -is $tx->res->body, $last, 'right content'; -isnt(Mojo::IOLoop->stream($tx->connection)->handle->sockport, $last, 'different ports'); - -# Keep alive request with SOCKS proxy -my $before = $last; -$tx = $ua->get('/'); -ok !$tx->error, 'no error'; -ok $tx->kept_alive, 'kept connection alive'; -ok $tx->keep_alive, 'keep connection alive'; -is $tx->res->code, 200, 'right status'; -is $tx->res->body, $last, 'right content'; -is $before, $last, 'same port'; -isnt(Mojo::IOLoop->stream($tx->connection)->handle->sockport, $last, 'different ports'); - -# WebSocket with SOCKS proxy -my ($result, $id); -$ua->websocket( - '/echo' => sub { - my ($ua, $tx) = @_; - $id = $tx->connection; - $tx->on(message => sub { $result = pop; Mojo::IOLoop->stop }); - $tx->send('test'); - } -); -Mojo::IOLoop->start; -is $result, $last, 'right result'; -isnt(Mojo::IOLoop->stream($id)->handle->sockport, $last, 'different ports'); - -# HTTPS request with SOCKS proxy -$ua->proxy->https("socks://foo:bar\@127.0.0.1:$port"); -$ua->server->url('https'); -$tx = $ua->get('/secure'); -ok !$tx->error, 'no error'; -ok !$tx->kept_alive, 'kept connection not alive'; -ok $tx->keep_alive, 'keep connection alive'; -is $tx->res->code, 200, 'right status'; -is $tx->res->body, "https:$last", 'right content'; -isnt(Mojo::IOLoop->stream($tx->connection)->handle->sockport, $last, 'different ports'); - -# Disabled SOCKS proxy -$ua->server->url('http'); -$ua->proxy->http("socks://foo:baz\@127.0.0.1:$port"); -$tx = $ua->build_tx(GET => '/'); -$tx->req->via_proxy(0); -$tx = $ua->start($tx); -ok !$tx->error, 'no error'; -is $tx->res->code, 200, 'right status'; -is $tx->res->body, $tx->local_port, 'right content'; +subtest "Failed authentication with SOCKS proxy" => sub { + $ua->proxy->http("socks://foo:baz\@127.0.0.1:$port"); + my $tx = $ua->get('/'); + ok $tx->error, 'has error'; +}; + +subtest "Simple request with SOCKS proxy" => sub { + $ua->proxy->http("socks://foo:bar\@127.0.0.1:$port"); + my $tx = $ua->get('/'); + ok !$tx->error, 'no error'; + ok !$tx->kept_alive, 'kept connection not alive'; + ok $tx->keep_alive, 'keep connection alive'; + is $tx->res->code, 200, 'right status'; + is $tx->req->headers->proxy_authorization, undef, 'no "Proxy-Authorization" value'; + is $tx->res->body, $last, 'right content'; + isnt(Mojo::IOLoop->stream($tx->connection)->handle->sockport, $last, 'different ports'); +}; + +subtest "Keep alive request with SOCKS proxy" => sub { + my $before = $last; + my $tx = $ua->get('/'); + ok !$tx->error, 'no error'; + ok $tx->kept_alive, 'kept connection alive'; + ok $tx->keep_alive, 'keep connection alive'; + is $tx->res->code, 200, 'right status'; + is $tx->res->body, $last, 'right content'; + is $before, $last, 'same port'; + isnt(Mojo::IOLoop->stream($tx->connection)->handle->sockport, $last, 'different ports'); +}; + +subtest "WebSocket with SOCKS proxy" => sub { + my ($result, $id); + $ua->websocket( + '/echo' => sub { + my ($ua, $tx) = @_; + $id = $tx->connection; + $tx->on(message => sub { $result = pop; Mojo::IOLoop->stop }); + $tx->send('test'); + } + ); + Mojo::IOLoop->start; + is $result, $last, 'right result'; + isnt(Mojo::IOLoop->stream($id)->handle->sockport, $last, 'different ports'); +}; + +subtest "HTTPS request with SOCKS proxy" => sub { + $ua->proxy->https("socks://foo:bar\@127.0.0.1:$port"); + $ua->server->url('https'); + my $tx = $ua->get('/secure'); + ok !$tx->error, 'no error'; + ok !$tx->kept_alive, 'kept connection not alive'; + ok $tx->keep_alive, 'keep connection alive'; + is $tx->res->code, 200, 'right status'; + is $tx->res->body, "https:$last", 'right content'; + isnt(Mojo::IOLoop->stream($tx->connection)->handle->sockport, $last, 'different ports'); +}; + +subtest "Disabled SOCKS proxy" => sub { + $ua->server->url('http'); + $ua->proxy->http("socks://foo:baz\@127.0.0.1:$port"); + my $tx = $ua->build_tx(GET => '/'); + $tx->req->via_proxy(0); + $tx = $ua->start($tx); + ok !$tx->error, 'no error'; + is $tx->res->code, 200, 'right status'; + is $tx->res->body, $tx->local_port, 'right content'; +}; done_testing();