Skip to content

Commit 76cee6b

Browse files
authored
[12.X] Fix Http::preventStrayRequests error propagation when using Http::pool (#55689)
* add a dedicated error for stray request exception * re-throw the error if it happens in a pool context * update tests * fix test * fix * fix
1 parent 98043a8 commit 76cee6b

File tree

3 files changed

+47
-4
lines changed

3 files changed

+47
-4
lines changed

src/Illuminate/Http/Client/PendingRequest.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
use OutOfBoundsException;
2727
use Psr\Http\Message\MessageInterface;
2828
use Psr\Http\Message\RequestInterface;
29-
use RuntimeException;
3029
use Symfony\Component\VarDumper\VarDumper;
3130

3231
class PendingRequest
@@ -1033,7 +1032,11 @@ protected function makePromise(string $method, string $url, array $options = [],
10331032
$this->dispatchResponseReceivedEvent($response);
10341033
});
10351034
})
1036-
->otherwise(function (OutOfBoundsException|TransferException $e) {
1035+
->otherwise(function (OutOfBoundsException|TransferException|StrayRequestException $e) {
1036+
if ($e instanceof StrayRequestException) {
1037+
throw $e;
1038+
}
1039+
10371040
if ($e instanceof ConnectException || ($e instanceof RequestException && ! $e->hasResponse())) {
10381041
$exception = new ConnectionException($e->getMessage(), 0, $e);
10391042

@@ -1334,7 +1337,7 @@ public function buildStubHandler()
13341337

13351338
if (is_null($response)) {
13361339
if ($this->preventStrayRequests) {
1337-
throw new RuntimeException('Attempted request to ['.(string) $request->getUri().'] without a matching fake.');
1340+
throw new StrayRequestException((string) $request->getUri());
13381341
}
13391342

13401343
return $handler($request, $options);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Illuminate\Http\Client;
4+
5+
use RuntimeException;
6+
7+
class StrayRequestException extends RuntimeException
8+
{
9+
public function __construct(string $uri)
10+
{
11+
parent::__construct('Attempted request to ['.$uri.'] without a matching fake.');
12+
}
13+
}

tests/Http/HttpClientTest.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Illuminate\Http\Client\RequestException;
2222
use Illuminate\Http\Client\Response;
2323
use Illuminate\Http\Client\ResponseSequence;
24+
use Illuminate\Http\Client\StrayRequestException;
2425
use Illuminate\Http\Response as HttpResponse;
2526
use Illuminate\Support\Arr;
2627
use Illuminate\Support\Carbon;
@@ -3178,12 +3179,38 @@ public function testItCanEnforceFaking()
31783179
$responses[] = $this->factory->get('https://forge.laravel.com')->body();
31793180
$this->assertSame(['ok', 'ok'], $responses);
31803181

3181-
$this->expectException(RuntimeException::class);
3182+
$this->expectException(StrayRequestException::class);
31823183
$this->expectExceptionMessage('Attempted request to [https://laravel.com] without a matching fake.');
31833184

31843185
$this->factory->get('https://laravel.com');
31853186
}
31863187

3188+
public function testItCanEnforceFakingInThePool()
3189+
{
3190+
$this->factory->preventStrayRequests();
3191+
$this->factory->fake(['https://vapor.laravel.com' => Factory::response('ok', 200)]);
3192+
$this->factory->fake(['https://forge.laravel.com' => Factory::response('ok', 200)]);
3193+
3194+
$responses = $this->factory->pool(function (Pool $pool) {
3195+
return [
3196+
$pool->get('https://vapor.laravel.com'),
3197+
$pool->get('https://forge.laravel.com'),
3198+
];
3199+
});
3200+
3201+
$this->assertSame(200, $responses[0]->status());
3202+
$this->assertSame(200, $responses[1]->status());
3203+
3204+
$this->expectException(StrayRequestException::class);
3205+
$this->expectExceptionMessage('Attempted request to [https://laravel.com] without a matching fake.');
3206+
3207+
$this->factory->pool(function (Pool $pool) {
3208+
return [
3209+
$pool->get('https://laravel.com'),
3210+
];
3211+
});
3212+
}
3213+
31873214
public function testPreventingStrayRequests()
31883215
{
31893216
$this->assertFalse($this->factory->preventingStrayRequests());

0 commit comments

Comments
 (0)