J. King
6 years ago
14 changed files with 171 additions and 89 deletions
@ -0,0 +1,10 @@ |
|||||
|
<?php |
||||
|
/** @license MIT |
||||
|
* Copyright 2017 J. King, Dustin Wilson et al. |
||||
|
* See LICENSE and AUTHORS files for details */ |
||||
|
|
||||
|
declare(strict_types=1); |
||||
|
namespace JKingWeb\Arsse\REST; |
||||
|
|
||||
|
class Exception501 extends \Exception { |
||||
|
} |
@ -1,65 +0,0 @@ |
|||||
<?php |
|
||||
/** @license MIT |
|
||||
* Copyright 2017 J. King, Dustin Wilson et al. |
|
||||
* See LICENSE and AUTHORS files for details */ |
|
||||
|
|
||||
declare(strict_types=1); |
|
||||
namespace JKingWeb\Arsse\REST; |
|
||||
|
|
||||
use JKingWeb\Arsse\Arsse; |
|
||||
|
|
||||
class Response { |
|
||||
const T_JSON = "application/json"; |
|
||||
const T_XML = "application/xml"; |
|
||||
const T_TEXT = "text/plain"; |
|
||||
|
|
||||
public $head = false; |
|
||||
public $code; |
|
||||
public $payload; |
|
||||
public $type; |
|
||||
public $fields; |
|
||||
|
|
||||
|
|
||||
public function __construct(int $code, $payload = null, string $type = self::T_JSON, array $extraFields = []) { |
|
||||
$this->code = $code; |
|
||||
$this->payload = $payload; |
|
||||
$this->type = $type; |
|
||||
$this->fields = $extraFields; |
|
||||
} |
|
||||
|
|
||||
public function output() { |
|
||||
if (!headers_sent()) { |
|
||||
foreach ($this->fields as $field) { |
|
||||
header($field); |
|
||||
} |
|
||||
$body = ""; |
|
||||
if (!is_null($this->payload)) { |
|
||||
switch ($this->type) { |
|
||||
case self::T_JSON: |
|
||||
$body = (string) json_encode($this->payload, \JSON_PRETTY_PRINT); |
|
||||
break; |
|
||||
default: |
|
||||
$body = (string) $this->payload; |
|
||||
break; |
|
||||
} |
|
||||
} |
|
||||
if (strlen($body)) { |
|
||||
header("Content-Type: ".$this->type); |
|
||||
header("Content-Length: ".strlen($body)); |
|
||||
} elseif ($this->code==200) { |
|
||||
$this->code = 204; |
|
||||
} |
|
||||
try { |
|
||||
$statusText = Arsse::$lang->msg("HTTP.Status.".$this->code); |
|
||||
} catch (\JKingWeb\Arsse\Lang\Exception $e) { |
|
||||
$statusText = ""; |
|
||||
} |
|
||||
header("Status: ".$this->code." ".$statusText); |
|
||||
if (!$this->head) { |
|
||||
echo $body; |
|
||||
} |
|
||||
} else { |
|
||||
throw new REST\Exception("headersSent"); |
|
||||
} |
|
||||
} |
|
||||
} |
|
@ -0,0 +1,50 @@ |
|||||
|
<?php |
||||
|
/** @license MIT |
||||
|
* Copyright 2017 J. King, Dustin Wilson et al. |
||||
|
* See LICENSE and AUTHORS files for details */ |
||||
|
|
||||
|
declare(strict_types=1); |
||||
|
namespace JKingWeb\Arsse\TestCase\REST; |
||||
|
|
||||
|
use JKingWeb\Arsse\REST; |
||||
|
use JKingWeb\Arsse\REST\Exception501; |
||||
|
|
||||
|
/** @covers \JKingWeb\Arsse\REST */ |
||||
|
class TestREST extends \JKingWeb\Arsse\Test\AbstractTest { |
||||
|
|
||||
|
/** @dataProvider provideApiMatchData */ |
||||
|
public function testMatchAUrlToAnApi($apiList, string $input, array $exp) { |
||||
|
$r = new REST($apiList); |
||||
|
try { |
||||
|
$out = $r->apiMatch($input); |
||||
|
} catch (Exception501 $e) { |
||||
|
$out = []; |
||||
|
} |
||||
|
$this->assertEquals($exp, $out); |
||||
|
} |
||||
|
|
||||
|
public function provideApiMatchData() { |
||||
|
$real = null; |
||||
|
$fake = [ |
||||
|
'unstripped' => ['match' => "/full/url", 'strip' => "", 'class' => "UnstrippedProtocol"], |
||||
|
]; |
||||
|
return [ |
||||
|
[$real, "/index.php/apps/news/api/v1-2/feeds", ["ncn_v1-2", "/feeds", \JKingWeb\Arsse\REST\NextCloudNews\V1_2::class]], |
||||
|
[$real, "/index.php/apps/news/api/v1-2", ["ncn", "/v1-2", \JKingWeb\Arsse\REST\NextCloudNews\Versions::class]], |
||||
|
[$real, "/index.php/apps/news/api/", ["ncn", "/", \JKingWeb\Arsse\REST\NextCloudNews\Versions::class]], |
||||
|
[$real, "/index%2Ephp/apps/news/api/", ["ncn", "/", \JKingWeb\Arsse\REST\NextCloudNews\Versions::class]], |
||||
|
[$real, "/index.php/apps/news/", []], |
||||
|
[$real, "/index!php/apps/news/api/", []], |
||||
|
[$real, "/tt-rss/api/index.php", ["ttrss_api", "/index.php", \JKingWeb\Arsse\REST\TinyTinyRSS\API::class]], |
||||
|
[$real, "/tt-rss/api", ["ttrss_api", "", \JKingWeb\Arsse\REST\TinyTinyRSS\API::class]], |
||||
|
[$real, "/tt-rss/API", []], |
||||
|
[$real, "/tt-rss/api-bogus", []], |
||||
|
[$real, "/tt-rss/api bogus", []], |
||||
|
[$real, "/tt-rss/feed-icons/", ["ttrss_icon", "", \JKingWeb\Arsse\REST\TinyTinyRSS\Icon::class]], |
||||
|
[$real, "/tt-rss/feed-icons/", ["ttrss_icon", "", \JKingWeb\Arsse\REST\TinyTinyRSS\Icon::class]], |
||||
|
[$real, "/tt-rss/feed-icons", []], |
||||
|
[$fake, "/full/url/", ["unstripped", "/full/url/", "UnstrippedProtocol"]], |
||||
|
[$fake, "/full/url-not", []], |
||||
|
]; |
||||
|
} |
||||
|
} |
Loading…
Reference in new issue