From 66b104780823c2049be2fc90e3eadf724a2d5f14 Mon Sep 17 00:00:00 2001 From: Andrew Broberg Date: Tue, 6 Aug 2024 09:58:14 +1000 Subject: [PATCH] Fix Http Client Pool requests that have no response 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. --- src/Illuminate/Http/Client/PendingRequest.php | 2 +- tests/Http/HttpClientTest.php | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Http/Client/PendingRequest.php b/src/Illuminate/Http/Client/PendingRequest.php index 44aa02e0feec..f43bd3d4d671 100644 --- a/src/Illuminate/Http/Client/PendingRequest.php +++ b/src/Illuminate/Http/Client/PendingRequest.php @@ -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); diff --git a/tests/Http/HttpClientTest.php b/tests/Http/HttpClientTest.php index 659c9052e2ae..2198ce09dd9d 100644 --- a/tests/Http/HttpClientTest.php +++ b/tests/Http/HttpClientTest.php @@ -35,6 +35,7 @@ use Psr\Http\Message\ResponseInterface; use RuntimeException; use Symfony\Component\VarDumper\VarDumper; +use Illuminate\Http\Client\ConnectionException; class HttpClientTest extends TestCase { @@ -2118,6 +2119,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([