Skip to content

Commit

Permalink
fixed bug in user_agent.t
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Nov 2, 2011
1 parent 7816434 commit 9fb0bb6
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 65 deletions.
1 change: 1 addition & 0 deletions Changes
Expand Up @@ -7,6 +7,7 @@ This file documents the revision history for Perl extension Mojolicious.
- Improved Mojo::ByteStream to generate most Mojo::Util based methods
automatically.
- Fixed route pattern bug.
- Fixed bug in "user_agent.t".

2.20 2011-11-01 00:00:00
- Changed semantics of almost all functions in Mojo::Util.
Expand Down
11 changes: 5 additions & 6 deletions lib/Mojo/DOM/CSS.pm
Expand Up @@ -161,7 +161,7 @@ sub _element {
my @selectors = reverse @$selectors;
my $first = 2;
my $parentonly = 0;
my ($current, $marker, $snapback, $siblings);
my ($current, $marker, $previous, $siblings);
for (my $i = 0; $i <= $#selectors; $i++) {
my $selector = $selectors[$i];

Expand All @@ -177,7 +177,7 @@ sub _element {
# Can't go back to the first
unless ($first) {
$marker //= $i;
$snapback //= $current;
$previous //= $current;
}
}

Expand Down Expand Up @@ -242,10 +242,9 @@ sub _element {
return unless defined $marker;

# Reset
$i = $marker - 2;
$current = $snapback;
$snapback = undef;
$marker = undef;
$i = $marker - 2;
$current = $previous;
($marker, $previous) = undef;
last;
}
}
Expand Down
74 changes: 15 additions & 59 deletions t/mojo/user_agent.t
Expand Up @@ -7,7 +7,7 @@ BEGIN {
$ENV{MOJO_IOWATCHER} = 'Mojo::IOWatcher';
}

use Test::More tests => 71;
use Test::More tests => 55;

# "The strong must protect the sweet."
use Mojo::IOLoop;
Expand All @@ -19,7 +19,7 @@ use_ok 'Mojo::UserAgent';
app->log->level('fatal');

# GET /
get '/' => {text => 'works'};
get '/' => {text => 'works!'};

# GET /timeout
my $timeout = undef;
Expand All @@ -37,14 +37,6 @@ get '/no_length' => sub {
$self->rendered(200);
};

# GET /last
my $last;
get '/last' => sub {
my $self = shift;
$last = $self->tx->connection;
$self->render(text => 'works!');
};

# Proxy detection
my $ua = Mojo::UserAgent->new;
my $backup = $ENV{HTTP_PROXY} || '';
Expand Down Expand Up @@ -107,16 +99,16 @@ $ua->get(
Mojo::IOLoop->start;
ok $success, 'successful';
is $code, 200, 'right status';
is $body, 'works', 'right content';
is $body, 'works!', 'right content';

# GET /last (custom connection)
# GET / (custom connection)
($success, $code, $body) = undef;
Mojo::IOLoop->connect(
address => 'localhost',
port => $ua->test_server->port,
on_connect => sub {
my ($loop, $id) = @_;
my $tx = $ua->build_tx(GET => 'http://mojolicio.us/last');
my $tx = $ua->build_tx(GET => 'http://mojolicio.us/');
$tx->connection($id);
$ua->start(
$tx => sub {
Expand All @@ -134,51 +126,15 @@ ok $success, 'successful';
is $code, 200, 'right status';
is $body, 'works!', 'right content';

# GET /last (blocking)
my $tx = $ua->get('/last');
# GET / (blocking)
my $tx = $ua->get('/');
ok $tx->success, 'successful';
ok !$tx->kept_alive, 'kept connection not alive';
is $tx->res->code, 200, 'right status';
is $tx->res->body, 'works!', 'right content';

# GET /last (again)
$tx = $ua->get('/last');
ok $tx->success, 'successful';
ok $tx->kept_alive, 'kept connection alive';
is $tx->res->code, 200, 'right status';
is $tx->res->body, 'works!', 'right content';

# Close connection
Mojo::IOLoop->stream($last)->emit('close');
Mojo::IOLoop->one_tick while Mojo::IOLoop->stream($last);

# GET /last (closed connection)
$tx = $ua->get('/last');
ok $tx->success, 'successful';
ok !$tx->kept_alive, 'kept connection not alive';
is $tx->res->code, 200, 'right status';
is $tx->res->body, 'works!', 'right content';

# GET /last (again)
$tx = $ua->get('/last');
ok $tx->success, 'successful';
ok $tx->kept_alive, 'kept connection alive';
is $tx->res->code, 200, 'right status';
is $tx->res->body, 'works!', 'right content';

# Close connection
Mojo::IOLoop->stream($last)->emit('close');
Mojo::IOLoop->one_tick while Mojo::IOLoop->stream($last);

# GET /last (closed connection)
$tx = $ua->get('/last');
ok $tx->success, 'successful';
ok !$tx->kept_alive, 'kept connection not alive';
is $tx->res->code, 200, 'right status';
is $tx->res->body, 'works!', 'right content';

# GET /last (again)
$tx = $ua->get('/last');
# GET / (again)
$tx = $ua->get('/');
ok $tx->success, 'successful';
ok $tx->kept_alive, 'kept connection alive';
is $tx->res->code, 200, 'right status';
Expand All @@ -187,8 +143,8 @@ is $tx->res->body, 'works!', 'right content';
# GET /
$tx = $ua->get('/');
ok $tx->success, 'successful';
is $tx->res->code, 200, 'right status';
is $tx->res->body, 'works', 'right content';
is $tx->res->code, 200, 'right status';
is $tx->res->body, 'works!', 'right content';

# GET / (callbacks)
my $finished;
Expand All @@ -203,8 +159,8 @@ $tx = $ua->start($tx);
$ua->unsubscribe('start');
ok $tx->success, 'successful';
is $finished, 1, 'finish event has been emitted';
is $tx->res->code, 200, 'right status';
is $tx->res->body, 'works', 'right content';
is $tx->res->code, 200, 'right status';
is $tx->res->body, 'works!', 'right content';

# GET /no_length (missing Content-Length header)
$tx = $ua->get('/no_length');
Expand All @@ -218,8 +174,8 @@ is $tx->res->body, 'works too!', 'right content';
# GET / (built-in server)
$tx = $ua->get('/');
ok $tx->success, 'successful';
is $tx->res->code, 200, 'right status';
is $tx->res->body, 'works', 'right content';
is $tx->res->code, 200, 'right status';
is $tx->res->body, 'works!', 'right content';

# GET / (built-in server times out)
$tx = $ua->get('/timeout');
Expand Down

0 comments on commit 9fb0bb6

Please sign in to comment.