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

More Guzzle conversion

This commit is contained in:
J. King 2022-08-06 13:40:02 -04:00
parent 64ec3f6ae4
commit 56f015bfb9
5 changed files with 27 additions and 29 deletions

View file

@ -14,7 +14,6 @@ use JKingWeb\Arsse\Misc\HTTP;
use JKingWeb\Arsse\Db\ExceptionInput;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use Laminas\Diactoros\Response\XmlResponse;
class API extends \JKingWeb\Arsse\REST\AbstractHandler {
public const LEVEL = 3;
@ -180,7 +179,7 @@ class API extends \JKingWeb\Arsse\REST\AbstractHandler {
if ($xml) {
$d = new \DOMDocument("1.0", "utf-8");
$d->appendChild($this->makeXMLAssoc($data, $d->createElement("response")));
return new XmlResponse($d->saveXML());
return HTTP::respXml($d->saveXML());
} else {
return HTTP::respJson($data, 200, [], \JSON_UNESCAPED_SLASHES | \JSON_UNESCAPED_UNICODE);
}

View file

@ -9,7 +9,6 @@ namespace JKingWeb\Arsse\REST\Miniflux;
use JKingWeb\Arsse\Misc\HTTP;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use Laminas\Diactoros\Response\TextResponse;
class Status extends \JKingWeb\Arsse\REST\AbstractHandler {
public function __construct() {
@ -32,6 +31,6 @@ class Status extends \JKingWeb\Arsse\REST\AbstractHandler {
} elseif ($target === "/healthcheck") {
$out = "OK";
}
return new TextResponse($out);
return HTTP::respText($out);
}
}

View file

@ -27,8 +27,7 @@ use JKingWeb\Arsse\User\ExceptionConflict;
use JKingWeb\Arsse\User\Exception as UserException;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use Laminas\Diactoros\Response\TextResponse as GenericResponse;
use Laminas\Diactoros\Uri;
use GuzzleHttp\Psr7\Uri;
class V1 extends \JKingWeb\Arsse\REST\AbstractHandler {
public const VERSION = "2.0.28";
@ -1203,6 +1202,6 @@ class V1 extends \JKingWeb\Arsse\REST\AbstractHandler {
}
protected function opmlExport(): ResponseInterface {
return new GenericResponse(Arsse::$obj->get(OPML::class)->export(Arsse::$user->id), 200, ['Content-Type' => "application/xml"]);
return HTTP::respText(Arsse::$obj->get(OPML::class)->export(Arsse::$user->id), 200, ['Content-Type' => "application/xml"]);
}
}

View file

@ -7,14 +7,16 @@ declare(strict_types=1);
namespace JKingWeb\Arsse\TestCase\Misc;
use JKingWeb\Arsse\Misc\HTTP;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
/** @covers \JKingWeb\Arsse\Misc\HTTP */
class TestHTTP extends \JKingWeb\Arsse\Test\AbstractTest {
/** @dataProvider provideMediaTypes */
public function testMatchMediaType(string $header, array $types, bool $exp): void {
$msg = (new \Laminas\Diactoros\Request)->withHeader("Content-Type", $header);
$msg = (new Request("POST", "/"))->withHeader("Content-Type", $header);
$this->assertSame($exp, HTTP::matchType($msg, ...$types));
$msg = (new \Laminas\Diactoros\Response)->withHeader("Content-Type", $header);
$msg = (new Response)->withHeader("Content-Type", $header);
$this->assertSame($exp, HTTP::matchType($msg, ...$types));
}

View file

@ -15,10 +15,9 @@ use JKingWeb\Arsse\REST\TinyTinyRSS\API as TTRSS;
use JKingWeb\Arsse\Misc\HTTP;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Laminas\Diactoros\Request;
use Laminas\Diactoros\Response;
use Laminas\Diactoros\ServerRequest;
use Laminas\Diactoros\Response\TextResponse;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\ServerRequest;
/** @covers \JKingWeb\Arsse\REST */
class TestREST extends \JKingWeb\Arsse\Test\AbstractTest {
@ -70,7 +69,7 @@ class TestREST extends \JKingWeb\Arsse\Test\AbstractTest {
$this->userMock->auth->with("someone.else@example.com", "")->returns(true);
Arsse::$user = $this->userMock->get();
// create an input server request
$req = new ServerRequest($serverParams);
$req = new ServerRequest("GET", "/", [], null, "1.1", $serverParams);
// create the expected output
$exp = $req;
foreach ($expAttr as $key => $value) {
@ -156,7 +155,7 @@ class TestREST extends \JKingWeb\Arsse\Test\AbstractTest {
return $origin;
});
$headers = isset($origin) ? ['Origin' => $origin] : [];
$req = new Request("", "GET", "php://memory", $headers);
$req = new Request("GET", "", $headers);
$act = $rMock->get()->corsNegotiate($req, $allowed, $denied);
$this->assertSame($exp, $act);
}
@ -189,7 +188,7 @@ class TestREST extends \JKingWeb\Arsse\Test\AbstractTest {
/** @dataProvider provideCorsHeaders */
public function testAddCorsHeaders(string $reqMethod, array $reqHeaders, array $resHeaders, array $expHeaders): void {
$r = new REST();
$req = new Request("", $reqMethod, "php://memory", $reqHeaders);
$req = new Request($reqMethod, "php://memory", $reqHeaders);
$res = HTTP::respEmpty(204, $resHeaders);
$exp = HTTP::respEmpty(204, $expHeaders);
$act = $r->corsApply($res, $req);
@ -277,12 +276,12 @@ class TestREST extends \JKingWeb\Arsse\Test\AbstractTest {
[HTTP::respEmpty(204, ['Allow' => ["PUT, DELETE", "OPTIONS"]]), HTTP::respEmpty(204, ['Allow' => "PUT, DELETE, OPTIONS"])],
[HTTP::respEmpty(204, ['Allow' => "HEAD,GET"]), HTTP::respEmpty(204, ['Allow' => "HEAD, GET, OPTIONS"])],
[HTTP::respEmpty(204, ['Allow' => "GET"]), HTTP::respEmpty(204, ['Allow' => "GET, HEAD, OPTIONS"])],
[new TextResponse("ook", 200), new TextResponse("ook", 200, ['Content-Length' => "3"])],
[new TextResponse("", 200), new TextResponse("", 200, ['Content-Length' => "0"])],
[new TextResponse("ook", 404), new TextResponse("ook", 404, ['Content-Length' => "3"])],
[new TextResponse("", 404), new TextResponse("", 404)],
[new Response($stream, 200), new Response($stream, 200, ['Content-Length' => "3"]), new Request("", "GET")],
[new Response($stream, 200), HTTP::respEmpty(200, ['Content-Length' => "3"]), new Request("", "HEAD")],
[HTTP::respText("ook", 200), HTTP::respText("ook", 200, ['Content-Length' => "3"])],
[HTTP::respText("", 200), HTTP::respText("", 200, ['Content-Length' => "0"])],
[HTTP::respText("ook", 404), HTTP::respText("ook", 404, ['Content-Length' => "3"])],
[HTTP::respText("", 404), HTTP::respText("", 404)],
[new Response(200, [], $stream), new Response(200, ['Content-Length' => "3"], $stream), new Request("", "GET")],
[new Response(200, [], $stream), HTTP::respEmpty(200, ['Content-Length' => "3"]), new Request("", "HEAD")],
];
}
@ -318,13 +317,13 @@ class TestREST extends \JKingWeb\Arsse\Test\AbstractTest {
public function provideMockRequests(): iterable {
return [
[new ServerRequest([], [], "/index.php/apps/news/api/v1-2/feeds", "GET"), "GET", true, NCN::class, "/feeds"],
[new ServerRequest([], [], "/index.php/apps/news/api/v1-2/feeds", "HEAD"), "GET", true, NCN::class, "/feeds"],
[new ServerRequest([], [], "/index.php/apps/news/api/v1-2/feeds", "get"), "GET", true, NCN::class, "/feeds"],
[new ServerRequest([], [], "/index.php/apps/news/api/v1-2/feeds", "head"), "GET", true, NCN::class, "/feeds"],
[new ServerRequest([], [], "/tt-rss/api/", "POST"), "POST", true, TTRSS::class, "/"],
[new ServerRequest([], [], "/no/such/api/", "HEAD"), "GET", false],
[new ServerRequest([], [], "/no/such/api/", "GET"), "GET", false],
[new ServerRequest("GET", "/index.php/apps/news/api/v1-2/feeds"), "GET", true, NCN::class, "/feeds"],
[new ServerRequest("GET", "/index.php/apps/news/api/v1-2/feeds"), "GET", true, NCN::class, "/feeds"],
[new ServerRequest("get", "/index.php/apps/news/api/v1-2/feeds"), "GET", true, NCN::class, "/feeds"],
[new ServerRequest("head", "/index.php/apps/news/api/v1-2/feeds"), "GET", true, NCN::class, "/feeds"],
[new ServerRequest("POST", "/tt-rss/api/"), "POST", true, TTRSS::class, "/"],
[new ServerRequest("HEAD", "/no/such/api/"), "GET", false],
[new ServerRequest("GET", "/no/such/api/"), "GET", false],
];
}
}