From 651d60625fd511c91945fde1e3aaef7e2cb253d9 Mon Sep 17 00:00:00 2001 From: Ben Kuhl Date: Thu, 2 Jan 2020 12:51:01 -0500 Subject: [PATCH] Revert "Throw an AuthenticationException for 401 status codes" --- src/Exception/AuthenticationException.php | 4 +- src/Http/Handlers/AuthenticationHandler.php | 27 -------- src/Http/Handlers/ClientErrorHandler.php | 2 +- src/Http/RestClientBuilder.php | 2 - tests/ApiClientIntegrationTestCase.php | 2 - .../Handlers/AuthenticationHandlerTest.php | 20 ------ tests/Http/Handlers/ClientHandlerTest.php | 66 ------------------- 7 files changed, 2 insertions(+), 121 deletions(-) delete mode 100644 src/Http/Handlers/AuthenticationHandler.php delete mode 100644 tests/Http/Handlers/AuthenticationHandlerTest.php delete mode 100644 tests/Http/Handlers/ClientHandlerTest.php diff --git a/src/Exception/AuthenticationException.php b/src/Exception/AuthenticationException.php index 77c3824e..80b182ff 100644 --- a/src/Exception/AuthenticationException.php +++ b/src/Exception/AuthenticationException.php @@ -4,8 +4,6 @@ namespace HelpScout\Api\Exception; -use GuzzleHttp\Exception\RequestException; - -class AuthenticationException extends RequestException implements Exception +class AuthenticationException extends \Exception implements Exception { } diff --git a/src/Http/Handlers/AuthenticationHandler.php b/src/Http/Handlers/AuthenticationHandler.php deleted file mode 100644 index 729a94e8..00000000 --- a/src/Http/Handlers/AuthenticationHandler.php +++ /dev/null @@ -1,27 +0,0 @@ -then( - function (ResponseInterface $response) use ($request) { - if ($response->getStatusCode() === 401) { - throw new AuthenticationException('Invalid Credentials', $request, $response); - } - - return $response; - } - ); - }; - } -} diff --git a/src/Http/Handlers/ClientErrorHandler.php b/src/Http/Handlers/ClientErrorHandler.php index fb47ac4e..82a44f31 100644 --- a/src/Http/Handlers/ClientErrorHandler.php +++ b/src/Http/Handlers/ClientErrorHandler.php @@ -15,7 +15,7 @@ public function __invoke(callable $handler) return function (RequestInterface $request, array $options = []) use ($handler) { return $handler($request, $options)->then( function (ResponseInterface $response) use ($request) { - if ($response->getStatusCode() >= 400 && $response->getStatusCode() !== 401) { + if ($response->getStatusCode() >= 400) { $e = RequestException::create($request, $response); throw $e; diff --git a/src/Http/RestClientBuilder.php b/src/Http/RestClientBuilder.php index a3159d1d..c558cddf 100644 --- a/src/Http/RestClientBuilder.php +++ b/src/Http/RestClientBuilder.php @@ -16,7 +16,6 @@ use HelpScout\Api\Http\Auth\LegacyCredentials; use HelpScout\Api\Http\Auth\NullCredentials; use HelpScout\Api\Http\Auth\RefreshCredentials; -use HelpScout\Api\Http\Handlers\AuthenticationHandler; use HelpScout\Api\Http\Handlers\ClientErrorHandler; use HelpScout\Api\Http\Handlers\RateLimitHandler; use HelpScout\Api\Http\Handlers\ValidationHandler; @@ -96,7 +95,6 @@ protected function getHandlerStack(): HandlerStack { $handler = HandlerStack::create(); - $handler->push(new AuthenticationHandler()); $handler->push(new ClientErrorHandler()); $handler->push(new RateLimitHandler()); $handler->push(new ValidationHandler()); diff --git a/tests/ApiClientIntegrationTestCase.php b/tests/ApiClientIntegrationTestCase.php index d0ee57e4..93f3bfda 100644 --- a/tests/ApiClientIntegrationTestCase.php +++ b/tests/ApiClientIntegrationTestCase.php @@ -11,7 +11,6 @@ use GuzzleHttp\Psr7\Response; use HelpScout\Api\ApiClient; use HelpScout\Api\Http\Authenticator; -use HelpScout\Api\Http\Handlers\AuthenticationHandler; use HelpScout\Api\Http\Handlers\ClientErrorHandler; use HelpScout\Api\Http\Handlers\RateLimitHandler; use HelpScout\Api\Http\Handlers\ValidationHandler; @@ -49,7 +48,6 @@ public function setUp() $handler = HandlerStack::create($this->mockHandler); $handler->push(Middleware::history($this->history)); - $handler->push(new AuthenticationHandler()); $handler->push(new ClientErrorHandler()); $handler->push(new RateLimitHandler()); $handler->push(new ValidationHandler()); diff --git a/tests/Http/Handlers/AuthenticationHandlerTest.php b/tests/Http/Handlers/AuthenticationHandlerTest.php deleted file mode 100644 index 3a296b07..00000000 --- a/tests/Http/Handlers/AuthenticationHandlerTest.php +++ /dev/null @@ -1,20 +0,0 @@ -expectException(AuthenticationException::class); - - $this->stubResponse($this->getResponse(401, json_encode([]))); - - $this->client->customers()->get(1); - } -} diff --git a/tests/Http/Handlers/ClientHandlerTest.php b/tests/Http/Handlers/ClientHandlerTest.php deleted file mode 100644 index e7f0a95f..00000000 --- a/tests/Http/Handlers/ClientHandlerTest.php +++ /dev/null @@ -1,66 +0,0 @@ -expectException(RequestException::class); - - $this->stubResponse($this->getResponse(400, json_encode([]))); - - $this->client->customers()->get(1); - } - - public function testRequestExceptionThrownWhenAccessDenied() - { - $this->expectException(RequestException::class); - - $this->stubResponse($this->getResponse(403, json_encode([]))); - - $this->client->customers()->get(1); - } - - public function testRequestExceptionThrownWhenNotFound() - { - $this->expectException(RequestException::class); - - $this->stubResponse($this->getResponse(404, json_encode([]))); - - $this->client->customers()->get(1); - } - - public function testRequestExceptionNotThrownWhenNotAuthorized() - { - $this->mockHandler = new MockHandler(); - $handler = HandlerStack::create($this->mockHandler); - - $client = new Client(['handler' => $handler]); - $this->authenticator = new Authenticator($client); - $this->authenticator->setAccessToken('abc123'); - - $this->client = new ApiClient( - new RestClient($client, $this->authenticator) - ); - - $this->stubResponse($this->getResponse(401, json_encode([]))); - - $this->assertInstanceOf( - Customer::class, - $this->client->customers()->get(1) - ); - } -}