From 4ba0b3f8ee364f0b110f8dc19fd65fd9b8dbeb42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCnther=20Debrauwer?= Date: Fri, 15 Nov 2024 17:40:26 +0100 Subject: [PATCH] [11.x] Http client: record request when faking connection exception (#53530) * Add failing tests * Record request after connect exception * Fix test --- src/Illuminate/Http/Client/Factory.php | 2 +- src/Illuminate/Http/Client/PendingRequest.php | 5 +- tests/Http/HttpClientTest.php | 51 +++++++++++++++---- 3 files changed, 47 insertions(+), 11 deletions(-) diff --git a/src/Illuminate/Http/Client/Factory.php b/src/Illuminate/Http/Client/Factory.php index 0781429c6cb8..a989f3aec152 100644 --- a/src/Illuminate/Http/Client/Factory.php +++ b/src/Illuminate/Http/Client/Factory.php @@ -322,7 +322,7 @@ protected function record() * Record a request response pair. * * @param \Illuminate\Http\Client\Request $request - * @param \Illuminate\Http\Client\Response $response + * @param \Illuminate\Http\Client\Response|null $response * @return void */ public function recordRequestResponsePair($request, $response) diff --git a/src/Illuminate/Http/Client/PendingRequest.php b/src/Illuminate/Http/Client/PendingRequest.php index d9b2e4cf2f89..e24442758649 100644 --- a/src/Illuminate/Http/Client/PendingRequest.php +++ b/src/Illuminate/Http/Client/PendingRequest.php @@ -939,8 +939,11 @@ public function send(string $method, string $url, array $options = []) }); } catch (ConnectException $e) { $exception = new ConnectionException($e->getMessage(), 0, $e); + $request = new Request($e->getRequest()); - $this->dispatchConnectionFailedEvent(new Request($e->getRequest()), $exception); + $this->factory->recordRequestResponsePair($request, null); + + $this->dispatchConnectionFailedEvent($request, $exception); throw $exception; } diff --git a/tests/Http/HttpClientTest.php b/tests/Http/HttpClientTest.php index c5fdcc8c7037..16586e652242 100644 --- a/tests/Http/HttpClientTest.php +++ b/tests/Http/HttpClientTest.php @@ -37,6 +37,7 @@ use Psr\Http\Message\ResponseInterface; use RuntimeException; use Symfony\Component\VarDumper\VarDumper; +use Throwable; class HttpClientTest extends TestCase { @@ -2200,30 +2201,60 @@ public function testFakeConnectionException() { $this->factory->fake($this->factory->failedConnection('Fake')); - $this->expectException(ConnectionException::class); - $this->expectExceptionMessage('Fake'); + $exception = null; + + try { + $this->factory->post('https://example.com'); + } catch (Throwable $e) { + $exception = $e; + } + + $this->assertNotNull($exception); + $this->assertInstanceOf(ConnectionException::class, $exception); + $this->assertSame('Fake', $exception->getMessage()); - $this->factory->post('https://example.com'); + $this->factory->assertSentCount(1); + $this->factory->assertSent(function (Request $request, ?Response $response) { + return $request->url() === 'https://example.com' && $response === null; + }); } public function testFakeConnectionExceptionWithinFakeClosure() { $this->factory->fake(fn () => $this->factory->failedConnection('Fake')); - $this->expectException(ConnectionException::class); - $this->expectExceptionMessage('Fake'); + $exception = null; + + try { + $this->factory->post('https://example.com'); + } catch (Throwable $e) { + $exception = $e; + } - $this->factory->post('https://example.com'); + $this->assertNotNull($exception); + $this->assertInstanceOf(ConnectionException::class, $exception); + $this->assertSame('Fake', $exception->getMessage()); + + $this->factory->assertSentCount(1); } public function testFakeConnectionExceptionWithinArray() { $this->factory->fake(['*' => $this->factory->failedConnection('Fake')]); - $this->expectException(ConnectionException::class); - $this->expectExceptionMessage('Fake'); + $exception = null; + + try { + $this->factory->post('https://example.com'); + } catch (Throwable $e) { + $exception = $e; + } + + $this->assertNotNull($exception); + $this->assertInstanceOf(ConnectionException::class, $exception); + $this->assertSame('Fake', $exception->getMessage()); - $this->factory->post('https://example.com'); + $this->factory->assertSentCount(1); } public function testFakeConnectionExceptionWithinSequence() @@ -2247,6 +2278,8 @@ public function testFakeConnectionExceptionWithinSequence() $this->assertNotNull($exception); $this->assertInstanceOf(ConnectionException::class, $exception); $this->assertSame('Fake', $exception->getMessage()); + + $this->factory->assertSentCount(2); } public function testMiddlewareRunsWhenFaked()