Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions src/Illuminate/Http/Client/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@ class Factory
*/
protected $preventStrayRequests = false;

/**
* A list of URL patterns that are allowed to bypass the stray request guard.
*
* @var array<int, string>
*/
protected $allowedStrayRequestUrls = [];

/**
* Create a new factory instance.
*
Expand Down Expand Up @@ -333,13 +340,22 @@ public function preventingStrayRequests()
}

/**
* Indicate that an exception should not be thrown if any request is not faked.
* Allow stray, unfaked requests entirely, or optionally allow only specific URLs.
*
* @param array<int, string>|null $only
* @return $this
*/
public function allowStrayRequests()
public function allowStrayRequests(?array $only = null)
{
return $this->preventStrayRequests(false);
if (is_null($only)) {
$this->preventStrayRequests(false);

$this->allowedStrayRequestUrls = [];
} else {
$this->allowedStrayRequestUrls = array_values($only);
}

return $this;
}

/**
Expand Down Expand Up @@ -486,7 +502,10 @@ public function recorded($callback = null)
public function createPendingRequest()
{
return tap($this->newPendingRequest(), function ($request) {
$request->stub($this->stubCallbacks)->preventStrayRequests($this->preventStrayRequests);
$request
->stub($this->stubCallbacks)
->preventStrayRequests($this->preventStrayRequests)
->allowStrayRequests($this->allowedStrayRequestUrls);
});
}

Expand Down
43 changes: 42 additions & 1 deletion src/Illuminate/Http/Client/PendingRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,13 @@ class PendingRequest
*/
protected $preventStrayRequests = false;

/**
* A list of URL patterns that are allowed to bypass the stray request guard.
*
* @var array<int, string>
*/
protected $allowedStrayRequestUrls = [];

/**
* The middleware callables added by users that will handle requests.
*
Expand Down Expand Up @@ -1376,7 +1383,7 @@ public function buildStubHandler()
->first();

if (is_null($response)) {
if ($this->preventStrayRequests) {
if (! $this->isAllowedRequestUrl((string) $request->getUri())) {
throw new StrayRequestException((string) $request->getUri());
}

Expand Down Expand Up @@ -1501,6 +1508,40 @@ public function preventStrayRequests($prevent = true)
return $this;
}

/**
* Allow stray, unfaked requests entirely, or optionally allow only specific URLs.
*
* @param array<int, string> $only
* @return $this
*/
public function allowStrayRequests(array $only)
{
$this->allowedStrayRequestUrls = array_values($only);

return $this;
}

/**
* Determine if the given URL is allowed as a stray request.
*
* @param string $url
* @return bool
*/
public function isAllowedRequestUrl($url)
{
if (! $this->preventStrayRequests) {
return true;
}

foreach ($this->allowedStrayRequestUrls as $pattern) {
if (Str::is($pattern, $url)) {
return true;
}
}

return false;
}

/**
* Toggle asynchronicity in requests.
*
Expand Down
15 changes: 15 additions & 0 deletions tests/Http/HttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3464,6 +3464,21 @@ public function testPreventingStrayRequests()
$this->assertTrue($this->factory->preventingStrayRequests());
}

public function testAllowingStrayRequestUrls()
{
$this->assertFalse($this->factory->preventingStrayRequests());
$this->assertTrue($this->factory->isAllowedRequestUrl('127.0.0.1'));

$this->factory->preventStrayRequests();
$this->assertFalse($this->factory->isAllowedRequestUrl('127.0.0.1'));
$this->factory->allowStrayRequests([
'127.0.0.1',
]);

$this->assertTrue($this->factory->preventingStrayRequests());
$this->assertTrue($this->factory->isAllowedRequestUrl('127.0.0.1'));
}

public function testItCanAddAuthorizationHeaderIntoRequestUsingBeforeSendingCallback()
{
$this->factory->fake();
Expand Down
7 changes: 7 additions & 0 deletions tests/Support/SupportFacadesHttpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ public function testFacadeRootIsSharedWhenEnforcingFaking(): void
$this->assertSame($client, $this->app->make(Factory::class));
}

public function testFacadeRootIsSharedWhenEnforcingFakingWithAllowedUrls(): void
{
$client = Http::preventStrayRequests()->allowStrayRequests(['127.0.0.1']);

$this->assertSame($client, $this->app->make(Factory::class));
}

public function test_can_set_prevents_to_prevents_stray_requests(): void
{
Http::preventStrayRequests(true);
Expand Down