Skip to content

Commit 4ba0b3f

Browse files
authored
[11.x] Http client: record request when faking connection exception (#53530)
* Add failing tests * Record request after connect exception * Fix test
1 parent e9a5cd2 commit 4ba0b3f

File tree

3 files changed

+47
-11
lines changed

3 files changed

+47
-11
lines changed

src/Illuminate/Http/Client/Factory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ protected function record()
322322
* Record a request response pair.
323323
*
324324
* @param \Illuminate\Http\Client\Request $request
325-
* @param \Illuminate\Http\Client\Response $response
325+
* @param \Illuminate\Http\Client\Response|null $response
326326
* @return void
327327
*/
328328
public function recordRequestResponsePair($request, $response)

src/Illuminate/Http/Client/PendingRequest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -939,8 +939,11 @@ public function send(string $method, string $url, array $options = [])
939939
});
940940
} catch (ConnectException $e) {
941941
$exception = new ConnectionException($e->getMessage(), 0, $e);
942+
$request = new Request($e->getRequest());
942943

943-
$this->dispatchConnectionFailedEvent(new Request($e->getRequest()), $exception);
944+
$this->factory->recordRequestResponsePair($request, null);
945+
946+
$this->dispatchConnectionFailedEvent($request, $exception);
944947

945948
throw $exception;
946949
}

tests/Http/HttpClientTest.php

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
use Psr\Http\Message\ResponseInterface;
3838
use RuntimeException;
3939
use Symfony\Component\VarDumper\VarDumper;
40+
use Throwable;
4041

4142
class HttpClientTest extends TestCase
4243
{
@@ -2200,30 +2201,60 @@ public function testFakeConnectionException()
22002201
{
22012202
$this->factory->fake($this->factory->failedConnection('Fake'));
22022203

2203-
$this->expectException(ConnectionException::class);
2204-
$this->expectExceptionMessage('Fake');
2204+
$exception = null;
2205+
2206+
try {
2207+
$this->factory->post('https://example.com');
2208+
} catch (Throwable $e) {
2209+
$exception = $e;
2210+
}
2211+
2212+
$this->assertNotNull($exception);
2213+
$this->assertInstanceOf(ConnectionException::class, $exception);
2214+
$this->assertSame('Fake', $exception->getMessage());
22052215

2206-
$this->factory->post('https://example.com');
2216+
$this->factory->assertSentCount(1);
2217+
$this->factory->assertSent(function (Request $request, ?Response $response) {
2218+
return $request->url() === 'https://example.com' && $response === null;
2219+
});
22072220
}
22082221

22092222
public function testFakeConnectionExceptionWithinFakeClosure()
22102223
{
22112224
$this->factory->fake(fn () => $this->factory->failedConnection('Fake'));
22122225

2213-
$this->expectException(ConnectionException::class);
2214-
$this->expectExceptionMessage('Fake');
2226+
$exception = null;
2227+
2228+
try {
2229+
$this->factory->post('https://example.com');
2230+
} catch (Throwable $e) {
2231+
$exception = $e;
2232+
}
22152233

2216-
$this->factory->post('https://example.com');
2234+
$this->assertNotNull($exception);
2235+
$this->assertInstanceOf(ConnectionException::class, $exception);
2236+
$this->assertSame('Fake', $exception->getMessage());
2237+
2238+
$this->factory->assertSentCount(1);
22172239
}
22182240

22192241
public function testFakeConnectionExceptionWithinArray()
22202242
{
22212243
$this->factory->fake(['*' => $this->factory->failedConnection('Fake')]);
22222244

2223-
$this->expectException(ConnectionException::class);
2224-
$this->expectExceptionMessage('Fake');
2245+
$exception = null;
2246+
2247+
try {
2248+
$this->factory->post('https://example.com');
2249+
} catch (Throwable $e) {
2250+
$exception = $e;
2251+
}
2252+
2253+
$this->assertNotNull($exception);
2254+
$this->assertInstanceOf(ConnectionException::class, $exception);
2255+
$this->assertSame('Fake', $exception->getMessage());
22252256

2226-
$this->factory->post('https://example.com');
2257+
$this->factory->assertSentCount(1);
22272258
}
22282259

22292260
public function testFakeConnectionExceptionWithinSequence()
@@ -2247,6 +2278,8 @@ public function testFakeConnectionExceptionWithinSequence()
22472278
$this->assertNotNull($exception);
22482279
$this->assertInstanceOf(ConnectionException::class, $exception);
22492280
$this->assertSame('Fake', $exception->getMessage());
2281+
2282+
$this->factory->assertSentCount(2);
22502283
}
22512284

22522285
public function testMiddlewareRunsWhenFaked()

0 commit comments

Comments
 (0)