Skip to content

Commit

Permalink
Fix Http Client Pool requests that have no response
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 committed Aug 5, 2024
1 parent 7e69645 commit 66b1047
Show file tree
Hide file tree
Showing 2 changed files with 19 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
18 changes: 18 additions & 0 deletions tests/Http/HttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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([
Expand Down

0 comments on commit 66b1047

Please sign in to comment.