File tree Expand file tree Collapse file tree 5 files changed +38
-7
lines changed
Expand file tree Collapse file tree 5 files changed +38
-7
lines changed Original file line number Diff line number Diff line change @@ -47,6 +47,11 @@ final class Config
4747 */
4848 private static ?MiddlewarePipeline $ globalMiddlewarePipeline = null ;
4949
50+ /**
51+ * Whether stray requests should be prevented
52+ */
53+ private static bool $ preventStrayRequests = false ;
54+
5055 /**
5156 * Write a custom sender resolver
5257 */
@@ -86,10 +91,20 @@ public static function clearGlobalMiddleware(): void
8691 */
8792 public static function preventStrayRequests (): void
8893 {
94+ self ::$ preventStrayRequests = true ;
95+
8996 self ::globalMiddleware ()->onRequest (static function (PendingRequest $ pendingRequest ) {
90- if (! $ pendingRequest ->hasMockClient ()) {
97+ if (self :: $ preventStrayRequests && ! $ pendingRequest ->hasMockClient ()) {
9198 throw new StrayRequestException ;
9299 }
93100 }, order: PipeOrder::LAST );
94101 }
102+
103+ /**
104+ * Allow stray requests without a MockClient.
105+ */
106+ public static function allowStrayRequests (): void
107+ {
108+ self ::$ preventStrayRequests = false ;
109+ }
95110}
Original file line number Diff line number Diff line change @@ -9,7 +9,7 @@ class BaseResource
99 /**
1010 * Constructor
1111 */
12- public function __construct (readonly protected Connector $ connector )
12+ public function __construct (protected readonly Connector $ connector )
1313 {
1414 //
1515 }
Original file line number Diff line number Diff line change @@ -13,10 +13,10 @@ class CustomOAuthAuthenticator extends AccessTokenAuthenticator
1313 * Constructor
1414 */
1515 public function __construct (
16- readonly public string $ accessToken ,
17- readonly public string $ greeting ,
18- readonly public ?string $ refreshToken = null ,
19- readonly public ?DateTimeImmutable $ expiresAt = null ,
16+ public readonly string $ accessToken ,
17+ public readonly string $ greeting ,
18+ public readonly ?string $ refreshToken = null ,
19+ public readonly ?DateTimeImmutable $ expiresAt = null ,
2020 ) {
2121 //
2222 }
Original file line number Diff line number Diff line change @@ -25,7 +25,7 @@ class QueryParameterRequest extends Request
2525 /**
2626 * Constructor
2727 */
28- public function __construct (readonly public string $ endpoint = '/user ' )
28+ public function __construct (public readonly string $ endpoint = '/user ' )
2929 {
3030 //
3131 }
Original file line number Diff line number Diff line change 8484
8585 Config::clearGlobalMiddleware ();
8686});
87+
88+ test ('you can prevent and then allow stray api requests ' , function () {
89+ Config::preventStrayRequests ();
90+
91+ try {
92+ TestConnector::make ()->send (new UserRequest );
93+ } catch (StrayRequestException $ e ) {
94+ expect ($ e )->toBeInstanceOf (StrayRequestException::class);
95+ }
96+
97+ Config::allowStrayRequests ();
98+
99+ TestConnector::make ()->send (new UserRequest );
100+
101+ Config::clearGlobalMiddleware ();
102+ });
You can’t perform that action at this time.
0 commit comments