mirror of
https://code.mensbeam.com/MensBeam/Arsse.git
synced 2024-12-23 06:34:55 +00:00
Complete tests for NCN version list
- Fixes #47 - Implemented an AbstractHandler class with a generic URL parser
This commit is contained in:
parent
25d9158171
commit
8b50297e6d
3 changed files with 65 additions and 9 deletions
33
lib/REST/AbstractHandler.php
Normal file
33
lib/REST/AbstractHandler.php
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
namespace JKingWeb\NewsSync\REST;
|
||||||
|
|
||||||
|
abstract class AbstractHandler implements Handler {
|
||||||
|
abstract function __construct(\JKingWeb\NewsSync\RuntimeData $data);
|
||||||
|
abstract function dispatch(Request $req): Response;
|
||||||
|
|
||||||
|
protected function parseURL(string $url): array {
|
||||||
|
// split the query string from the path
|
||||||
|
$parts = explode("?", $url);
|
||||||
|
$out = ['path' => $parts[0], 'query' => []];
|
||||||
|
// if there is a query string, parse it
|
||||||
|
if(isset($parts[1])) {
|
||||||
|
// split along & to get key-value pairs
|
||||||
|
$query = explode("&", $parts[1]);
|
||||||
|
for($a = 0; $a < sizeof($query); $a++) {
|
||||||
|
// split each pair, into no more than two parts
|
||||||
|
$data = explode("=", $query[$a], 2);
|
||||||
|
// decode the key
|
||||||
|
$key = rawurldecode($data[0]);
|
||||||
|
// decode the value if there is one
|
||||||
|
$value = "";
|
||||||
|
if(isset($data[1])) {
|
||||||
|
$value = rawurldecode($data[1]);
|
||||||
|
}
|
||||||
|
// add the pair to the query output, overwriting earlier values for the same key, is present
|
||||||
|
$out['query'][$key] = $value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $out;
|
||||||
|
}
|
||||||
|
}
|
|
@ -3,27 +3,28 @@ declare(strict_types=1);
|
||||||
namespace JKingWeb\NewsSync\REST\NextCloudNews;
|
namespace JKingWeb\NewsSync\REST\NextCloudNews;
|
||||||
use JKingWeb\NewsSync\REST\Response;
|
use JKingWeb\NewsSync\REST\Response;
|
||||||
|
|
||||||
class Versions implements \JKingWeb\NewsSync\REST\Handler {
|
class Versions extends \JKingWeb\NewsSync\REST\AbstractHandler {
|
||||||
function __construct(\JKingWeb\NewsSync\RuntimeData $data) {
|
function __construct(\JKingWeb\NewsSync\RuntimeData $data) {
|
||||||
|
// runtime data is not needed; this method is deliberately empty
|
||||||
}
|
}
|
||||||
|
|
||||||
function dispatch(\JKingWeb\NewsSync\REST\Request $req): \JKingWeb\NewsSync\REST\Response {
|
function dispatch(\JKingWeb\NewsSync\REST\Request $req): \JKingWeb\NewsSync\REST\Response {
|
||||||
$path = $req->url;
|
// parse the URL and populate $path and $query
|
||||||
$query = "";
|
extract($this->parseURL($req->url));
|
||||||
if(strpos($path, "?") !== false) {
|
// if a method other than GET was used, this is an error
|
||||||
list($path, $query) = explode("?", $path);
|
|
||||||
}
|
|
||||||
if($req->method != "GET") {
|
if($req->method != "GET") {
|
||||||
return new Response(405);
|
return new Response(405);
|
||||||
}
|
}
|
||||||
if(preg_match("<^/?$>",$path)) {
|
if(preg_match("<^/?$>",$path)) {
|
||||||
|
// if the request path is an empty string or just a slash, return the supported versions
|
||||||
$out = [
|
$out = [
|
||||||
'apiLevels' => [
|
'apiLevels' => [
|
||||||
'v1-2'
|
'v1-2',
|
||||||
]
|
]
|
||||||
];
|
];
|
||||||
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
|
||||||
return new Response(404);
|
return new Response(404);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,10 +13,32 @@ class TestNCNVersionDiscovery extends \PHPUnit\Framework\TestCase {
|
||||||
$this->data = new Test\RuntimeData($conf);
|
$this->data = new Test\RuntimeData($conf);
|
||||||
}
|
}
|
||||||
|
|
||||||
function testVersionList() {
|
function testFetchVersionList() {
|
||||||
$exp = new Response(200, ['apiLevels' => ['v1-2']]);
|
$exp = new Response(200, ['apiLevels' => ['v1-2']]);
|
||||||
$req = new Request("GET", "/");
|
|
||||||
$h = new Rest\NextCloudNews\Versions($this->data);
|
$h = new Rest\NextCloudNews\Versions($this->data);
|
||||||
|
$req = new Request("GET", "/");
|
||||||
|
$res = $h->dispatch($req);
|
||||||
|
$this->assertEquals($exp, $res);
|
||||||
|
$req = new Request("GET", "");
|
||||||
|
$res = $h->dispatch($req);
|
||||||
|
$this->assertEquals($exp, $res);
|
||||||
|
$req = new Request("GET", "/?id=1827");
|
||||||
|
$res = $h->dispatch($req);
|
||||||
|
$this->assertEquals($exp, $res);
|
||||||
|
}
|
||||||
|
|
||||||
|
function testUseIncorrectMethod() {
|
||||||
|
$exp = new Response(405);
|
||||||
|
$h = new Rest\NextCloudNews\Versions($this->data);
|
||||||
|
$req = new Request("POST", "/");
|
||||||
|
$res = $h->dispatch($req);
|
||||||
|
$this->assertEquals($exp, $res);
|
||||||
|
}
|
||||||
|
|
||||||
|
function testUseIncorrectPath() {
|
||||||
|
$exp = new Response(404);
|
||||||
|
$h = new Rest\NextCloudNews\Versions($this->data);
|
||||||
|
$req = new Request("GET", "/ook");
|
||||||
$res = $h->dispatch($req);
|
$res = $h->dispatch($req);
|
||||||
$this->assertEquals($exp, $res);
|
$this->assertEquals($exp, $res);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue