Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
more diverse WebSocket tests
  • Loading branch information
kraih committed Feb 26, 2015
1 parent 0d4ef05 commit edd4313
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 33 deletions.
2 changes: 1 addition & 1 deletion lib/Mojo/Server/Daemon.pm
Expand Up @@ -165,7 +165,7 @@ sub _listen {
my ($loop, $stream, $id) = @_;

my $c = $self->{connections}{$id} = {tls => $tls};
warn "-- Accept (@{[$stream->handle->peerhost]})\n" if DEBUG;
warn "-- Accept $id (@{[$stream->handle->peerhost]})\n" if DEBUG;
$stream->timeout($self->inactivity_timeout);

$stream->on(close => sub { $self && $self->_close($id) });
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojo/UserAgent.pm
Expand Up @@ -187,7 +187,7 @@ sub _connection {
my ($proto, $host, $port) = $self->transactor->endpoint($tx);
$id ||= $self->_dequeue($nb, "$proto:$host:$port", 1);
if ($id && !ref $id) {
warn "-- Reusing connection ($proto://$host:$port)\n" if DEBUG;
warn "-- Reusing connection $id ($proto://$host:$port)\n" if DEBUG;
$self->{connections}{$id} = {cb => $cb, nb => $nb, tx => $tx};
$tx->kept_alive(1) unless $tx->connection;
$self->_connected($id);
Expand All @@ -198,8 +198,8 @@ sub _connection {
if (my $id = $self->_connect_proxy($nb, $tx, $cb)) { return $id }

# Connect
warn "-- Connect ($proto://$host:$port)\n" if DEBUG;
$id = $self->_connect($nb, 1, $tx, $id, \&_connected);
warn "-- Connect $id ($proto://$host:$port)\n" if DEBUG;
$self->{connections}{$id} = {cb => $cb, nb => $nb, tx => $tx};

return $id;
Expand Down
4 changes: 2 additions & 2 deletions lib/Test/Mojo.pm
Expand Up @@ -885,8 +885,8 @@ Perform a C<POST> request and check for transport errors, takes the same
arguments as L<Mojo::UserAgent/"post">, except for the callback.
# Test file upload
$t->post_ok('/upload' => form => {foo => {content => 'bar'}})
->status_is(200);
my $upload = {foo => {content => 'bar', filename => 'baz.txt'}};
$t->post_ok('/upload' => form => $upload)->status_is(200);
# Test JSON API
$t->post_ok('/hello.json' => json => {hello => 'world'})
Expand Down
3 changes: 1 addition & 2 deletions t/mojo/user_agent_tls.t
Expand Up @@ -68,8 +68,7 @@ $ua->ioloop->client(
}
);
$ua->ioloop->start;
$tx = $ua->build_tx(GET => 'https://lalala/');
$tx->connection($sock);
$tx = $ua->build_tx(GET => 'https://lalala/')->connection($sock);
$ua->start($tx);
ok $tx->success, 'successful';
is $tx->req->method, 'GET', 'right method';
Expand Down
53 changes: 27 additions & 26 deletions t/mojo/websocket.t
Expand Up @@ -51,16 +51,21 @@ websocket '/socket' => sub {
$c->send(
$c->req->headers->host => sub {
my $c = shift;
$c->send(Mojo::IOLoop->stream($c->tx->connection)->timeout);
$c->finish(1000 => 'I ♥ Mojolicious!');
$c->send(Mojo::IOLoop->stream($c->tx->connection)->timeout)->finish;
}
)->rendered(101);
};

websocket '/early_start' => sub {
my $c = shift;
$c->send('test1');
$c->on(message => sub { shift->send(shift() . 'test2')->finish });
$c->on(
message => sub {
my ($c, $msg) = @_;
$c->send("${msg}test2");
$c->finish(1000 => 'I ♥ Mojolicious!');
}
);
};

websocket '/denied' => sub {
Expand Down Expand Up @@ -163,52 +168,46 @@ $ua->websocket(
Mojo::IOLoop->start;
ok !$ws, 'not a WebSocket';
is $code, 200, 'right status';
ok $body =~ /^(\d+)failed!$/, 'right content';
is $1, 15, 'right timeout';
ok $body =~ /^(\d+)failed!$/ && $1 == 15, 'right content';

# Using an already prepared socket
my $port = $ua->server->nb_url->port;
my $tx = $ua->build_websocket_tx('ws://lalala/socket');
my $tx = $ua->build_websocket_tx('ws://lalala/socket');
my $finished;
$tx->on(finish => sub { $finished++ });
my $port = $ua->server->nb_url->port;
my $sock = IO::Socket::INET->new(PeerAddr => '127.0.0.1', PeerPort => $port);
$sock->blocking(0);
$tx->connection($sock);
$result = '';
my ($local, $early, $status, $msg);
my $early;
$ua->start(
$tx => sub {
my ($ua, $tx) = @_;
$early = $finished;
$tx->on(
finish => sub {
my ($tx, $code, $reason) = @_;
$status = $code;
$msg = $reason;
Mojo::IOLoop->stop;
}
);
$tx->on(finish => sub { Mojo::IOLoop->stop });
$tx->on(message => sub { $result .= pop });
$local = Mojo::IOLoop->stream($tx->connection)->handle->sockport;
}
);
Mojo::IOLoop->start;
is $finished, 1, 'finish event has been emitted once';
is $early, 1, 'finish event has been emitted at the right time';
is $status, 1000, 'right status';
is $msg, 'I ♥ Mojolicious!', 'right message';
ok $result =~ /^lalala(\d+)$/, 'right result';
is $1, 15, 'right timeout';
ok $local, 'local port';
is $finished, 1, 'finish event has been emitted once';
is $early, 1, 'finish event has been emitted at the right time';
ok $result =~ /^lalala(\d+)$/ && $1 == 15, 'right result';
is(Mojo::IOLoop->stream($tx->connection)->handle, $sock,
'right connection id');

# Server directly sends a message
$result = undef;
my ($status, $msg);
$ua->websocket(
'/early_start' => sub {
my ($ua, $tx) = @_;
$tx->on(finish => sub { Mojo::IOLoop->stop });
$tx->on(
finish => sub {
my ($tx, $code, $reason) = @_;
($status, $msg) = ($code, $reason);
Mojo::IOLoop->stop;
}
);
$tx->on(
message => sub {
my ($tx, $msg) = @_;
Expand All @@ -219,7 +218,9 @@ $ua->websocket(
}
);
Mojo::IOLoop->start;
is $result, 'test3test2', 'right result';
is $status, 1000, 'right status';
is $msg, 'I ♥ Mojolicious!', 'right message';
is $result, 'test3test2', 'right result';

# Connection denied
($stash, $code, $ws) = ();
Expand Down

0 comments on commit edd4313

Please sign in to comment.