From 46a2cfbace5093e94a9247aa65a5cddee5ce348a Mon Sep 17 00:00:00 2001 From: Sammyjo20 <29132017+Sammyjo20@users.noreply.github.com> Date: Sun, 3 Sep 2023 22:43:54 +0100 Subject: [PATCH 1/3] Feature | V2 Prevent Stray Requests --- src/Exceptions/StrayRequestException.php | 11 +++++++++++ src/Helpers/Config.php | 17 +++++++++++++++++ tests/Unit/ConfigTest.php | 13 +++++++++++++ 3 files changed, 41 insertions(+) create mode 100644 src/Exceptions/StrayRequestException.php diff --git a/src/Exceptions/StrayRequestException.php b/src/Exceptions/StrayRequestException.php new file mode 100644 index 00000000..3eff46e1 --- /dev/null +++ b/src/Exceptions/StrayRequestException.php @@ -0,0 +1,11 @@ +onRequest(static function (PendingRequest $pendingRequest) { + if (! $pendingRequest->hasMockClient()) { + throw new StrayRequestException; + } + }); + } } diff --git a/tests/Unit/ConfigTest.php b/tests/Unit/ConfigTest.php index ad2b3c13..7cfa68f8 100644 --- a/tests/Unit/ConfigTest.php +++ b/tests/Unit/ConfigTest.php @@ -2,6 +2,8 @@ declare(strict_types=1); +use Saloon\Exceptions\SaloonException; +use Saloon\Exceptions\StrayRequestException; use Saloon\Http\Response; use Saloon\Helpers\Config; use Saloon\Http\PendingRequest; @@ -68,3 +70,14 @@ expect($sender)->toBeInstanceOf(GuzzleSender::class); }); + +test('you can prevent stray api requests', function () { + Config::preventStrayRequests(); + + $this->expectException(StrayRequestException::class); + $this->expectExceptionMessage('Attempted to make a real API request! Make sure to use a MockClient or Saloon::fake() if you are using Laravel.'); + + TestConnector::make()->send(new UserRequest); + + Config::resetMiddleware(); +}); From 27ac64a4883e4a230298c20bbb5198fb2e623059 Mon Sep 17 00:00:00 2001 From: Sammyjo20 Date: Sun, 3 Sep 2023 21:44:59 +0000 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=AA=84=20Code=20Style=20Fixes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Exceptions/StrayRequestException.php | 2 ++ src/Helpers/Config.php | 5 ++--- tests/Unit/ConfigTest.php | 3 +-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Exceptions/StrayRequestException.php b/src/Exceptions/StrayRequestException.php index 3eff46e1..950300c3 100644 --- a/src/Exceptions/StrayRequestException.php +++ b/src/Exceptions/StrayRequestException.php @@ -1,5 +1,7 @@ Date: Sun, 3 Sep 2023 22:45:40 +0100 Subject: [PATCH 3/3] Updated error message --- src/Exceptions/StrayRequestException.php | 2 +- tests/Unit/ConfigTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Exceptions/StrayRequestException.php b/src/Exceptions/StrayRequestException.php index 950300c3..9fafdf63 100644 --- a/src/Exceptions/StrayRequestException.php +++ b/src/Exceptions/StrayRequestException.php @@ -8,6 +8,6 @@ class StrayRequestException extends SaloonException { public function __construct() { - parent::__construct('Attempted to make a real API request! Make sure to use a MockClient or Saloon::fake() if you are using Laravel.'); + parent::__construct('Attempted to make a real API request! Make sure to use a mock response or fixture.'); } } diff --git a/tests/Unit/ConfigTest.php b/tests/Unit/ConfigTest.php index e08f3295..0a6af4ac 100644 --- a/tests/Unit/ConfigTest.php +++ b/tests/Unit/ConfigTest.php @@ -74,7 +74,7 @@ Config::preventStrayRequests(); $this->expectException(StrayRequestException::class); - $this->expectExceptionMessage('Attempted to make a real API request! Make sure to use a MockClient or Saloon::fake() if you are using Laravel.'); + $this->expectExceptionMessage('Attempted to make a real API request! Make sure to use a mock response or fixture.'); TestConnector::make()->send(new UserRequest);