Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
test and documentation tweaks
  • Loading branch information
kraih committed Mar 21, 2013
1 parent 12fb487 commit 15bc6de
Show file tree
Hide file tree
Showing 12 changed files with 63 additions and 61 deletions.
4 changes: 2 additions & 2 deletions Changes
@@ -1,8 +1,8 @@

3.92 2013-03-18
3.92 2013-03-21
- Improved WebSocket send method to stringify objects. (jberger)
- Improved documentation.
- Improved tests.
- Improved WebSocket send method to force stringification on object.

3.91 2013-03-17
- Improved bad charset handling in Mojo::DOM::HTML.
Expand Down
16 changes: 8 additions & 8 deletions lib/Mojo/Transaction/WebSocket.pm
Expand Up @@ -195,15 +195,15 @@ sub send {
my ($self, $frame, $cb) = @_;

# Binary or raw text
if (ref $frame eq 'HASH') {
$frame
= exists $frame->{text}
? [1, 0, 0, 0, TEXT, $frame->{text}]
: [1, 0, 0, 0, BINARY, $frame->{binary}];
}
$frame
= exists $frame->{text}
? [1, 0, 0, 0, TEXT, $frame->{text}]
: [1, 0, 0, 0, BINARY, $frame->{binary}]
if ref $frame eq 'HASH';

# Text or object (forcing stringification)
elsif (ref $frame ne 'ARRAY') { $frame = [1, 0, 0, 0, TEXT, encode('UTF-8', "$frame")] }
$frame = [1, 0, 0, 0, TEXT, encode('UTF-8', "$frame")]
if ref $frame ne 'ARRAY';

$self->once(drain => $cb) if $cb;
$self->{write} .= $self->build_frame(@$frame);
Expand Down Expand Up @@ -567,8 +567,8 @@ Resume C<handshake> transaction.
$ws = $ws->send({binary => $bytes});
$ws = $ws->send({text => $bytes});
$ws = $ws->send([$fin, $rsv1, $rsv2, $rsv3, $op, $bytes]);
$ws = $ws->send($chars);
$ws = $ws->send(Mojo::ByteStream->new($chars));
$ws = $ws->send($chars);
$ws = $ws->send($chars => sub {...});
Send message or frame non-blocking via WebSocket, the optional drain callback
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/Command/get.pm
Expand Up @@ -65,7 +65,7 @@ sub run {
my $v = my $buffer = '';
$ua->on(
start => sub {
my $tx = pop;
my ($ua, $tx) = @_;

# Verbose callback
my $v = $verbose;
Expand Down
1 change: 1 addition & 0 deletions lib/Mojolicious/Controller.pm
Expand Up @@ -822,6 +822,7 @@ is set to the value C<XMLHttpRequest>.
$c = $c->send({binary => $bytes});
$c = $c->send({text => $bytes});
$c = $c->send([$fin, $rsv1, $rsv2, $rsv3, $op, $bytes]);
$c = $c->send(Mojo::ByteStream->new($chars));
$c = $c->send($chars);
$c = $c->send($chars => sub {...});
Expand Down
2 changes: 1 addition & 1 deletion lib/Test/Mojo.pm
Expand Up @@ -302,7 +302,7 @@ sub websocket_ok {
$self->{finished} = 0;
$self->ua->websocket(
$url => @_ => sub {
my $tx = pop;
my ($ua, $tx) = @_;
$self->tx($tx);
$tx->on(finish => sub { $self->{finished} = 1 });
$tx->on(binary => sub { push @{$self->{messages}}, [binary => pop] });
Expand Down
2 changes: 1 addition & 1 deletion t/mojo/user_agent.t
Expand Up @@ -139,7 +139,7 @@ $ua = Mojo::UserAgent->new(ioloop => Mojo::IOLoop->singleton);
my ($success, $code, $body);
$ua->get(
'/' => sub {
my $tx = pop;
my ($ua, $tx) = @_;
$success = $tx->success;
$code = $tx->res->code;
$body = $tx->res->body;
Expand Down
4 changes: 2 additions & 2 deletions t/mojo/user_agent_online.t
Expand Up @@ -34,7 +34,7 @@ my $ua = Mojo::UserAgent->new;
my ($id, $code);
$ua->get(
'http://metacpan.org' => sub {
my $tx = pop;
my ($ua, $tx) = @_;
$id = $tx->connection;
$code = $tx->res->code;
$loop->stop;
Expand Down Expand Up @@ -101,7 +101,7 @@ Mojo::IOLoop->singleton->start;
my $kept_alive;
$ua->get(
'http://mojolicio.us' => sub {
my $tx = pop;
my ($ua, $tx) = @_;
Mojo::IOLoop->singleton->stop;
$kept_alive = $tx->kept_alive;
}
Expand Down
85 changes: 43 additions & 42 deletions t/mojo/websocket.t
Expand Up @@ -8,11 +8,11 @@ BEGIN {

use Test::More;
use IO::Socket::INET;
use Mojo::ByteStream 'b';
use Mojo::IOLoop;
use Mojo::Transaction::WebSocket;
use Mojo::UserAgent;
use Mojolicious::Lite;
use Mojo::ByteStream 'b';

# Max WebSocket size
{
Expand Down Expand Up @@ -88,7 +88,7 @@ websocket '/subreq' => sub {
my $self = shift;
$self->ua->websocket(
'/echo' => sub {
my $tx = pop;
my ($ua, $tx) = @_;
$tx->on(
message => sub {
my ($tx, $msg) = @_;
Expand All @@ -114,22 +114,22 @@ websocket '/echo' => sub {
);
};

websocket '/echo_object' => sub {
my $self = shift;
$self->on(
my $buffer = '';
websocket '/double_echo' => sub {
shift->on(
message => sub {
my ($self, $msg) = @_;
$self->send(b($msg));
$self->send($msg => sub { shift->send($msg) });
}
);
};

my $buffer = '';
websocket '/double_echo' => sub {
shift->on(
websocket '/squish' => sub {
my $self = shift;
$self->on(
message => sub {
my ($self, $msg) = @_;
$self->send($msg => sub { shift->send($msg) });
$self->send(b($msg)->squish);
}
);
};
Expand Down Expand Up @@ -165,7 +165,7 @@ like $res->body, qr/Page not found/, 'right content';
my $result;
$ua->websocket(
'/' => sub {
my $tx = pop;
my ($ua, $tx) = @_;
$tx->on(finish => sub { Mojo::IOLoop->stop });
$tx->on(
message => sub {
Expand All @@ -180,29 +180,11 @@ $ua->websocket(
Mojo::IOLoop->start;
like $result, qr!test1test2ws://localhost:\d+/!, 'right result';

# Plain WebSocket sending Mojo::ByteStream object
$ua->websocket(
'/echo_object' => sub {
my $tx = pop;
$tx->on(finish => sub { Mojo::IOLoop->stop });
$tx->on(
message => sub {
my ($tx, $msg) = @_;
$result = $msg;
$tx->finish;
}
);
$tx->send('test1');
}
);
Mojo::IOLoop->start;
is $result, 'test1', 'right result';

# Failed WebSocket connection
my ($code, $body, $ws);
$ua->websocket(
'/something/else' => sub {
my $tx = pop;
my ($ua, $tx) = @_;
$ws = $tx->is_websocket;
$code = $tx->res->code;
$body = $tx->res->body;
Expand All @@ -227,7 +209,7 @@ $result = '';
my ($local, $early);
$ua->start(
$tx => sub {
my $tx = pop;
my ($ua, $tx) = @_;
$early = $finished;
$tx->on(finish => sub { Mojo::IOLoop->stop });
$tx->on(
Expand All @@ -254,7 +236,7 @@ $result = undef;
my $client;
$ua->websocket(
'/early_start' => sub {
my $tx = pop;
my ($ua, $tx) = @_;
$tx->on(
finish => sub {
$client += 2;
Expand All @@ -279,7 +261,7 @@ is $client, 3, 'finish event has been emitted';
$code = $ws = undef;
$ua->websocket(
'/denied' => sub {
my $tx = pop;
my ($ua, $tx) = @_;
$ws = $tx->is_websocket;
$code = $tx->res->code;
Mojo::IOLoop->stop;
Expand All @@ -296,7 +278,7 @@ $finished = 0;
($code, $result) = ();
$ua->websocket(
'/subreq' => sub {
my $tx = pop;
my ($ua, $tx) = @_;
$code = $tx->res->code;
$tx->on(
message => sub {
Expand Down Expand Up @@ -327,7 +309,7 @@ my ($code2, $result2);
$delay->begin;
$ua->websocket(
'/subreq' => sub {
my $tx = pop;
my ($ua, $tx) = @_;
$code = $tx->res->code;
$tx->on(
message => sub {
Expand All @@ -347,7 +329,7 @@ $ua->websocket(
$delay->begin;
$ua->websocket(
'/subreq' => sub {
my $tx = pop;
my ($ua, $tx) = @_;
$code2 = $tx->res->code;
$tx->on(
message => sub {
Expand Down Expand Up @@ -378,7 +360,7 @@ $client = 0;
my ($drain, $counter);
$ua->websocket(
'/echo' => sub {
my $tx = pop;
my ($ua, $tx) = @_;
$tx->on(
finish => sub {
$client += 2;
Expand Down Expand Up @@ -412,7 +394,7 @@ $result = '';
$counter = $client = 0;
$ua->websocket(
'/double_echo' => sub {
my $tx = pop;
my ($ua, $tx) = @_;
$tx->on(
finish => sub {
$client += 2;
Expand All @@ -434,12 +416,31 @@ Mojo::IOLoop->start;
is $result, 'hi!hi!', 'right result';
is $client, 3, 'finish event has been emitted';

# Sending objects
$result = undef;
$ua->websocket(
'/squish' => sub {
my ($ua, $tx) = @_;
$tx->on(finish => sub { Mojo::IOLoop->stop });
$tx->on(
message => sub {
my ($tx, $msg) = @_;
$result = $msg;
$tx->finish;
}
);
$tx->send(b(' foo bar '));
}
);
Mojo::IOLoop->start;
is $result, 'foo bar', 'right result';

# Dies
$finished = $code = undef;
my ($websocket, $msg);
$ua->websocket(
'/dead' => sub {
my $tx = pop;
my ($ua, $tx) = @_;
$finished = $tx->is_finished;
$websocket = $tx->is_websocket;
$code = $tx->res->code;
Expand All @@ -457,7 +458,7 @@ is $msg, 'Internal Server Error', 'right message';
($websocket, $code, $msg) = ();
$ua->websocket(
'/foo' => sub {
my $tx = pop;
my ($ua, $tx) = @_;
$websocket = $tx->is_websocket;
$code = $tx->res->code;
$msg = $tx->res->message;
Expand All @@ -482,7 +483,7 @@ Mojo::IOLoop->start;
$result = undef;
$ua->websocket(
'/echo' => sub {
my $tx = pop;
my ($ua, $tx) = @_;
$tx->on(finish => sub { Mojo::IOLoop->stop });
$tx->on(
message => sub {
Expand Down Expand Up @@ -514,7 +515,7 @@ like $log, qr/Inactivity timeout\./, 'right log message';
my $pong;
$ua->websocket(
'/echo' => sub {
my $tx = pop;
my ($ua, $tx) = @_;
$tx->on(
frame => sub {
my ($tx, $frame) = @_;
Expand Down
2 changes: 1 addition & 1 deletion t/mojo/websocket_proxy.t
Expand Up @@ -136,7 +136,7 @@ is $result, "Hello World! / http://localhost:$port/", 'right content';
$result = undef;
$ua->websocket(
"ws://localhost:$port/test" => sub {
my $tx = pop;
my ($ua, $tx) = @_;
$tx->on(finish => sub { Mojo::IOLoop->stop });
$tx->on(
message => sub {
Expand Down
2 changes: 1 addition & 1 deletion t/mojo/websocket_proxy_tls.t
Expand Up @@ -187,7 +187,7 @@ $ua->unsubscribe('start');
$result = undef;
$ua->websocket(
"wss://localhost:$port/test" => sub {
my $tx = pop;
my ($ua, $tx) = @_;
$tx->on(finish => sub { Mojo::IOLoop->stop });
$tx->on(
message => sub {
Expand Down
2 changes: 1 addition & 1 deletion t/mojolicious/embedded_lite_app.t
Expand Up @@ -41,7 +41,7 @@ get '/bye' => sub {
$self->render_later;
$self->ua->app(main::app())->get(
'/hello/hello' => sub {
my $tx = pop;
my ($ua, $tx) = @_;
$self->render_text($tx->res->body . "$name! $nb");
}
);
Expand Down
2 changes: 1 addition & 1 deletion t/mojolicious/lite_app.t
Expand Up @@ -337,7 +337,7 @@ get '/subrequest_non_blocking' => sub {
my $self = shift;
$self->ua->post(
'/template' => sub {
my $tx = pop;
my ($ua, $tx) = @_;
$self->render_text($tx->res->body . $self->stash->{nb});
$nb = $self->stash->{nb};
}
Expand Down

0 comments on commit 15bc6de

Please sign in to comment.