Skip to content

Commit

Permalink
fix state transition bugs in Mojo::Transaction::HTTP
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Sep 22, 2016
1 parent f6a27f6 commit e135670
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 3 deletions.
4 changes: 3 additions & 1 deletion Changes
@@ -1,5 +1,7 @@

7.08 2016-09-21
7.08 2016-09-22
- Improved accuracy of finished_ok test in Test::Mojo.
- Fixed state transition bugs in Mojo::Transaction::HTTP.

7.07 2016-09-20
- Fixed bug in Mojo::UserAgent::Transactor where 303 redirects would not be
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Transaction/HTTP.pm
Expand Up @@ -96,7 +96,7 @@ sub _headers {
@$self{qw(http_state offset)} = ('body', 0);

# Response without body
if ($head && $self->is_empty) { $self->completed }
if ($head && $self->is_empty) { $self->completed->{http_state} = 'empty' }

# Body
else { $self->{write} = $msg->content->is_dynamic ? 1 : $msg->body_size }
Expand Down
2 changes: 1 addition & 1 deletion lib/Test/Mojo.pm
Expand Up @@ -113,7 +113,7 @@ sub finished_ok {
my ($self, $code) = @_;
Mojo::IOLoop->one_tick while !$self->{finished};
Test::More::diag "WebSocket closed with status $self->{finished}[0]"
unless my $ok = grep { $self->{finished}[0] == $_ } $code, 1006;
unless my $ok = $self->{finished}[0] == $code;
return $self->_test('ok', $ok, "WebSocket closed with status $code");
}

Expand Down
17 changes: 17 additions & 0 deletions t/mojo/websocket.t
Expand Up @@ -57,6 +57,11 @@ websocket '/early_start' => sub {
);
};

websocket '/early_finish' => sub {
my $c = shift;
Mojo::IOLoop->next_tick(sub { $c->rendered(101)->finish(4000, 'kaboom') });
};

websocket '/denied' => sub {
my $c = shift;
$c->tx->handshake->on(finish => sub { $c->stash->{handshake}++ });
Expand Down Expand Up @@ -187,6 +192,18 @@ is $status, 1000, 'right status';
is $msg, 'I ♥ Mojolicious!', 'right message';
is $result, 'test0test2test1', 'right result';

# WebSocket connection gets closed very fast
$status = undef;
$ua->websocket(
'/early_finish' => sub {
my ($ua, $tx) = @_;
$tx->on(finish => sub { $status = [@_[1, 2]]; Mojo::IOLoop->stop });
}
);
Mojo::IOLoop->start;
is $status->[0], 4000, 'right status';
is $status->[1], 'kaboom', 'right message';

# Connection denied
($stash, $code, $ws) = ();
app->plugins->once(before_dispatch => sub { $stash = shift->stash });
Expand Down
1 change: 1 addition & 0 deletions t/mojolicious/websocket_lite_app.t
Expand Up @@ -25,6 +25,7 @@ get '/echo' => {text => 'plain echo!'};
websocket '/no_compression' => sub {
my $c = shift;
$c->on(binary => sub { shift->send({binary => shift}) });
$c->render(text => 'this should be ignored', status => 101);
};

websocket '/protocols' => sub {
Expand Down

0 comments on commit e135670

Please sign in to comment.