Skip to content

Commit

Permalink
Convert to subtests for issue mojolicious#1520
Browse files Browse the repository at this point in the history
  • Loading branch information
petdance authored and karenetheridge committed Sep 20, 2023
1 parent 526b2b5 commit 60eb0f6
Showing 1 changed file with 75 additions and 62 deletions.
137 changes: 75 additions & 62 deletions t/mojo/user_agent_socks.t
Expand Up @@ -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();

0 comments on commit 60eb0f6

Please sign in to comment.