From 56f015bfb98f816f84d2056d6f3b1917d5941b64 Mon Sep 17 00:00:00 2001 From: "J. King" Date: Sat, 6 Aug 2022 13:40:02 -0400 Subject: [PATCH] More Guzzle conversion --- lib/REST/Fever/API.php | 3 +-- lib/REST/Miniflux/Status.php | 3 +-- lib/REST/Miniflux/V1.php | 5 ++--- tests/cases/Misc/TestHTTP.php | 6 ++++-- tests/cases/REST/TestREST.php | 39 +++++++++++++++++------------------ 5 files changed, 27 insertions(+), 29 deletions(-) diff --git a/lib/REST/Fever/API.php b/lib/REST/Fever/API.php index aa900c91..9b243129 100644 --- a/lib/REST/Fever/API.php +++ b/lib/REST/Fever/API.php @@ -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); } diff --git a/lib/REST/Miniflux/Status.php b/lib/REST/Miniflux/Status.php index c829193b..b84f4a13 100644 --- a/lib/REST/Miniflux/Status.php +++ b/lib/REST/Miniflux/Status.php @@ -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); } } diff --git a/lib/REST/Miniflux/V1.php b/lib/REST/Miniflux/V1.php index bfdf678f..7ff9df24 100644 --- a/lib/REST/Miniflux/V1.php +++ b/lib/REST/Miniflux/V1.php @@ -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"]); } } diff --git a/tests/cases/Misc/TestHTTP.php b/tests/cases/Misc/TestHTTP.php index 9bffe073..b4a580ed 100644 --- a/tests/cases/Misc/TestHTTP.php +++ b/tests/cases/Misc/TestHTTP.php @@ -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)); } diff --git a/tests/cases/REST/TestREST.php b/tests/cases/REST/TestREST.php index 059803da..58dfa524 100644 --- a/tests/cases/REST/TestREST.php +++ b/tests/cases/REST/TestREST.php @@ -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], ]; } }