Skip to content

Commit

Permalink
More Guzzle conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
JKingweb committed Aug 6, 2022
1 parent 64ec3f6 commit 56f015b
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 29 deletions.
3 changes: 1 addition & 2 deletions lib/REST/Fever/API.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
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;
Expand Down Expand Up @@ -180,7 +179,7 @@ protected function formatResponse(array $data, bool $xml): ResponseInterface {
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);
}
Expand Down
3 changes: 1 addition & 2 deletions lib/REST/Miniflux/Status.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
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() {
Expand All @@ -32,6 +31,6 @@ public function dispatch(ServerRequestInterface $req): ResponseInterface {
} elseif ($target === "/healthcheck") {
$out = "OK";
}
return new TextResponse($out);
return HTTP::respText($out);
}
}
5 changes: 2 additions & 3 deletions lib/REST/Miniflux/V1.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@
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";
Expand Down Expand Up @@ -1203,6 +1202,6 @@ protected function opmlImport(string $data): ResponseInterface {
}

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"]);
}
}
6 changes: 4 additions & 2 deletions tests/cases/Misc/TestHTTP.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@
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));
}

Expand Down
39 changes: 19 additions & 20 deletions tests/cases/REST/TestREST.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
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 {
Expand Down Expand Up @@ -70,7 +69,7 @@ public function testAuthenticateRequests(array $serverParams, array $expAttr): v
$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) {
Expand Down Expand Up @@ -156,7 +155,7 @@ public function testNegotiateCors($origin, bool $exp, string $allowed = null, st
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);
}
Expand Down Expand Up @@ -189,7 +188,7 @@ public function provideCorsNegotiations(): iterable {
/** @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);
Expand Down Expand Up @@ -277,12 +276,12 @@ public function provideUnnormalizedResponses(): iterable {
[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")],
];
}

Expand Down Expand Up @@ -318,13 +317,13 @@ public function testDispatchRequests(ServerRequest $req, string $method, bool $c

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],
];
}
}

0 comments on commit 56f015b

Please sign in to comment.