Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Check for HTTP errors after attempting to cache connection for reuse
fixes #935
  • Loading branch information
jberger committed Mar 12, 2016
1 parent dd66d84 commit 213bbcb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
11 changes: 6 additions & 5 deletions lib/Mojo/UserAgent.pm
Expand Up @@ -218,14 +218,11 @@ sub _finish {
$c->{ioloop}->remove($c->{timeout}) if $c->{timeout};
return $self->_reuse($id, $close) unless my $old = $c->{tx};

# Premature connection close or 4xx/5xx
# Premature connection close
my $res = $old->closed->res->finish;
if ($close && !$res->code && !$res->error) {
$res->error({message => 'Premature connection close'});
}
elsif ($res->is_status_class(400) || $res->is_status_class(500)) {
$res->error({message => $res->message, code => $res->code});
}

# Always remove connection for WebSockets
return $self->_remove($id) if $old->is_websocket;
Expand All @@ -240,8 +237,12 @@ sub _finish {
return $new->client_read($old->res->content->leftovers);
}

# Finish connection and handle redirects
# Finish connection
$self->_reuse($id, $close);

if ($res->is_status_class(400) || $res->is_status_class(500)) {
$res->error({message => $res->message, code => $res->code});
}
$c->{cb}($self, $old) unless $self->_redirect($c, $old);
}

Expand Down
8 changes: 8 additions & 0 deletions t/mojo/user_agent.t
Expand Up @@ -362,7 +362,15 @@ ok $tx->res->is_limit_exceeded, 'limit is exceeded';

# 404 response
$tx = $ua->get('/does_not_exist');
ok !$tx->success, 'not successful';
ok !$tx->kept_alive, 'kept connection not alive';
ok $tx->keep_alive, 'keep connection alive';
is $tx->error->{message}, 'Not Found', 'right error';
is $tx->error->{code}, 404, 'right status';
$tx = $ua->get('/does_not_exist');
ok !$tx->success, 'not successful';
ok $tx->kept_alive, 'kept connection alive';
ok $tx->keep_alive, 'keep connection alive';
is $tx->error->{message}, 'Not Found', 'right error';
is $tx->error->{code}, 404, 'right status';

Expand Down

0 comments on commit 213bbcb

Please sign in to comment.