From 60eb0f6cfd3f1fc0735e599af6848f87a0fb95d3 Mon Sep 17 00:00:00 2001 From: Andy Lester Date: Mon, 9 Aug 2021 13:30:45 -0500 Subject: [PATCH] Convert to subtests for issue #1520 --- t/mojo/user_agent_socks.t | 137 +++++++++++++++++++++----------------- 1 file changed, 75 insertions(+), 62 deletions(-) diff --git a/t/mojo/user_agent_socks.t b/t/mojo/user_agent_socks.t index d8a1a19569..bd7af1aea3 100644 --- a/t/mojo/user_agent_socks.t +++ b/t/mojo/user_agent_socks.t @@ -99,67 +99,80 @@ 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 { + 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'; +}; + +subtest 'Simple request with SOCKS proxy' => sub { + my $ua = Mojo::UserAgent->new(ioloop => Mojo::IOLoop->singleton, insecure => 1); + $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 $ua = Mojo::UserAgent->new(ioloop => Mojo::IOLoop->singleton, insecure => 1); + $ua->proxy->http("socks://foo:bar\@127.0.0.1:$port"); + 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 $ua = Mojo::UserAgent->new(ioloop => Mojo::IOLoop->singleton, insecure => 1); + $ua->proxy->http("socks://foo:bar\@127.0.0.1:$port"); + 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 { + my $ua = Mojo::UserAgent->new(ioloop => Mojo::IOLoop->singleton, insecure => 1); + $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 { + my $ua = Mojo::UserAgent->new(ioloop => Mojo::IOLoop->singleton, insecure => 1); + $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();