Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/Illuminate/Http/Client/PendingRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1572,8 +1572,10 @@ protected function marshalConnectionException(ConnectException $e)
{
$exception = new ConnectionException($e->getMessage(), 0, $e);

$request = new Request($e->getRequest());

$this->factory?->recordRequestResponsePair(
$request = new Request($e->getRequest()), null
$request, null
);

$this->dispatchConnectionFailedEvent($request, $exception);
Expand All @@ -1591,8 +1593,10 @@ protected function marshalRequestExceptionWithoutResponse(RequestException $e)
{
$exception = new ConnectionException($e->getMessage(), 0, $e);

$request = new Request($e->getRequest());

$this->factory?->recordRequestResponsePair(
$request = new Request($e->getRequest()), null
$request, null
);

$this->dispatchConnectionFailedEvent($request, $exception);
Expand All @@ -1608,9 +1612,11 @@ protected function marshalRequestExceptionWithoutResponse(RequestException $e)
*/
protected function marshalRequestExceptionWithResponse(RequestException $e)
{
$response = $this->populateResponse($this->newResponse($e->getResponse()));

$this->factory?->recordRequestResponsePair(
new Request($e->getRequest()),
$response = $this->populateResponse($this->newResponse($e->getResponse()))
$response
);

throw $response->toException() ?? new ConnectionException($e->getMessage(), 0, $e);
Expand Down
73 changes: 72 additions & 1 deletion tests/Http/HttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Illuminate\Tests\Http;

use Exception;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\RequestException as GuzzleRequestException;
use GuzzleHttp\Exception\TooManyRedirectsException;
use GuzzleHttp\Middleware;
Expand Down Expand Up @@ -2379,7 +2380,7 @@ public function testRequestCanBeModifiedInRetryCallbackInPool()

public function testHandleRequestExeptionWithNoResponseInPoolConsideredConnectionException()
{
$requestException = new \GuzzleHttp\Exception\RequestException('Error', new \GuzzleHttp\Psr7\Request('GET', '/'));
$requestException = new GuzzleRequestException('Error', new \GuzzleHttp\Psr7\Request('GET', '/'));
$this->factory->fake([
'noresponse.com' => new RejectedPromise($requestException),
]);
Expand Down Expand Up @@ -2610,6 +2611,76 @@ public function testSslCertificateErrorsConvertedToConnectionException()
$this->factory->head('https://ssl-error.laravel.example');
}

public function testConnectExceptionIsConvertedToConnectionExceptionEvenWhenWithoutFactory()
{
$this->expectException(ConnectionException::class);
$this->expectExceptionMessage('cURL error 60: SSL certificate problem');

$pendingRequest = new PendingRequest();

$pendingRequest->setHandler(function () {
throw new ConnectException(
'cURL error 60: SSL certificate problem: unable to get local issuer certificate',
new GuzzleRequest('HEAD', 'https://ssl-error.laravel.example')
);
});

$pendingRequest->head('https://ssl-error.laravel.example');
}

public function testRequestExceptionWithoutResponseIsConvertedToConnectionExceptionEvenWhenWithoutFactory()
{
$this->expectException(ConnectionException::class);
$this->expectExceptionMessage('cURL error 28: Operation timed out');

$pendingRequest = new PendingRequest();

$pendingRequest->setHandler(function () {
throw new GuzzleRequestException(
'cURL error 28: Operation timed out',
new GuzzleRequest('GET', 'https://timeout-laravel.example')
);
});

$pendingRequest->get('https://timeout-laravel.example');
}

public function testRequestExceptionWithResponseIsConvertedToConnectionExceptionEvenWhenWithoutFactory()
{
$this->expectException(ConnectionException::class);
$this->expectExceptionMessage('cURL error 28: Operation timed out');

$pendingRequest = new PendingRequest();

$pendingRequest->setHandler(function () {
throw new GuzzleRequestException(
'cURL error 28: Operation timed out',
new GuzzleRequest('GET', 'https://timeout-laravel.example'),
new Psr7Response(301)
);
});

$pendingRequest->get('https://timeout-laravel.example');
}

public function testTooManyRedirectsExceptionIsConvertedToConnectionExceptionEvenWhenWithoutFactory()
{
$this->expectException(ConnectionException::class);
$this->expectExceptionMessage('Maximum number of redirects (5) exceeded');

$pendingRequest = new PendingRequest();

$pendingRequest->setHandler(function () {
throw new TooManyRedirectsException(
'Maximum number of redirects (5) exceeded',
new GuzzleRequest('GET', 'https://redirect.laravel.example'),
new Psr7Response(301)
);
});

$pendingRequest->maxRedirects(5)->get('https://redirect.laravel.example');
}

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