diff --git a/src/Illuminate/Http/Client/PendingRequest.php b/src/Illuminate/Http/Client/PendingRequest.php index 6fdfc8e6bf41..28bde9944772 100644 --- a/src/Illuminate/Http/Client/PendingRequest.php +++ b/src/Illuminate/Http/Client/PendingRequest.php @@ -1613,7 +1613,7 @@ protected function marshalRequestExceptionWithResponse(RequestException $e) $response = $this->populateResponse($this->newResponse($e->getResponse())) ); - throw $response->toException(); + throw $response->toException() ?? new ConnectionException($e->getMessage(), 0, $e); } /** diff --git a/tests/Http/HttpClientTest.php b/tests/Http/HttpClientTest.php index 9d89ee977ec6..1604be010c15 100644 --- a/tests/Http/HttpClientTest.php +++ b/tests/Http/HttpClientTest.php @@ -4,6 +4,7 @@ use Exception; use GuzzleHttp\Exception\RequestException as GuzzleRequestException; +use GuzzleHttp\Exception\TooManyRedirectsException; use GuzzleHttp\Middleware; use GuzzleHttp\Promise\PromiseInterface; use GuzzleHttp\Promise\RejectedPromise; @@ -2609,6 +2610,38 @@ public function testSslCertificateErrorsConvertedToConnectionException() $this->factory->head('https://ssl-error.laravel.example'); } + public function testTooManyRedirectsExceptionConvertedToConnectionException() + { + $this->factory->fake(function () { + $request = new GuzzleRequest('GET', 'https://redirect.laravel.example'); + $response = new Psr7Response(301, ['Location' => 'https://redirect2.laravel.example']); + + throw new TooManyRedirectsException( + 'Maximum number of redirects (5) exceeded', + $request, + $response + ); + }); + + $this->expectException(ConnectionException::class); + $this->expectExceptionMessage('Maximum number of redirects (5) exceeded'); + + $this->factory->maxRedirects(5)->get('https://redirect.laravel.example'); + } + + public function testTooManyRedirectsWithFakedRedirectChain() + { + $this->factory->fake([ + '1.example.com' => $this->factory->response(null, 301, ['Location' => 'https://2.example.com']), + '2.example.com' => $this->factory->response(null, 301, ['Location' => 'https://3.example.com']), + '3.example.com' => $this->factory->response('', 200), + ]); + + $this->expectException(ConnectionException::class); + + $this->factory->maxRedirects(1)->get('https://1.example.com'); + } + public function testRequestExceptionIsNotThrownIfThePendingRequestIsSetToThrowOnFailureButTheResponseIsSuccessful() { $this->factory->fake([