Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
made online tests a little more portable
  • Loading branch information
kraih committed May 26, 2012
1 parent 20d3dd1 commit b4b83e3
Showing 1 changed file with 10 additions and 99 deletions.
109 changes: 10 additions & 99 deletions t/mojo/user_agent_online.t
Expand Up @@ -14,7 +14,7 @@ plan skip_all => 'IO::Socket::IP 0.06 required for this test!'
unless Mojo::IOLoop::Server::IPV6;
plan skip_all => 'IO::Socket::SSL 1.37 required for this test!'
unless Mojo::IOLoop::Server::TLS;
plan tests => 117;
plan tests => 86;

# "So then I said to the cop, "No, you're driving under the influence...
# of being a jerk"."
Expand Down Expand Up @@ -71,15 +71,17 @@ is $ua->get('/remote_address')->res->body, $address, 'right address';
$ua = Mojo::UserAgent->new;

# Connection refused
my $tx = $ua->build_tx(GET => 'http://localhost:99999');
my $port = Mojo::IOLoop->generate_port;
my $tx = $ua->build_tx(GET => "http://localhost:$port");
$ua->start($tx);
ok !$tx->is_finished, 'transaction is not finished';
is $tx->error, "Couldn't connect.", 'right error';
ok $tx->error, 'has error';

# Connection refused
$tx = $ua->build_tx(GET => 'http://127.0.0.1:99999');
$tx = $ua->build_tx(GET => "http://127.0.0.1:$port");
$ua->start($tx);
ok !$tx->is_finished, 'transaction is not finished';
ok $tx->error, 'has error';

# Host does not exist
$tx = $ua->build_tx(GET => 'http://cdeabcdeffoobarnonexisting.com');
Expand Down Expand Up @@ -148,6 +150,7 @@ like $tx->res->headers->connection, qr/close/i, 'right "Connection" header';
# Oneliner
is g('mojolicio.us')->code, 200, 'right status';
is h('mojolicio.us')->code, 200, 'right status';
is h('mojolicio.us')->body, '', 'no body';
is p('mojolicio.us/lalalala')->code, 404, 'right status';
is g('http://mojolicio.us')->code, 200, 'right status';
is p('http://mojolicio.us')->code, 404, 'right status';
Expand Down Expand Up @@ -211,7 +214,7 @@ is $tx->req->headers->content_length, 9, 'right content length';
is $tx->req->body, 'Hi there!', 'right content';
is $tx->res->code, 200, 'right status';

# Simple form POST
# Simple keep alive form POST
$tx = $ua->post_form(
'http://search.cpan.org/search' => {query => 'mojolicious'});
is $tx->req->method, 'POST', 'right method';
Expand All @@ -221,8 +224,6 @@ is $tx->req->body, 'query=mojolicious', 'right content';
like $tx->res->body, qr/Mojolicious/, 'right content';
is $tx->res->code, 200, 'right status';
ok $tx->keep_alive, 'connection will be kept alive';

# Simple keep alive form POST
$tx = $ua->post_form(
'http://search.cpan.org/search' => {query => 'mojolicious'});
is $tx->req->method, 'POST', 'right method';
Expand All @@ -233,18 +234,7 @@ like $tx->res->body, qr/Mojolicious/, 'right content';
is $tx->res->code, 200, 'right status';
ok $tx->kept_alive, 'connection was kept alive';

# Simple requests with redirect
$ua->max_redirects(3);
$tx = $ua->get('http://wikipedia.org/wiki/Perl');
$ua->max_redirects(0);
is $tx->req->method, 'GET', 'right method';
is $tx->req->url, 'http://en.wikipedia.org/wiki/Perl', 'right url';
is $tx->res->code, 200, 'right status';
is $tx->previous->req->method, 'GET', 'right method';
is $tx->previous->req->url, 'http://www.wikipedia.org/wiki/Perl', 'right url';
is $tx->previous->res->code, 301, 'right status';

# Simple requests with redirect and no callback
# Simple request with redirect
$ua->max_redirects(3);
$tx = $ua->get('http://wikipedia.org/wiki/Perl');
$ua->max_redirects(0);
Expand All @@ -255,7 +245,7 @@ is $tx->previous->req->method, 'GET', 'right method';
is $tx->previous->req->url, 'http://www.wikipedia.org/wiki/Perl', 'right url';
is $tx->previous->res->code, 301, 'right status';

