Skip to content
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
17 changes: 16 additions & 1 deletion src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ final class Config
*/
private static ?MiddlewarePipeline $globalMiddlewarePipeline = null;

/**
* Whether stray requests should be prevented
*/
private static bool $preventStrayRequests = false;

/**
* Write a custom sender resolver
*/
Expand Down Expand Up @@ -86,10 +91,20 @@ public static function clearGlobalMiddleware(): void
*/
public static function preventStrayRequests(): void
{
self::$preventStrayRequests = true;

self::globalMiddleware()->onRequest(static function (PendingRequest $pendingRequest) {
if (! $pendingRequest->hasMockClient()) {
if (self::$preventStrayRequests && ! $pendingRequest->hasMockClient()) {
throw new StrayRequestException;
}
}, order: PipeOrder::LAST);
}

/**
* Allow stray requests without a MockClient.
*/
public static function allowStrayRequests(): void
{
self::$preventStrayRequests = false;
}
}
2 changes: 1 addition & 1 deletion src/Http/BaseResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class BaseResource
/**
* Constructor
*/
public function __construct(readonly protected Connector $connector)
public function __construct(protected readonly Connector $connector)
{
//
}
Expand Down
8 changes: 4 additions & 4 deletions tests/Fixtures/Authenticators/CustomOAuthAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ class CustomOAuthAuthenticator extends AccessTokenAuthenticator
* Constructor
*/
public function __construct(
readonly public string $accessToken,
readonly public string $greeting,
readonly public ?string $refreshToken = null,
readonly public ?DateTimeImmutable $expiresAt = null,
public readonly string $accessToken,
public readonly string $greeting,
public readonly ?string $refreshToken = null,
public readonly ?DateTimeImmutable $expiresAt = null,
) {
//
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Fixtures/Requests/QueryParameterRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class QueryParameterRequest extends Request
/**
* Constructor
*/
public function __construct(readonly public string $endpoint = '/user')
public function __construct(public readonly string $endpoint = '/user')
{
//
}
Expand Down
16 changes: 16 additions & 0 deletions tests/Unit/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,19 @@

Config::clearGlobalMiddleware();
});

test('you can prevent and then allow stray api requests', function () {
Config::preventStrayRequests();

try {
TestConnector::make()->send(new UserRequest);
} catch (StrayRequestException $e) {
expect($e)->toBeInstanceOf(StrayRequestException::class);
}

Config::allowStrayRequests();

TestConnector::make()->send(new UserRequest);

Config::clearGlobalMiddleware();
});
Loading