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 Oct 30, 2011
1 parent 942c252 commit 39de890
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 116 deletions.
3 changes: 3 additions & 0 deletions Changes
@@ -1,5 +1,8 @@
This file documents the revision history for Perl extension Mojolicious.

2.17 2011-10-30 00:00:00
- Fixed bug in "user_agent.t".

2.16 2011-10-30 00:00:00
- Removed experimental status from Mojo::EventEmitter.
- Merged unsubscribe and unsubscribe_all methods in
Expand Down
2 changes: 2 additions & 0 deletions lib/Mojo/IOLoop.pm
Expand Up @@ -183,6 +183,7 @@ sub on_read { shift->_event(read => @_) }

sub one_tick {
my $self = shift;
$self = $self->singleton unless ref $self;
$self->timer(shift // '0.025' => sub { shift->stop });
$self->start;
}
Expand Down Expand Up @@ -822,6 +823,7 @@ Callback to be invoked if new data arrives on the connection.
=head2 C<one_tick>
Mojo::IOLoop->one_tick;
$loop->one_tick;
$loop->one_tick('0.25');
$loop->one_tick(0);
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious.pm
Expand Up @@ -35,7 +35,7 @@ has static => sub { Mojolicious::Static->new };
has types => sub { Mojolicious::Types->new };

our $CODENAME = 'Leaf Fluttering In Wind';
our $VERSION = '2.16';
our $VERSION = '2.17';

# "These old doomsday devices are dangerously unstable.
# I'll rest easier not knowing where they are."
Expand Down
5 changes: 2 additions & 3 deletions lib/Test/Mojo.pm
Expand Up @@ -136,7 +136,7 @@ sub finish_ok {
my ($self, $desc) = @_;

$self->tx->finish;
Mojo::IOLoop->singleton->one_tick while !$self->{finished};
Mojo::IOLoop->one_tick while !$self->{finished};
local $Test::Builder::Level = $Test::Builder::Level + 1;
Test::More::ok 1, $desc || 'finished websocket';

Expand Down Expand Up @@ -349,8 +349,7 @@ sub _get_content {

sub _message {
my $self = shift;
Mojo::IOLoop->singleton->one_tick
while !$self->{finished} && !@{$self->{messages}};
Mojo::IOLoop->one_tick while !$self->{finished} && !@{$self->{messages}};
return shift @{$self->{messages}};
}

Expand Down
195 changes: 85 additions & 110 deletions t/mojo/user_agent.t
Expand Up @@ -7,7 +7,7 @@ BEGIN {
$ENV{MOJO_IOWATCHER} = 'Mojo::IOWatcher';
}

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

# "The strong must protect the sweet."
use Mojo::IOLoop;
Expand All @@ -30,6 +30,21 @@ get '/timeout' => sub {
$self->render_later;
};

# GET /no_length
get '/no_length' => sub {
my $self = shift;
$self->finish('works too!');
$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 @@ -76,97 +91,36 @@ $ENV{https_proxy} = $backup5;
$ENV{no_proxy} = $backup6;

# User agent
$ua = Mojo::UserAgent->new->app(app);
$ua = Mojo::UserAgent->new(ioloop => Mojo::IOLoop->singleton);

# Server
my $port = Mojo::IOLoop->generate_port;
my $buffer = {};
my $last;
my $id = Mojo::IOLoop->listen(
port => $port,
on_accept => sub {
my ($loop, $id) = @_;
$last = $id;
$buffer->{$id} = '';
},
on_read => sub {
my ($loop, $id, $chunk) = @_;
$buffer->{$id} .= $chunk;
if (index $buffer->{$id}, "\x0d\x0a\x0d\x0a") {
delete $buffer->{$id};
$loop->write($id => "HTTP/1.1 200 OK\x0d\x0a"
. "Connection: keep-alive\x0d\x0a"
. "Content-Length: 6\x0d\x0a\x0d\x0aworks!");
}
},
on_error => sub {
my ($self, $id) = @_;
delete $buffer->{$id};
}
);

# Wonky server (missing Content-Length header)
my $port2 = Mojo::IOLoop->generate_port;
my $buffer2 = {};
Mojo::IOLoop->listen(
port => $port2,
on_accept => sub {
my ($loop, $id) = @_;
$buffer2->{$id} = '';
},
on_read => sub {
my ($loop, $id, $chunk) = @_;
$buffer2->{$id} .= $chunk;
if (index($buffer2->{$id}, "\x0d\x0a\x0d\x0a") >= 0) {
delete $buffer2->{$id};
$loop->write(
$id => "HTTP/1.1 200 OK\x0d\x0a"
. "Content-Type: text/plain\x0d\x0a\x0d\x0aworks too!",
sub { shift->drop(shift) }
);
}
},
on_error => sub {
my ($self, $id) = @_;
delete $buffer2->{$id};
}
);

# GET /
my $tx = $ua->get('/');
ok $tx->success, 'successful';
is $tx->res->code, 200, 'right status';
is $tx->res->body, 'works', 'right content';

# GET / (callbacks)
my $finished;
$tx = $ua->build_tx(GET => '/');
$ua->on(
start => sub {
my ($self, $tx) = @_;
$tx->on(finish => sub { $finished++ });
# GET / (non-blocking)
my ($success, $code, $body);
$ua->get(
'/' => sub {
my $tx = pop;
$success = $tx->success;
$code = $tx->res->code;
$body = $tx->res->body;
Mojo::IOLoop->stop;
}
);
$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';
Mojo::IOLoop->start;
ok $success, 'successful';
is $code, 200, 'right status';
is $body, 'works', 'right content';

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

# Fresh blocking user agent
$ua = Mojo::UserAgent->new(ioloop => Mojo::IOLoop->singleton)->app(app);

# GET / (missing Content-Lengt header)
$tx = $ua->get("http://localhost:$port2/");
ok $tx->success, 'successful';
ok !$tx->error, 'no error';
ok !$tx->kept_alive, 'kept connection not alive';
ok $tx->keep_alive, 'keep connection alive';
is $tx->res->code, 200, 'right status';
is $tx->res->body, 'works too!', 'right content';

# GET / (mock server)
$tx = $ua->get("http://localhost:$port/mock");
# GET /last (blocking)
my $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 / (mock server again)
$tx = $ua->get("http://localhost:$port/mock");
# 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 (bypassing safety net)
Mojo::IOLoop->singleton->_drop($last);
# Close connection
Mojo::IOLoop->stream($last)->emit('close');
Mojo::IOLoop->one_tick while Mojo::IOLoop->stream($last);

# GET / (mock server closed connection)
$tx = $ua->get("http://localhost:$port/mock");
# 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 / (mock server again)
$tx = $ua->get("http://localhost:$port/mock");
# 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 (bypassing safety net)
Mojo::IOLoop->singleton->_drop($last);
# Close connection
Mojo::IOLoop->stream($last)->emit('close');
Mojo::IOLoop->one_tick while Mojo::IOLoop->stream($last);

# GET / (mock server closed connection)
$tx = $ua->get("http://localhost:$port/mock");
# 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 / (mock server again)
$tx = $ua->get("http://localhost:$port/mock");
# 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';

# GET /
$tx = $ua->get('/');
ok $tx->success, 'successful';
is $tx->res->code, 200, 'right status';
is $tx->res->body, 'works', 'right content';

# GET / (callbacks)
my $finished;
$tx = $ua->build_tx(GET => '/');
$ua->on(
start => sub {
my ($self, $tx) = @_;
$tx->on(finish => sub { $finished++ });
}
);
$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';

# GET /no_length (missing Content-Lengt header)
$tx = $ua->get('/no_length');
ok $tx->success, 'successful';
ok !$tx->error, 'no error';
ok $tx->kept_alive, 'kept connection alive';
ok !$tx->keep_alive, 'keep connection not alive';
is $tx->res->code, 200, 'right status';
is $tx->res->body, 'works too!', 'right content';

# GET / (built-in server)
$tx = $ua->get('/');
ok $tx->success, 'successful';
Expand Down Expand Up @@ -302,8 +277,8 @@ Mojo::IOLoop->start;
is_deeply \@kept_alive, [1, 1], 'connections kept alive';

# Premature connection close
$port = Mojo::IOLoop->generate_port;
$id = Mojo::IOLoop->listen(
my $port = Mojo::IOLoop->generate_port;
my $id = Mojo::IOLoop->listen(
port => $port,
on_accept => sub { shift->drop(shift) }
);
Expand Down
3 changes: 3 additions & 0 deletions t/mojolicious/embedded_lite_app.json
@@ -0,0 +1,3 @@
{
"it": "just works"
}
5 changes: 4 additions & 1 deletion t/mojolicious/embedded_lite_app.t
Expand Up @@ -10,7 +10,7 @@ BEGIN {
$ENV{MOJO_MODE} = 'testing';
}

use Test::More tests => 122;
use Test::More tests => 125;

use FindBin;
use lib "$FindBin::Bin/lib";
Expand Down Expand Up @@ -184,6 +184,9 @@ $t->get_ok('/third')->status_is(200)
# GET /just/works (from external embedded app)
$t->get_ok('/just/works')->status_is(200)->content_is("It is working!\n");

# GET /just/works/too (from external embedded app)
$t->get_ok('/just/works/too')->status_is(200)->content_is("It just works!\n");

# GET /x/1/ (full external application)
$t->get_ok('/x/1/')->status_is(200)->content_is("works!\n\ntoo!works!!!\n");

Expand Down
9 changes: 8 additions & 1 deletion t/mojolicious/lib/EmbeddedTestApp.pm
@@ -1,10 +1,17 @@
package EmbeddedTestApp;
use Mojolicious::Lite;

plugin "JSONConfig";

# "But you're better than normal, you're abnormal."
get '/works' => 'works';
get '/works';

get '/works/too' => 'too';

1;
__DATA__
@@ works.html.ep
It is <%= $name %>!
@@ too.html.ep
It <%= config->{it} %>!

0 comments on commit 39de890

Please sign in to comment.