Skip to content

Commit

Permalink
"0" is also a valid subprotocol
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jan 19, 2016
1 parent 6a7e55e commit ae07f91
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/Mojo/Transaction/WebSocket.pm
Expand Up @@ -136,8 +136,8 @@ sub with_protocols {
my $self = shift;

my %protos = map { trim($_) => 1 } split ',',
$self->req->headers->sec_websocket_protocol || '';
return undef unless my $proto = first { $protos{$_} } @_;
$self->req->headers->sec_websocket_protocol // '';
return undef unless defined(my $proto = first { $protos{$_} } @_);

$self->res->headers->sec_websocket_protocol($proto);
return $proto;
Expand Down
7 changes: 6 additions & 1 deletion t/mojolicious/websocket_lite_app.t
Expand Up @@ -29,7 +29,7 @@ websocket '/no_compression' => sub {

websocket '/protocols' => sub {
my $c = shift;
$c->send($c->tx->with_protocols('foo', 'bar', 'baz') // 'none');
$c->send($c->tx->with_protocols('foo', 'bar', 'baz', '0') // 'none');
$c->send($c->tx->protocol // 'none');
};

Expand Down Expand Up @@ -225,6 +225,11 @@ $t->websocket_ok('/protocols' => ['baz', 'bar', 'foo'])
is $t->tx->protocol, 'foo', 'right protocol';
is $t->tx->res->headers->sec_websocket_protocol, 'foo',
'right "Sec-WebSocket-Protocol" value';
$t->websocket_ok('/protocols' => ['0'])->message_ok->message_is('0')
->message_ok->message_is('0')->finish_ok;
is $t->tx->protocol, '0', 'right protocol';
is $t->tx->res->headers->sec_websocket_protocol, '0',
'right "Sec-WebSocket-Protocol" value';
$t->websocket_ok('/protocols' => [''])->message_ok->message_is('none')
->message_ok->message_is('none')->finish_ok;
is $t->tx->protocol, undef, 'no protocol';
Expand Down

0 comments on commit ae07f91

Please sign in to comment.