Skip to content

Commit

Permalink
Convert user_agent_socks.t to use subtests
Browse files Browse the repository at this point in the history
  • Loading branch information
spazm committed Nov 1, 2020
1 parent c7910df commit 6d10644
Showing 1 changed file with 67 additions and 61 deletions.
128 changes: 67 additions & 61 deletions t/mojo/user_agent_socks.t
Original file line number Diff line number Diff line change
Expand Up @@ -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();

0 comments on commit 6d10644

Please sign in to comment.