Mojo-UserAgent-Cached

 view release on metacpan or  search on metacpan

t/mojo/user_agent_socks.t  view on Meta::CPAN

use Mojo::Base -strict;

BEGIN { $ENV{MOJO_REACTOR} = 'Mojo::Reactor::Poll' }

use Test::More;
use Mojo::IOLoop::Client;
use Mojo::IOLoop::TLS;

plan skip_all => 'set TEST_SOCKS to enable this test (developer only!)' unless $ENV{TEST_SOCKS} || $ENV{TEST_ALL};
plan skip_all => 'IO::Socket::Socks 0.64+ required for this test!'      unless Mojo::IOLoop::Client->can_socks;
plan skip_all => 'IO::Socket::SSL 2.009+ required for this test!'       unless Mojo::IOLoop::TLS->can_tls;

use Mojo::IOLoop;
use Mojo::IOLoop::Server;
use Mojo::IOLoop::Stream;
use Mojo::UserAgent::Cached;
use Mojolicious::Lite;
use Scalar::Util qw(weaken);

# Silence
app->log->level('fatal');

get '/' => sub {
  my $c = shift;
  $c->render(text => $c->tx->remote_port);
};

websocket '/echo' => sub {
  my $c = shift;
  $c->on(
    message => sub {
      my $c = shift;
      $c->send($c->tx->remote_port);
    }
  );
};

get '/secure' => sub {
  my $c = shift;
  $c->render(text => $c->req->url->to_abs->protocol . ':' . $c->tx->remote_port);
};

my $port   = Mojo::IOLoop::Server->generate_port;
my $server = IO::Socket::Socks->new(
  Blocking    => 0,
  Listen      => 10,
  ProxyAddr   => '127.0.0.1',
  ProxyPort   => $port,
  RequireAuth => 1,
  UserAuth    => sub { $_[0] eq 'foo' && $_[1] eq 'bar' }
);

# SOCKS proxy server for testing
my $last;
Mojo::IOLoop->singleton->reactor->io(
  $server => sub {
    my $reactor = shift;

    my $client = $server->accept;
    $client->blocking(0);
    my ($address, $port);
    $reactor->io(
      $client => sub {
        my $reactor = shift;

        my $err = $IO::Socket::Socks::SOCKS_ERROR;
        if ($client->ready) {

          if ($address) {
            $reactor->remove($client);
            Mojo::IOLoop->client(



( run in 1.280 second using v1.01-cache-2.11-cpan-39bf76dae61 )