mirror of
https://code.mensbeam.com/MensBeam/Arsse.git
synced 2024-12-22 13:12:41 +00:00
More Guzzle conversion
This commit is contained in:
parent
64ec3f6ae4
commit
56f015bfb9
5 changed files with 27 additions and 29 deletions
|
@ -14,7 +14,6 @@ use JKingWeb\Arsse\Misc\HTTP;
|
||||||
use JKingWeb\Arsse\Db\ExceptionInput;
|
use JKingWeb\Arsse\Db\ExceptionInput;
|
||||||
use Psr\Http\Message\ServerRequestInterface;
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
use Psr\Http\Message\ResponseInterface;
|
use Psr\Http\Message\ResponseInterface;
|
||||||
use Laminas\Diactoros\Response\XmlResponse;
|
|
||||||
|
|
||||||
class API extends \JKingWeb\Arsse\REST\AbstractHandler {
|
class API extends \JKingWeb\Arsse\REST\AbstractHandler {
|
||||||
public const LEVEL = 3;
|
public const LEVEL = 3;
|
||||||
|
@ -180,7 +179,7 @@ class API extends \JKingWeb\Arsse\REST\AbstractHandler {
|
||||||
if ($xml) {
|
if ($xml) {
|
||||||
$d = new \DOMDocument("1.0", "utf-8");
|
$d = new \DOMDocument("1.0", "utf-8");
|
||||||
$d->appendChild($this->makeXMLAssoc($data, $d->createElement("response")));
|
$d->appendChild($this->makeXMLAssoc($data, $d->createElement("response")));
|
||||||
return new XmlResponse($d->saveXML());
|
return HTTP::respXml($d->saveXML());
|
||||||
} else {
|
} else {
|
||||||
return HTTP::respJson($data, 200, [], \JSON_UNESCAPED_SLASHES | \JSON_UNESCAPED_UNICODE);
|
return HTTP::respJson($data, 200, [], \JSON_UNESCAPED_SLASHES | \JSON_UNESCAPED_UNICODE);
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,7 +9,6 @@ namespace JKingWeb\Arsse\REST\Miniflux;
|
||||||
use JKingWeb\Arsse\Misc\HTTP;
|
use JKingWeb\Arsse\Misc\HTTP;
|
||||||
use Psr\Http\Message\ServerRequestInterface;
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
use Psr\Http\Message\ResponseInterface;
|
use Psr\Http\Message\ResponseInterface;
|
||||||
use Laminas\Diactoros\Response\TextResponse;
|
|
||||||
|
|
||||||
class Status extends \JKingWeb\Arsse\REST\AbstractHandler {
|
class Status extends \JKingWeb\Arsse\REST\AbstractHandler {
|
||||||
public function __construct() {
|
public function __construct() {
|
||||||
|
@ -32,6 +31,6 @@ class Status extends \JKingWeb\Arsse\REST\AbstractHandler {
|
||||||
} elseif ($target === "/healthcheck") {
|
} elseif ($target === "/healthcheck") {
|
||||||
$out = "OK";
|
$out = "OK";
|
||||||
}
|
}
|
||||||
return new TextResponse($out);
|
return HTTP::respText($out);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,8 +27,7 @@ use JKingWeb\Arsse\User\ExceptionConflict;
|
||||||
use JKingWeb\Arsse\User\Exception as UserException;
|
use JKingWeb\Arsse\User\Exception as UserException;
|
||||||
use Psr\Http\Message\ServerRequestInterface;
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
use Psr\Http\Message\ResponseInterface;
|
use Psr\Http\Message\ResponseInterface;
|
||||||
use Laminas\Diactoros\Response\TextResponse as GenericResponse;
|
use GuzzleHttp\Psr7\Uri;
|
||||||
use Laminas\Diactoros\Uri;
|
|
||||||
|
|
||||||
class V1 extends \JKingWeb\Arsse\REST\AbstractHandler {
|
class V1 extends \JKingWeb\Arsse\REST\AbstractHandler {
|
||||||
public const VERSION = "2.0.28";
|
public const VERSION = "2.0.28";
|
||||||
|
@ -1203,6 +1202,6 @@ class V1 extends \JKingWeb\Arsse\REST\AbstractHandler {
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function opmlExport(): 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"]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,14 +7,16 @@ declare(strict_types=1);
|
||||||
namespace JKingWeb\Arsse\TestCase\Misc;
|
namespace JKingWeb\Arsse\TestCase\Misc;
|
||||||
|
|
||||||
use JKingWeb\Arsse\Misc\HTTP;
|
use JKingWeb\Arsse\Misc\HTTP;
|
||||||
|
use GuzzleHttp\Psr7\Request;
|
||||||
|
use GuzzleHttp\Psr7\Response;
|
||||||
|
|
||||||
/** @covers \JKingWeb\Arsse\Misc\HTTP */
|
/** @covers \JKingWeb\Arsse\Misc\HTTP */
|
||||||
class TestHTTP extends \JKingWeb\Arsse\Test\AbstractTest {
|
class TestHTTP extends \JKingWeb\Arsse\Test\AbstractTest {
|
||||||
/** @dataProvider provideMediaTypes */
|
/** @dataProvider provideMediaTypes */
|
||||||
public function testMatchMediaType(string $header, array $types, bool $exp): void {
|
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));
|
$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));
|
$this->assertSame($exp, HTTP::matchType($msg, ...$types));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -15,10 +15,9 @@ use JKingWeb\Arsse\REST\TinyTinyRSS\API as TTRSS;
|
||||||
use JKingWeb\Arsse\Misc\HTTP;
|
use JKingWeb\Arsse\Misc\HTTP;
|
||||||
use Psr\Http\Message\RequestInterface;
|
use Psr\Http\Message\RequestInterface;
|
||||||
use Psr\Http\Message\ResponseInterface;
|
use Psr\Http\Message\ResponseInterface;
|
||||||
use Laminas\Diactoros\Request;
|
use GuzzleHttp\Psr7\Response;
|
||||||
use Laminas\Diactoros\Response;
|
use GuzzleHttp\Psr7\Request;
|
||||||
use Laminas\Diactoros\ServerRequest;
|
use GuzzleHttp\Psr7\ServerRequest;
|
||||||
use Laminas\Diactoros\Response\TextResponse;
|
|
||||||
|
|
||||||
/** @covers \JKingWeb\Arsse\REST */
|
/** @covers \JKingWeb\Arsse\REST */
|
||||||
class TestREST extends \JKingWeb\Arsse\Test\AbstractTest {
|
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);
|
$this->userMock->auth->with("someone.else@example.com", "")->returns(true);
|
||||||
Arsse::$user = $this->userMock->get();
|
Arsse::$user = $this->userMock->get();
|
||||||
// create an input server request
|
// create an input server request
|
||||||
$req = new ServerRequest($serverParams);
|
$req = new ServerRequest("GET", "/", [], null, "1.1", $serverParams);
|
||||||
// create the expected output
|
// create the expected output
|
||||||
$exp = $req;
|
$exp = $req;
|
||||||
foreach ($expAttr as $key => $value) {
|
foreach ($expAttr as $key => $value) {
|
||||||
|
@ -156,7 +155,7 @@ class TestREST extends \JKingWeb\Arsse\Test\AbstractTest {
|
||||||
return $origin;
|
return $origin;
|
||||||
});
|
});
|
||||||
$headers = isset($origin) ? ['Origin' => $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);
|
$act = $rMock->get()->corsNegotiate($req, $allowed, $denied);
|
||||||
$this->assertSame($exp, $act);
|
$this->assertSame($exp, $act);
|
||||||
}
|
}
|
||||||
|
@ -189,7 +188,7 @@ class TestREST extends \JKingWeb\Arsse\Test\AbstractTest {
|
||||||
/** @dataProvider provideCorsHeaders */
|
/** @dataProvider provideCorsHeaders */
|
||||||
public function testAddCorsHeaders(string $reqMethod, array $reqHeaders, array $resHeaders, array $expHeaders): void {
|
public function testAddCorsHeaders(string $reqMethod, array $reqHeaders, array $resHeaders, array $expHeaders): void {
|
||||||
$r = new REST();
|
$r = new REST();
|
||||||
$req = new Request("", $reqMethod, "php://memory", $reqHeaders);
|
$req = new Request($reqMethod, "php://memory", $reqHeaders);
|
||||||
$res = HTTP::respEmpty(204, $resHeaders);
|
$res = HTTP::respEmpty(204, $resHeaders);
|
||||||
$exp = HTTP::respEmpty(204, $expHeaders);
|
$exp = HTTP::respEmpty(204, $expHeaders);
|
||||||
$act = $r->corsApply($res, $req);
|
$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' => ["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' => "HEAD,GET"]), HTTP::respEmpty(204, ['Allow' => "HEAD, GET, OPTIONS"])],
|
||||||
[HTTP::respEmpty(204, ['Allow' => "GET"]), HTTP::respEmpty(204, ['Allow' => "GET, HEAD, OPTIONS"])],
|
[HTTP::respEmpty(204, ['Allow' => "GET"]), HTTP::respEmpty(204, ['Allow' => "GET, HEAD, OPTIONS"])],
|
||||||
[new TextResponse("ook", 200), new TextResponse("ook", 200, ['Content-Length' => "3"])],
|
[HTTP::respText("ook", 200), HTTP::respText("ook", 200, ['Content-Length' => "3"])],
|
||||||
[new TextResponse("", 200), new TextResponse("", 200, ['Content-Length' => "0"])],
|
[HTTP::respText("", 200), HTTP::respText("", 200, ['Content-Length' => "0"])],
|
||||||
[new TextResponse("ook", 404), new TextResponse("ook", 404, ['Content-Length' => "3"])],
|
[HTTP::respText("ook", 404), HTTP::respText("ook", 404, ['Content-Length' => "3"])],
|
||||||
[new TextResponse("", 404), new TextResponse("", 404)],
|
[HTTP::respText("", 404), HTTP::respText("", 404)],
|
||||||
[new Response($stream, 200), new Response($stream, 200, ['Content-Length' => "3"]), new Request("", "GET")],
|
[new Response(200, [], $stream), new Response(200, ['Content-Length' => "3"], $stream), new Request("", "GET")],
|
||||||
[new Response($stream, 200), HTTP::respEmpty(200, ['Content-Length' => "3"]), new Request("", "HEAD")],
|
[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 {
|
public function provideMockRequests(): iterable {
|
||||||
return [
|
return [
|
||||||
[new ServerRequest([], [], "/index.php/apps/news/api/v1-2/feeds", "GET"), "GET", true, NCN::class, "/feeds"],
|
[new ServerRequest("GET", "/index.php/apps/news/api/v1-2/feeds"), "GET", true, NCN::class, "/feeds"],
|
||||||
[new ServerRequest([], [], "/index.php/apps/news/api/v1-2/feeds", "HEAD"), "GET", true, NCN::class, "/feeds"],
|
[new ServerRequest("GET", "/index.php/apps/news/api/v1-2/feeds"), "GET", true, NCN::class, "/feeds"],
|
||||||
[new ServerRequest([], [], "/index.php/apps/news/api/v1-2/feeds", "get"), "GET", true, NCN::class, "/feeds"],
|
[new ServerRequest("get", "/index.php/apps/news/api/v1-2/feeds"), "GET", true, NCN::class, "/feeds"],
|
||||||
[new ServerRequest([], [], "/index.php/apps/news/api/v1-2/feeds", "head"), "GET", true, NCN::class, "/feeds"],
|
[new ServerRequest("head", "/index.php/apps/news/api/v1-2/feeds"), "GET", true, NCN::class, "/feeds"],
|
||||||
[new ServerRequest([], [], "/tt-rss/api/", "POST"), "POST", true, TTRSS::class, "/"],
|
[new ServerRequest("POST", "/tt-rss/api/"), "POST", true, TTRSS::class, "/"],
|
||||||
[new ServerRequest([], [], "/no/such/api/", "HEAD"), "GET", false],
|
[new ServerRequest("HEAD", "/no/such/api/"), "GET", false],
|
||||||
[new ServerRequest([], [], "/no/such/api/", "GET"), "GET", false],
|
[new ServerRequest("GET", "/no/such/api/"), "GET", false],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue