Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.x] Http client: record request when faking connection exception #53530

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
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