Mojo-UserAgent-Cached

 view release on metacpan or  search on metacpan

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

use Mojo::Base -strict;


BEGIN {
  use Mojo::File;
  $ENV{MUAC_CACHE_ROOT_DIR} = Mojo::File::tempdir();
  $ENV{MOJO_PROXY}   = 0;
  $ENV{MOJO_REACTOR} = 'Mojo::Reactor::Poll';
}

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

plan skip_all => 'set TEST_ONLINE to enable this test (developer only!)' unless $ENV{TEST_ONLINE} || $ENV{TEST_ALL};
plan skip_all => 'IO::Socket::SSL 2.009+ required for this test!'        unless Mojo::IOLoop::TLS->can_tls;
plan skip_all => 'Mozilla::CA required for this test!'                   unless eval { require Mozilla::CA; 1 };

use IO::Socket::INET;
use Mojo::IOLoop;
use Mojo::Transaction::HTTP;
use Mojo::UserAgent::Cached;
use Mojolicious::Lite;
use ojo;


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

my $loop = Mojo::IOLoop->singleton;
my $ua   = Mojo::UserAgent->new;

subtest 'Make sure user agents dont taint the ioloop' => sub {
  my ($id, $code);
  $ua->get(
    'http://metacpan.org' => sub {
      my ($ua, $tx) = @_;
      $id   = $tx->connection;
      $code = $tx->res->code;
      $loop->stop;
    }
  );
  $loop->start;
  $ua = undef;
  $loop->timer(0.25 => sub { shift->stop });
  $loop->start;
  ok !$loop->stream($id), 'loop not tainted';
  is $code, 301, 'right status';
};

$ua = Mojo::UserAgent::Cached->new;

subtest 'Local address' => sub {
  $ua->server->app(app);
  my $sock    = IO::Socket::INET->new(PeerAddr => 'mojolicious.org', PeerPort => 80);
  my $address = $sock->sockhost;
  isnt $address, '127.0.0.1', 'different address';
  $ua->max_connections(0)->socket_options->{LocalAddr} = '127.0.0.1';
  my $tx = $ua->get('/remote_address');
  ok !$ua->ioloop->stream($tx->connection), 'connection is not active';
  is $tx->res->body, '127.0.0.1', 'right address';
  $ua->socket_options->{LocalAddr} = $address;
  is $ua->get('/remote_address')->res->body, $address, 'right address';
};

$ua = Mojo::UserAgent::Cached->new;
my $port = Mojo::IOLoop::Server->generate_port;

subtest 'Connection refused' => sub {
  my $tx = $ua->build_tx(GET => "http://127.0.0.1:$port");
  $ua->start($tx);
  ok $tx->is_finished, 'transaction is finished';
  ok $tx->error,       'has error';
};



( run in 2.333 seconds using v1.01-cache-2.11-cpan-75ffa21a3d4 )