Browse Source

Refactoring

microsub
J. King 7 years ago
parent
commit
e1f1c8b859
  1. 4
      lib/REST/NextCloudNews/Exception404.php
  2. 2
      lib/REST/NextCloudNews/Exception405.php
  3. 31
      lib/REST/NextCloudNews/V1_2.php
  4. 2
      lib/REST/NextCloudNews/Versions.php
  5. 8
      tests/REST/NextCloudNews/TestNCNV1_2.php
  6. 2
      tests/REST/NextCloudNews/TestNCNVersionDiscovery.php

4
lib/REST/Exception501.php → lib/REST/NextCloudNews/Exception404.php

@ -4,7 +4,7 @@
* See LICENSE and AUTHORS files for details */ * See LICENSE and AUTHORS files for details */
declare(strict_types=1); declare(strict_types=1);
namespace JKingWeb\Arsse\REST; namespace JKingWeb\Arsse\REST\NextCloudNews;
class Exception501 extends \Exception { class Exception404 extends \Exception {
} }

2
lib/REST/Exception405.php → lib/REST/NextCloudNews/Exception405.php

@ -4,7 +4,7 @@
* See LICENSE and AUTHORS files for details */ * See LICENSE and AUTHORS files for details */
declare(strict_types=1); declare(strict_types=1);
namespace JKingWeb\Arsse\REST; namespace JKingWeb\Arsse\REST\NextCloudNews;
class Exception405 extends \Exception { class Exception405 extends \Exception {
} }

31
lib/REST/NextCloudNews/V1_2.php

@ -15,8 +15,6 @@ use JKingWeb\Arsse\AbstractException;
use JKingWeb\Arsse\Db\ExceptionInput; use JKingWeb\Arsse\Db\ExceptionInput;
use JKingWeb\Arsse\Feed\Exception as FeedException; use JKingWeb\Arsse\Feed\Exception as FeedException;
use JKingWeb\Arsse\REST\Response; use JKingWeb\Arsse\REST\Response;
use JKingWeb\Arsse\REST\Exception501;
use JKingWeb\Arsse\REST\Exception405;
class V1_2 extends \JKingWeb\Arsse\REST\AbstractHandler { class V1_2 extends \JKingWeb\Arsse\REST\AbstractHandler {
const REALM = "NextCloud News API v1-2"; 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 // check to make sure the requested function is implemented
try { try {
$func = $this->chooseCall($req->paths, $req->method); $func = $this->chooseCall($req->paths, $req->method);
} catch (Exception501 $e) { } catch (Exception404 $e) {
return new Response(501); return new Response(404);
} catch (Exception405 $e) { } catch (Exception405 $e) {
return new Response(405, "", "", ["Allow: ".$e->getMessage()]); return new Response(405, "", "", ["Allow: ".$e->getMessage()]);
} }
@ -136,20 +134,19 @@ class V1_2 extends \JKingWeb\Arsse\REST\AbstractHandler {
$method = strtoupper($method); $method = strtoupper($method);
// we now evaluate the supplied URL against every supported path for the selected scope // 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 // 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 (isset($this->paths[$url])) {
if ($path===$url) { // if the path is supported, make sure the method is allowed
// if the path matches, make sure the method is allowed if (isset($this->paths[$url][$method])) {
if (array_key_exists($method, $funcs)) { // if it is allowed, return the object method to run
// if it is allowed, return the object method to run return $this->paths[$url][$method];
return $funcs[$method]; } else {
} else { // otherwise return 405
// otherwise return 405 throw new Exception405(implode(", ", array_keys($this->paths[$url])));
throw new Exception405(implode(", ", array_keys($funcs))); }
} } 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 { protected function feedTranslate(array $feed): array {

2
lib/REST/NextCloudNews/Versions.php

@ -27,7 +27,7 @@ class Versions implements \JKingWeb\Arsse\REST\Handler {
return new Response(200, $out); return new Response(200, $out);
} else { } else {
// if the URL path was anything else, the client is probably trying a version we don't support // 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);
} }
} }
} }

8
tests/REST/NextCloudNews/TestNCNV1_2.php

@ -313,7 +313,7 @@ class TestNCNV1_2 extends Test\AbstractTest {
public function testRespondToInvalidPaths() { public function testRespondToInvalidPaths() {
$errs = [ $errs = [
501 => [ 404 => [
['GET', "/"], ['GET', "/"],
['PUT', "/"], ['PUT', "/"],
['POST', "/"], ['POST', "/"],
@ -343,10 +343,10 @@ class TestNCNV1_2 extends Test\AbstractTest {
], ],
], ],
]; ];
foreach ($errs[501] as $req) { foreach ($errs[404] as $req) {
$exp = new Response(501); $exp = new Response(404);
list($method, $path) = $req; 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) { foreach ($errs[405] as $allow => $cases) {
$exp = new Response(405, "", "", ['Allow: '.$allow]); $exp = new Response(405, "", "", ['Allow: '.$allow]);

2
tests/REST/NextCloudNews/TestNCNVersionDiscovery.php

@ -38,7 +38,7 @@ class TestNCNVersionDiscovery extends Test\AbstractTest {
} }
public function testUseIncorrectPath() { public function testUseIncorrectPath() {
$exp = new Response(501); $exp = new Response(404);
$h = new REST\NextCloudNews\Versions(); $h = new REST\NextCloudNews\Versions();
$req = new Request("GET", "/ook"); $req = new Request("GET", "/ook");
$res = $h->dispatch($req); $res = $h->dispatch($req);

Loading…
Cancel
Save