Skip to content

Commit

Permalink
Fix Http Client Pool requests that have no response (#52393)
Browse files Browse the repository at this point in the history
Fixes an issue when a HTTP Client Pool is used and a request throws a RequestException and there is no response body. Previously this would still return a Response class, with no response code or body. This now considers it a ConnectionException.
  • Loading branch information
andrewbroberg authored Aug 6, 2024
1 parent 7e69645 commit 64c45e0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Illuminate/Http/Client/PendingRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1029,7 +1029,7 @@ protected function makePromise(string $method, string $url, array $options = [],
});
})
->otherwise(function (OutOfBoundsException|TransferException $e) {
if ($e instanceof ConnectException) {
if ($e instanceof ConnectException || ($e instanceof RequestException && ! $e->hasResponse())) {
$exception = new ConnectionException($e->getMessage(), 0, $e);

$this->dispatchConnectionFailedEvent(new Request($e->getRequest()), $exception);
Expand Down
19 changes: 19 additions & 0 deletions tests/Http/HttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
use Exception;
use GuzzleHttp\Middleware;
use GuzzleHttp\Promise\PromiseInterface;
use GuzzleHttp\Promise\RejectedPromise;
use GuzzleHttp\Psr7\Response as Psr7Response;
use GuzzleHttp\Psr7\Utils;
use GuzzleHttp\TransferStats;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Http\Client\ConnectionException;
use Illuminate\Http\Client\Events\RequestSending;
use Illuminate\Http\Client\Events\ResponseReceived;
use Illuminate\Http\Client\Factory;
Expand Down Expand Up @@ -2118,6 +2120,23 @@ public function testRequestCanBeModifiedInRetryCallbackInPool()
});
}

public function testHandleRequestExeptionWithNoResponseInPoolConsideredConnectionException()
{
$requestException = new \GuzzleHttp\Exception\RequestException('Error', new \GuzzleHttp\Psr7\Request('GET', '/'));
$this->factory->fake([
'noresponse.com' => new RejectedPromise($requestException),
]);

$responses = $this->factory->pool(function (Pool $pool) {
return [
$pool->get('noresponse.com'),
];
});

self::assertInstanceOf(ConnectionException::class, $responses[0]);
self::assertSame($requestException, $responses[0]->getPrevious());
}

public function testExceptionThrownInRetryCallbackIsReturnedWithoutRetryingInPool()
{
$this->factory->fake([
Expand Down

0 comments on commit 64c45e0

Please sign in to comment.