Skip to content

Commit

Permalink
[11.x] Http client: record request when faking connection exception (#…
Browse files Browse the repository at this point in the history
…53530)

* Add failing tests

* Record request after connect exception

* Fix test
  • Loading branch information
gdebrauwer authored Nov 15, 2024
1 parent e9a5cd2 commit 4ba0b3f
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/Illuminate/Http/Client/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 4 additions & 1 deletion src/Illuminate/Http/Client/PendingRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
51 changes: 42 additions & 9 deletions tests/Http/HttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
use Psr\Http\Message\ResponseInterface;
use RuntimeException;
use Symfony\Component\VarDumper\VarDumper;
use Throwable;

class HttpClientTest extends TestCase
{
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand Down

0 comments on commit 4ba0b3f

Please sign in to comment.