1
1
Fork 0
mirror of https://code.mensbeam.com/MensBeam/Arsse.git synced 2024-12-22 21:22:40 +00:00

Refactoring

This commit is contained in:
J. King 2017-11-29 13:41:26 -05:00
parent b242c70968
commit e1f1c8b859
6 changed files with 23 additions and 26 deletions

View file

@ -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 {
}

View file

@ -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 {
}

View file

@ -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,21 +134,20 @@ 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 (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 $funcs[$method];
return $this->paths[$url][$method];
} else {
// otherwise return 405
throw new Exception405(implode(", ", array_keys($funcs)));
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 {
// map fields to proper names

View file

@ -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);
}
}
}

View file

@ -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]);

View file

@ -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);