From e1f1c8b859a466b6f9b721bd74fc421a274a9764 Mon Sep 17 00:00:00 2001 From: "J. King" Date: Wed, 29 Nov 2017 13:41:26 -0500 Subject: [PATCH] Refactoring --- .../Exception404.php} | 4 +-- lib/REST/{ => NextCloudNews}/Exception405.php | 2 +- lib/REST/NextCloudNews/V1_2.php | 31 +++++++++---------- lib/REST/NextCloudNews/Versions.php | 2 +- tests/REST/NextCloudNews/TestNCNV1_2.php | 8 ++--- .../NextCloudNews/TestNCNVersionDiscovery.php | 2 +- 6 files changed, 23 insertions(+), 26 deletions(-) rename lib/REST/{Exception501.php => NextCloudNews/Exception404.php} (63%) rename lib/REST/{ => NextCloudNews}/Exception405.php (80%) diff --git a/lib/REST/Exception501.php b/lib/REST/NextCloudNews/Exception404.php similarity index 63% rename from lib/REST/Exception501.php rename to lib/REST/NextCloudNews/Exception404.php index 77d1e30..325a4f5 100644 --- a/lib/REST/Exception501.php +++ b/lib/REST/NextCloudNews/Exception404.php @@ -4,7 +4,7 @@ * See LICENSE and AUTHORS files for details */ declare(strict_types=1); -namespace JKingWeb\Arsse\REST; +namespace JKingWeb\Arsse\REST\NextCloudNews; -class Exception501 extends \Exception { +class Exception404 extends \Exception { } diff --git a/lib/REST/Exception405.php b/lib/REST/NextCloudNews/Exception405.php similarity index 80% rename from lib/REST/Exception405.php rename to lib/REST/NextCloudNews/Exception405.php index 842ccdb..b41c0d5 100644 --- a/lib/REST/Exception405.php +++ b/lib/REST/NextCloudNews/Exception405.php @@ -4,7 +4,7 @@ * See LICENSE and AUTHORS files for details */ declare(strict_types=1); -namespace JKingWeb\Arsse\REST; +namespace JKingWeb\Arsse\REST\NextCloudNews; class Exception405 extends \Exception { } diff --git a/lib/REST/NextCloudNews/V1_2.php b/lib/REST/NextCloudNews/V1_2.php index 8bbb26a..a60129d 100644 --- a/lib/REST/NextCloudNews/V1_2.php +++ b/lib/REST/NextCloudNews/V1_2.php @@ -15,8 +15,6 @@ use JKingWeb\Arsse\AbstractException; use JKingWeb\Arsse\Db\ExceptionInput; use JKingWeb\Arsse\Feed\Exception as FeedException; use JKingWeb\Arsse\REST\Response; -use JKingWeb\Arsse\REST\Exception501; -use JKingWeb\Arsse\REST\Exception405; class V1_2 extends \JKingWeb\Arsse\REST\AbstractHandler { const REALM = "NextCloud News API v1-2"; @@ -97,8 +95,8 @@ class V1_2 extends \JKingWeb\Arsse\REST\AbstractHandler { // check to make sure the requested function is implemented try { $func = $this->chooseCall($req->paths, $req->method); - } catch (Exception501 $e) { - return new Response(501); + } catch (Exception404 $e) { + return new Response(404); } catch (Exception405 $e) { return new Response(405, "", "", ["Allow: ".$e->getMessage()]); } @@ -136,20 +134,19 @@ class V1_2 extends \JKingWeb\Arsse\REST\AbstractHandler { $method = strtoupper($method); // we now evaluate the supplied URL against every supported path for the selected scope // the URL is evaluated as an array so as to avoid decoded escapes turning invalid URLs into valid ones - foreach ($this->paths as $path => $funcs) { - if ($path===$url) { - // if the path matches, make sure the method is allowed - if (array_key_exists($method, $funcs)) { - // if it is allowed, return the object method to run - return $funcs[$method]; - } else { - // otherwise return 405 - throw new Exception405(implode(", ", array_keys($funcs))); - } - } + if (isset($this->paths[$url])) { + // if the path is supported, make sure the method is allowed + if (isset($this->paths[$url][$method])) { + // if it is allowed, return the object method to run + return $this->paths[$url][$method]; + } else { + // otherwise return 405 + throw new Exception405(implode(", ", array_keys($this->paths[$url]))); + } + } else { + // if the path is not supported, return 501 + throw new Exception404(); } - // if the path was not found, return 501 - throw new Exception501(); } protected function feedTranslate(array $feed): array { diff --git a/lib/REST/NextCloudNews/Versions.php b/lib/REST/NextCloudNews/Versions.php index 720c298..556d765 100644 --- a/lib/REST/NextCloudNews/Versions.php +++ b/lib/REST/NextCloudNews/Versions.php @@ -27,7 +27,7 @@ class Versions implements \JKingWeb\Arsse\REST\Handler { return new Response(200, $out); } else { // if the URL path was anything else, the client is probably trying a version we don't support - return new Response(501); + return new Response(404); } } } diff --git a/tests/REST/NextCloudNews/TestNCNV1_2.php b/tests/REST/NextCloudNews/TestNCNV1_2.php index a119e65..7f8b47a 100644 --- a/tests/REST/NextCloudNews/TestNCNV1_2.php +++ b/tests/REST/NextCloudNews/TestNCNV1_2.php @@ -313,7 +313,7 @@ class TestNCNV1_2 extends Test\AbstractTest { public function testRespondToInvalidPaths() { $errs = [ - 501 => [ + 404 => [ ['GET', "/"], ['PUT', "/"], ['POST', "/"], @@ -343,10 +343,10 @@ class TestNCNV1_2 extends Test\AbstractTest { ], ], ]; - foreach ($errs[501] as $req) { - $exp = new Response(501); + foreach ($errs[404] as $req) { + $exp = new Response(404); list($method, $path) = $req; - $this->assertEquals($exp, $this->h->dispatch(new Request($method, $path)), "$method call to $path did not return 501."); + $this->assertEquals($exp, $this->h->dispatch(new Request($method, $path)), "$method call to $path did not return 404."); } foreach ($errs[405] as $allow => $cases) { $exp = new Response(405, "", "", ['Allow: '.$allow]); diff --git a/tests/REST/NextCloudNews/TestNCNVersionDiscovery.php b/tests/REST/NextCloudNews/TestNCNVersionDiscovery.php index b994289..ee4c9ed 100644 --- a/tests/REST/NextCloudNews/TestNCNVersionDiscovery.php +++ b/tests/REST/NextCloudNews/TestNCNVersionDiscovery.php @@ -38,7 +38,7 @@ class TestNCNVersionDiscovery extends Test\AbstractTest { } public function testUseIncorrectPath() { - $exp = new Response(501); + $exp = new Response(404); $h = new REST\NextCloudNews\Versions(); $req = new Request("GET", "/ook"); $res = $h->dispatch($req);