# Custom chunked request without callback
# Custom chunked request
$tx = Mojo::Transaction::HTTP->new;
$tx->req->method('GET');
$tx->req->url->parse('http://www.google.com');
Expand All @@ -268,88 +258,9 @@ $tx->req->write_chunk(
$ua->start($tx);
is_deeply [$tx->error], ['Bad Request', 400], 'right error';
is_deeply [$tx->res->error], ['Bad Request', 400], 'right error';

# Custom requests with keep alive
$tx = Mojo::Transaction::HTTP->new;
$tx->req->method('GET');
$tx->req->url->parse('http://www.wikipedia.org');
ok !$tx->kept_alive, 'connection was not kept alive';
$ua->start($tx);
ok $tx->is_finished, 'transaction is finished';
ok $tx->kept_alive, 'connection was kept alive';
$tx = Mojo::Transaction::HTTP->new;
$tx->req->method('GET');
$tx->req->url->parse('http://www.wikipedia.org');
ok !$tx->kept_alive, 'connection was not kept alive';
$ua->start($tx);
ok $tx->is_finished, 'transaction is finished';
ok $tx->kept_alive, 'connection was kept alive';
ok $tx->local_address, 'has local address';
ok $tx->local_port > 0, 'has local port';

# Multiple requests
$tx = Mojo::Transaction::HTTP->new;
$tx->req->method('GET');
$tx->req->url->parse('http://www.wikipedia.org');
my $tx2 = Mojo::Transaction::HTTP->new;
$tx2->req->method('GET');
$tx2->req->url->parse('http://www.wikipedia.org');
my $tx3 = Mojo::Transaction::HTTP->new;
$tx3->req->method('GET');
$tx3->req->url->parse('http://www.wikipedia.org');
$ua->start($tx);
$ua->start($tx2);
$ua->start($tx3);
ok $tx->is_finished, 'transaction is finished';
ok $tx2->is_finished, 'transaction is finished';
ok $tx3->is_finished, 'transaction is finished';
is $tx->res->code, 200, 'right status';
is $tx2->res->code, 200, 'right status';
is $tx3->res->code, 200, 'right status';
like $tx2->res->content->asset->slurp, qr/Wikipedia/i, 'right content';

# Mixed HEAD and GET requests
$tx = Mojo::Transaction::HTTP->new;
$tx->req->method('HEAD');
$tx->req->url->parse('http://www.wikipedia.org');
$tx2 = Mojo::Transaction::HTTP->new;
$tx2->req->method('GET');
$tx2->req->url->parse('http://www.wikipedia.org');
$ua->start($tx);
$ua->start($tx2);
ok $tx->is_finished, 'transaction is finished';
ok $tx2->is_finished, 'transaction is finished';
is $tx->res->code, 200, 'right status';
is $tx2->res->code, 200, 'right status';
like $tx2->res->content->asset->slurp, qr/Wikipedia/i, 'right content';

# Multiple requests
$tx = Mojo::Transaction::HTTP->new;
$tx->req->method('GET');
$tx->req->url->parse('http://www.perl.org');
$tx2 = Mojo::Transaction::HTTP->new;
$tx2->req->method('GET');
$tx2->req->url->parse('http://www.perl.org');
$tx3 = Mojo::Transaction::HTTP->new;
$tx3->req->method('GET');
$tx3->req->url->parse('http://www.perl.org');
my $tx4 = Mojo::Transaction::HTTP->new;
$tx4->req->method('GET');
$tx4->req->url->parse('http://www.perl.org');
$ua->start($tx);
$ua->start($tx2);
$ua->start($tx3);
$ua->start($tx4);
ok $tx->is_finished, 'transaction is finished';
ok $tx2->is_finished, 'transaction is finished';
ok $tx3->is_finished, 'transaction is finished';
ok $tx4->is_finished, 'transaction is finished';
is $tx->res->code, 200, 'right status';
is $tx2->res->code, 200, 'right status';
is $tx3->res->code, 200, 'right status';
is $tx4->res->code, 200, 'right status';
like $tx2->res->content->asset->slurp, qr/Perl/i, 'right content';

# Connect timeout (non-routable address)
$tx = $ua->connect_timeout(0.5)->get('192.0.2.1');
ok !$tx->is_finished, 'transaction is not finished';
Expand Down

0 comments on commit b4b83e3

Please sign in to comment.