Mojo-UserAgent-Cached

 view release on metacpan or  search on metacpan

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

is $code,    200,      'right status';
is $body,    'works!', 'right content';

# Promises
my @results;
my $p1 = $ua->get_p('/');
my $p2 = $ua->get_p('/');
Mojo::Promise->all($p1, $p2)->then(sub {
  my ($first, $second) = @_;
  push @results, $first, $second;
})->wait;
ok !$results[0][0]->error, 'no error';
is $results[0][0]->res->code, 200,      'right status';
is $results[0][0]->res->body, 'works!', 'right content';
ok !$results[1][0]->error, 'no error';
is $results[1][0]->res->code, 200,      'right status';
is $results[1][0]->res->body, 'works!', 'right content';

# Promises (shortcut methods)
my $result;
$ua->delete_p('/method')->then(sub { $result = shift->res->body })->wait;
is $result, 'DELETE', 'right result';
$ua->get_p('/method')->then(sub { $result = shift->res->body })->wait;
is $result, 'GET', 'right result';
$ua->head_p('/method')->then(sub { $result = shift->res->body })->wait;
is $result, '', 'no result';
$ua->options_p('/method')->then(sub { $result = shift->res->body })->wait;
is $result, 'OPTIONS', 'right result';
$ua->patch_p('/method')->then(sub { $result = shift->res->body })->wait;
is $result, 'PATCH', 'right result';
$ua->post_p('/method')->then(sub { $result = shift->res->body })->wait;
is $result, 'POST', 'right result';
$ua->put_p('/method')->then(sub { $result = shift->res->body })->wait;
is $result, 'PUT', 'right result';
$ua->start_p($ua->build_tx(TEST => '/method'))->then(sub { $result = shift->res->body })->wait;
is $result, 'TEST', 'right result';

# No timeout
$ua = Mojo::UserAgent::Cached->new(inactivity_timeout => 0);
my $tx = $ua->get('/');
ok $tx->keep_alive, 'keep connection alive';
is $tx->res->code, 200,      'right status';
is $tx->res->body, 'works!', 'right content';
$tx = $ua->get('/');
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, 'works!', 'right content';

# SOCKS proxy request without SOCKS support
$ua = Mojo::UserAgent::Cached->new;
$tx = $ua->build_tx(GET => '/');
$tx->req->proxy($ua->server->url->scheme('socks'));
$tx = $ua->start($tx);
like $tx->error->{message}, qr/IO::Socket::Socks/, 'right error';
ok !Mojo::IOLoop::Client->can_socks, 'no SOCKS5 support';

# HTTPS request without TLS support
$ua = Mojo::UserAgent::Cached->new;
$tx = $ua->get($ua->server->url->scheme('https'));
like $tx->error->{message}, qr/IO::Socket::SSL/, 'right error';
ok !Mojo::IOLoop::TLS->can_tls, 'no TLS support';

# Promises (rejected)
my $error;
$ua->get_p($ua->server->url->scheme('https'))->catch(sub { $error = shift })->wait;
like $error, qr/IO::Socket::SSL/, 'right error';

# No non-blocking name resolution
ok !Mojo::IOLoop::Client->can_nnr, 'no non-blocking name resolution support';

# Blocking
$tx = $ua->get('/');
ok !$tx->error,      'no error';
ok !$tx->kept_alive, 'kept connection not alive';
is $tx->res->version, '1.1', 'right version';
is $tx->res->code,    200,   'right status';
ok !$tx->res->headers->connection, 'no "Connection" value';
is $tx->res->max_message_size, 2147483648, 'right value';
is $tx->res->body,             'works!',   'right content';

# Again
$tx = $ua->get('/');
ok !$tx->error, 'no error';
ok $tx->kept_alive, 'kept connection alive';
is $tx->res->version, '1.1', 'right version';
is $tx->res->code,    200,   'right status';
ok !$tx->res->headers->connection, 'no "Connection" value';
is $tx->res->body, 'works!', 'right content';
$tx = $ua->max_response_size(0)->get('/');
ok !$tx->error, 'no error';
is $tx->res->version, '1.1', 'right version';
is $tx->res->code,    200,   'right status';
ok !$tx->res->headers->connection, 'no "Connection" value';
is $tx->res->max_message_size, 0,        'right value';
is $tx->res->body,             'works!', 'right content';

# Unsupported protocol
$tx = $ua->request_timeout(3600)->get('htttp://example.com');
is $tx->error->{message}, 'Unsupported protocol: htttp', 'right error';
eval { $tx->result };
like $@, qr/Unsupported protocol: htttp/, 'right error';

# Shortcuts for common request methods
is $ua->delete('/method')->res->body,  'DELETE',  'right content';
is $ua->get('/method')->res->body,     'GET',     'right content';
is $ua->head('/method')->res->body,    '',        'no content';
is $ua->options('/method')->res->body, 'OPTIONS', 'right method';
is $ua->patch('/method')->res->body,   'PATCH',   'right method';
is $ua->post('/method')->res->body,    'POST',    'right method';
is $ua->put('/method')->res->body,     'PUT',     'right method';

# No keep-alive
$tx = $ua->get('/one?connection=test');
ok !$tx->error,      'no error';
ok !$tx->keep_alive, 'connection will not be kept alive';
is $tx->res->version, '1.0', 'right version';
is $tx->res->code,    200,   'right status';
is $tx->res->headers->connection, 'test', 'right "Connection" value';
is $tx->res->body, 'One!', 'right content';
$tx = $ua->get('/one?connection=test');
ok !$tx->error,      'no error';
ok !$tx->kept_alive, 'kept connection not alive';
ok !$tx->keep_alive, 'connection will not be kept alive';
is $tx->res->version, '1.0', 'right version';
is $tx->res->code,    200,   'right status';
is $tx->res->headers->connection, 'test', 'right "Connection" value';



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