From 01c1e137f752603cba424b7c0db2db9ed334b295 Mon Sep 17 00:00:00 2001 From: Philipp Date: Sun, 2 Jan 2022 20:25:32 +0100 Subject: [PATCH 01/17] Add OPTIONS endpoint --- src/App/Router.php | 12 ++++-------- src/BaseModule.php | 15 +++++++++++++++ src/Module/Special/Options.php | 16 ++++++++++++++++ 3 files changed, 35 insertions(+), 8 deletions(-) create mode 100644 src/Module/Special/Options.php diff --git a/src/App/Router.php b/src/App/Router.php index bbc3dd348..c2887b105 100644 --- a/src/App/Router.php +++ b/src/App/Router.php @@ -37,9 +37,9 @@ use Friendica\Core\Lock\Capability\ICanLock; use Friendica\LegacyModule; use Friendica\Module\HTTPException\MethodNotAllowed; use Friendica\Module\HTTPException\PageNotFound; +use Friendica\Module\Special\Options; use Friendica\Network\HTTPException; use Friendica\Network\HTTPException\MethodNotAllowedException; -use Friendica\Network\HTTPException\NoContentException; use Friendica\Network\HTTPException\NotFoundException; use Psr\Log\LoggerInterface; @@ -141,13 +141,6 @@ class Router $httpMethod = $this->server['REQUEST_METHOD'] ?? self::GET; - // @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/OPTIONS - // @todo Check allowed methods per requested path - if ($httpMethod === static::OPTIONS) { - header('Allow: ' . implode(',', Router::ALLOWED_METHODS)); - throw new NoContentException(); - } - $this->httpMethod = in_array($httpMethod, self::ALLOWED_METHODS) ? $httpMethod : self::GET; $this->routeCollector = isset($routeCollector) ? @@ -284,6 +277,9 @@ class Router $this->parameters = $routeInfo[2]; } elseif ($routeInfo[0] === Dispatcher::METHOD_NOT_ALLOWED) { throw new HTTPException\MethodNotAllowedException($this->l10n->t('Method not allowed for this module. Allowed method(s): %s', implode(', ', $routeInfo[1]))); + } elseif ($this->httpMethod === static::OPTIONS) { + // Default response for HTTP OPTIONS requests in case there is no special treatment + $moduleClass = Options::class; } else { throw new HTTPException\NotFoundException($this->l10n->t('Page not found.')); } diff --git a/src/BaseModule.php b/src/BaseModule.php index 08efff3d7..4fbf39cf7 100644 --- a/src/BaseModule.php +++ b/src/BaseModule.php @@ -173,6 +173,18 @@ abstract class BaseModule implements ICanHandleRequests { } + /** + * Module OPTIONS method to process submitted data + * + * Extend this method if the module is supposed to process OPTIONS requests. + * Doesn't display any content + * + * @param string[] $request The $_REQUEST content + */ + protected function options(array $request = []) + { + } + /** * {@inheritDoc} */ @@ -225,6 +237,9 @@ abstract class BaseModule implements ICanHandleRequests case Router::PUT: $this->put($request); break; + case Router::OPTIONS: + $this->options($request); + break; } $timestamp = microtime(true); diff --git a/src/Module/Special/Options.php b/src/Module/Special/Options.php new file mode 100644 index 000000000..36bbe4fb1 --- /dev/null +++ b/src/Module/Special/Options.php @@ -0,0 +1,16 @@ +response->setHeader('Allow', implode(',', Router::ALLOWED_METHODS)); + $this->response->setHeader(($this->server['SERVER_PROTOCOL'] ?? 'HTTP/1.1') . ' 204 No Content'); + } +} From 3092e74a3a4aa0c4d81bedfa16ff1f8545e56a87 Mon Sep 17 00:00:00 2001 From: Philipp Date: Sun, 2 Jan 2022 20:40:43 +0100 Subject: [PATCH 02/17] Add OPTIONS endpoint --- src/App/Router.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/App/Router.php b/src/App/Router.php index c2887b105..0640e589d 100644 --- a/src/App/Router.php +++ b/src/App/Router.php @@ -276,10 +276,12 @@ class Router $moduleClass = $routeInfo[1]; $this->parameters = $routeInfo[2]; } elseif ($routeInfo[0] === Dispatcher::METHOD_NOT_ALLOWED) { - throw new HTTPException\MethodNotAllowedException($this->l10n->t('Method not allowed for this module. Allowed method(s): %s', implode(', ', $routeInfo[1]))); - } elseif ($this->httpMethod === static::OPTIONS) { - // Default response for HTTP OPTIONS requests in case there is no special treatment - $moduleClass = Options::class; + if ($this->httpMethod === static::OPTIONS) { + // Default response for HTTP OPTIONS requests in case there is no special treatment + $moduleClass = Options::class; + } else { + throw new HTTPException\MethodNotAllowedException($this->l10n->t('Method not allowed for this module. Allowed method(s): %s', implode(', ', $routeInfo[1]))); + } } else { throw new HTTPException\NotFoundException($this->l10n->t('Page not found.')); } From eaad2207387f23ae9183d03f843fef0a902367ac Mon Sep 17 00:00:00 2001 From: Philipp Date: Sun, 2 Jan 2022 21:28:28 +0100 Subject: [PATCH 03/17] Add explicit status setting for PSR/ResponseInterface & add tests for OPTIONS endpoint --- src/App.php | 3 ++- src/App/Page.php | 6 ++++++ src/Capabilities/ICanCreateResponses.php | 10 +++++++++ src/Module/Response.php | 15 +++++++++++++- src/Module/Special/Options.php | 4 ++-- tests/src/Module/Special/OptionsTest.php | 26 ++++++++++++++++++++++++ 6 files changed, 60 insertions(+), 4 deletions(-) create mode 100644 tests/src/Module/Special/OptionsTest.php diff --git a/src/App.php b/src/App.php index f7c929820..c56222b63 100644 --- a/src/App.php +++ b/src/App.php @@ -710,7 +710,8 @@ class App $timestamp = microtime(true); $response = $module->run($input); $this->profiler->set(microtime(true) - $timestamp, 'content'); - if ($response->getHeaderLine(ICanCreateResponses::X_HEADER) === ICanCreateResponses::TYPE_HTML) { + if ($response->getHeaderLine(ICanCreateResponses::X_HEADER) === ICanCreateResponses::TYPE_HTML && + $response->getStatusCode() == 200) { $page->run($this, $this->baseURL, $this->args, $this->mode, $response, $this->l10n, $this->profiler, $this->config, $pconfig); } else { $page->exit($response); diff --git a/src/App/Page.php b/src/App/Page.php index be5f8dc1e..57468f8e5 100644 --- a/src/App/Page.php +++ b/src/App/Page.php @@ -378,6 +378,12 @@ class Page implements ArrayAccess */ public function exit(ResponseInterface $response) { + header(sprintf("HTTP/%s %i %s", + $response->getProtocolVersion(), + $response->getStatusCode(), + $response->getReasonPhrase()) + ); + foreach ($response->getHeaders() as $key => $header) { if (is_array($header)) { $header_str = implode(',', $header); diff --git a/src/Capabilities/ICanCreateResponses.php b/src/Capabilities/ICanCreateResponses.php index f0dac471f..96149bab1 100644 --- a/src/Capabilities/ICanCreateResponses.php +++ b/src/Capabilities/ICanCreateResponses.php @@ -70,6 +70,16 @@ interface ICanCreateResponses */ public function setType(string $type, ?string $content_type = null): void; + /** + * Sets the status and the reason for the response + * + * @param int $status The HTTP status code + * @param null|string $reason Reason phrase (when empty a default will be used based on the status code) + * + * @return void + */ + public function setStatus(int $status = 200, ?string $reason = null): void; + /** * Creates a PSR-7 compliant interface * @see https://www.php-fig.org/psr/psr-7/ diff --git a/src/Module/Response.php b/src/Module/Response.php index dc11c9908..e4bfde7a3 100644 --- a/src/Module/Response.php +++ b/src/Module/Response.php @@ -40,6 +40,10 @@ class Response implements ICanCreateResponses */ protected $type = self::TYPE_HTML; + protected $status = 200; + + protected $reason = null; + /** * {@inheritDoc} */ @@ -111,6 +115,15 @@ class Response implements ICanCreateResponses $this->type = $type; } + /** + * {@inheritDoc} + */ + public function setStatus(int $status = 200, ?string $reason = null): void + { + $this->status = $status; + $this->reason = $reason; + } + /** * {@inheritDoc} */ @@ -127,6 +140,6 @@ class Response implements ICanCreateResponses // Setting the response type as an X-header for direct usage $this->headers[static::X_HEADER] = $this->type; - return new \GuzzleHttp\Psr7\Response(200, $this->headers, $this->content); + return new \GuzzleHttp\Psr7\Response($this->status, $this->headers, $this->content, $this->reason); } } diff --git a/src/Module/Special/Options.php b/src/Module/Special/Options.php index 36bbe4fb1..327f746b0 100644 --- a/src/Module/Special/Options.php +++ b/src/Module/Special/Options.php @@ -10,7 +10,7 @@ class Options extends BaseModule protected function options(array $request = []) { // @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/OPTIONS - $this->response->setHeader('Allow', implode(',', Router::ALLOWED_METHODS)); - $this->response->setHeader(($this->server['SERVER_PROTOCOL'] ?? 'HTTP/1.1') . ' 204 No Content'); + $this->response->setHeader(implode(',', Router::ALLOWED_METHODS), 'Allow'); + $this->response->setStatus(204); } } diff --git a/tests/src/Module/Special/OptionsTest.php b/tests/src/Module/Special/OptionsTest.php new file mode 100644 index 000000000..3f7024f2d --- /dev/null +++ b/tests/src/Module/Special/OptionsTest.php @@ -0,0 +1,26 @@ + Router::OPTIONS]))->run(); + + self::assertEmpty((string)$response->getBody()); + self::assertEquals(204, $response->getStatusCode()); + self::assertEquals('No Content', $response->getReasonPhrase()); + self::assertEquals([ + 'Allow' => [implode(',', Router::ALLOWED_METHODS)], + ICanCreateResponses::X_HEADER => ['html'], + ], $response->getHeaders()); + self::assertEquals(implode(',', Router::ALLOWED_METHODS), $response->getHeaderLine('Allow')); + } +} From c7f2ba213b6be62166a8a8eb738f6900764515cf Mon Sep 17 00:00:00 2001 From: Philipp Date: Sun, 2 Jan 2022 22:17:04 +0100 Subject: [PATCH 04/17] Fix OPTIONS --- src/App/Page.php | 3 ++- src/BaseModule.php | 8 ++++---- src/Module/Response.php | 2 +- tests/src/Module/Special/OptionsTest.php | 2 ++ 4 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/App/Page.php b/src/App/Page.php index 57468f8e5..9e59ab9ae 100644 --- a/src/App/Page.php +++ b/src/App/Page.php @@ -32,6 +32,7 @@ use Friendica\Core\Hook; use Friendica\Core\L10n; use Friendica\Core\Renderer; use Friendica\Core\Theme; +use Friendica\DI; use Friendica\Network\HTTPException; use Friendica\Util\Network; use Friendica\Util\Strings; @@ -378,7 +379,7 @@ class Page implements ArrayAccess */ public function exit(ResponseInterface $response) { - header(sprintf("HTTP/%s %i %s", + header(sprintf("HTTP/%s %s %s", $response->getProtocolVersion(), $response->getStatusCode(), $response->getReasonPhrase()) diff --git a/src/BaseModule.php b/src/BaseModule.php index 4fbf39cf7..ae3b72140 100644 --- a/src/BaseModule.php +++ b/src/BaseModule.php @@ -191,23 +191,23 @@ abstract class BaseModule implements ICanHandleRequests public function run(array $request = []): ResponseInterface { // @see https://github.com/tootsuite/mastodon/blob/c3aef491d66aec743a3a53e934a494f653745b61/config/initializers/cors.rb - if (substr($request['pagename'] ?? '', 0, 12) == '.well-known/') { + if (substr($this->args->getQueryString(), 0, 12) == '.well-known/') { $this->response->setHeader('*', 'Access-Control-Allow-Origin'); $this->response->setHeader('*', 'Access-Control-Allow-Headers'); $this->response->setHeader(Router::GET, 'Access-Control-Allow-Methods'); $this->response->setHeader('false', 'Access-Control-Allow-Credentials'); - } elseif (substr($request['pagename'] ?? '', 0, 8) == 'profile/') { + } elseif (substr($this->args->getQueryString(), 0, 8) == 'profile/') { $this->response->setHeader('*', 'Access-Control-Allow-Origin'); $this->response->setHeader('*', 'Access-Control-Allow-Headers'); $this->response->setHeader(Router::GET, 'Access-Control-Allow-Methods'); $this->response->setHeader('false', 'Access-Control-Allow-Credentials'); - } elseif (substr($request['pagename'] ?? '', 0, 4) == 'api/') { + } elseif (substr($this->args->getQueryString(), 0, 4) == 'api/') { $this->response->setHeader('*', 'Access-Control-Allow-Origin'); $this->response->setHeader('*', 'Access-Control-Allow-Headers'); $this->response->setHeader(implode(',', Router::ALLOWED_METHODS), 'Access-Control-Allow-Methods'); $this->response->setHeader('false', 'Access-Control-Allow-Credentials'); $this->response->setHeader('Link', 'Access-Control-Expose-Headers'); - } elseif (substr($request['pagename'] ?? '', 0, 11) == 'oauth/token') { + } elseif (substr($this->args->getQueryString(), 0, 11) == 'oauth/token') { $this->response->setHeader('*', 'Access-Control-Allow-Origin'); $this->response->setHeader('*', 'Access-Control-Allow-Headers'); $this->response->setHeader(Router::POST, 'Access-Control-Allow-Methods'); diff --git a/src/Module/Response.php b/src/Module/Response.php index e4bfde7a3..db30a10d8 100644 --- a/src/Module/Response.php +++ b/src/Module/Response.php @@ -140,6 +140,6 @@ class Response implements ICanCreateResponses // Setting the response type as an X-header for direct usage $this->headers[static::X_HEADER] = $this->type; - return new \GuzzleHttp\Psr7\Response($this->status, $this->headers, $this->content, $this->reason); + return new \GuzzleHttp\Psr7\Response($this->status, $this->headers, $this->content, '1.1', $this->reason); } } diff --git a/tests/src/Module/Special/OptionsTest.php b/tests/src/Module/Special/OptionsTest.php index 3f7024f2d..ea5982e65 100644 --- a/tests/src/Module/Special/OptionsTest.php +++ b/tests/src/Module/Special/OptionsTest.php @@ -2,6 +2,8 @@ namespace Friendica\Test\src\Module\Special; +use Friendica\App\Arguments; +use Friendica\App\Page; use Friendica\App\Router; use Friendica\Capabilities\ICanCreateResponses; use Friendica\DI; From ee2a15d822f8c0b02791638c25180215d943b974 Mon Sep 17 00:00:00 2001 From: Philipp Date: Sun, 2 Jan 2022 22:21:41 +0100 Subject: [PATCH 05/17] Add HTTP method to App\Arguments --- src/App/Arguments.php | 16 ++++++++++++++-- tests/src/App/ArgumentsTest.php | 26 ++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/src/App/Arguments.php b/src/App/Arguments.php index ef1ed9285..ce2474083 100644 --- a/src/App/Arguments.php +++ b/src/App/Arguments.php @@ -52,14 +52,19 @@ class Arguments * @var int The count of arguments */ private $argc; + /** + * @var string The used HTTP method + */ + private $method; - public function __construct(string $queryString = '', string $command = '', string $moduleName = '', array $argv = [], int $argc = 0) + public function __construct(string $queryString = '', string $command = '', string $moduleName = '', array $argv = [], int $argc = 0, string $method = Router::GET) { $this->queryString = $queryString; $this->command = $command; $this->moduleName = $moduleName; $this->argv = $argv; $this->argc = $argc; + $this->method = $method; } /** @@ -94,6 +99,11 @@ class Arguments return $this->argv; } + public function getMethod() + { + return $this->method; + } + /** * @return int The count of arguments of this call */ @@ -199,6 +209,8 @@ class Arguments $module = "login"; } - return new Arguments($queryString, $command, $module, $argv, $argc); + $httpMethod = in_array($server['REQUEST_METHOD'] ?? '', Router::ALLOWED_METHODS) ? $server['REQUEST_METHOD'] : Router::GET; + + return new Arguments($queryString, $command, $module, $argv, $argc, $httpMethod); } } diff --git a/tests/src/App/ArgumentsTest.php b/tests/src/App/ArgumentsTest.php index e41c99acb..51931fe81 100644 --- a/tests/src/App/ArgumentsTest.php +++ b/tests/src/App/ArgumentsTest.php @@ -32,6 +32,7 @@ class ArgumentsTest extends TestCase self::assertEquals($assert['command'], $arguments->getCommand()); self::assertEquals($assert['argv'], $arguments->getArgv()); self::assertEquals($assert['argc'], $arguments->getArgc()); + self::assertEquals($assert['method'], $arguments->getMethod()); self::assertCount($assert['argc'], $arguments->getArgv()); } @@ -47,6 +48,7 @@ class ArgumentsTest extends TestCase 'command' => '', 'argv' => [], 'argc' => 0, + 'method' => App\Router::GET ], $arguments); } @@ -60,6 +62,7 @@ class ArgumentsTest extends TestCase 'command' => 'profile/test/it', 'argv' => ['profile', 'test', 'it'], 'argc' => 3, + 'method' => App\Router::GET, ], 'server' => [ 'QUERY_STRING' => 'pagename=profile/test/it&arg1=value1&arg2=value2', @@ -74,6 +77,7 @@ class ArgumentsTest extends TestCase 'command' => '~test/it', 'argv' => ['~test', 'it'], 'argc' => 2, + 'method' => App\Router::GET, ], 'server' => [ 'QUERY_STRING' => 'pagename=~test/it&arg1=value1&arg2=value2', @@ -88,6 +92,7 @@ class ArgumentsTest extends TestCase 'command' => 'u/test/it', 'argv' => ['u', 'test', 'it'], 'argc' => 3, + 'method' => App\Router::GET, ], 'server' => [ 'QUERY_STRING' => 'pagename=u/test/it&arg1=value1&arg2=value2', @@ -102,6 +107,7 @@ class ArgumentsTest extends TestCase 'command' => 'profile/test/it', 'argv' => ['profile', 'test', 'it'], 'argc' => 3, + 'method' => App\Router::GET, ], 'server' => [ 'QUERY_STRING' => 'pagename=profile/test/it&arg1=value1&arg2=value2/', @@ -116,6 +122,7 @@ class ArgumentsTest extends TestCase 'command' => 'profile/test/it', 'argv' => ['profile', 'test', 'it'], 'argc' => 3, + 'method' => App\Router::GET, ], 'server' => [ 'QUERY_STRING' => 'wrong=profile/test/it&arg1=value1&arg2=value2/', @@ -130,6 +137,7 @@ class ArgumentsTest extends TestCase 'command' => 'notvalid/it', 'argv' => ['notvalid', 'it'], 'argc' => 2, + 'method' => App\Router::GET, ], 'server' => [ 'QUERY_STRING' => 'pagename=notvalid/it&arg1=value1&arg2=value2/', @@ -143,6 +151,7 @@ class ArgumentsTest extends TestCase 'command' => '', 'argv' => [], 'argc' => 0, + 'method' => App\Router::GET, ], 'server' => [ 'QUERY_STRING' => 'arg1=value1&arg2=value2/', @@ -156,6 +165,7 @@ class ArgumentsTest extends TestCase 'command' => 'api/call.json', 'argv' => ['api', 'call.json'], 'argc' => 2, + 'method' => App\Router::GET, ], 'server' => [ 'QUERY_STRING' => 'pagename=api/call.json', @@ -164,6 +174,22 @@ class ArgumentsTest extends TestCase 'pagename' => 'api/call.json' ], ], + 'withHTTPMethod' => [ + 'assert' => [ + 'queryString' => 'api/call.json', + 'command' => 'api/call.json', + 'argv' => ['api', 'call.json'], + 'argc' => 2, + 'method' => App\Router::POST, + ], + 'server' => [ + 'QUERY_STRING' => 'pagename=api/call.json', + 'REQUEST_METHOD' => App\Router::POST, + ], + 'get' => [ + 'pagename' => 'api/call.json' + ], + ], ]; } From 4e67bfed8d78db4e080bfde16d251c7f600e000f Mon Sep 17 00:00:00 2001 From: Philipp Date: Sun, 2 Jan 2022 22:25:50 +0100 Subject: [PATCH 06/17] Use Args::getMethod() at various places --- src/App/Router.php | 13 ++----------- src/BaseModule.php | 2 +- src/Factory/Api/Mastodon/Error.php | 2 +- src/Module/BaseApi.php | 2 +- src/Module/Special/HTTPException.php | 2 +- 5 files changed, 6 insertions(+), 15 deletions(-) diff --git a/src/App/Router.php b/src/App/Router.php index 0640e589d..2984a8aca 100644 --- a/src/App/Router.php +++ b/src/App/Router.php @@ -74,11 +74,6 @@ class Router /** @var RouteCollector */ protected $routeCollector; - /** - * @var string The HTTP method - */ - private $httpMethod; - /** * @var array Module parameters */ @@ -139,10 +134,6 @@ class Router $this->logger = $logger; $this->dice_profiler_threshold = $config->get('system', 'dice_profiler_threshold', 0); - $httpMethod = $this->server['REQUEST_METHOD'] ?? self::GET; - - $this->httpMethod = in_array($httpMethod, self::ALLOWED_METHODS) ? $httpMethod : self::GET; - $this->routeCollector = isset($routeCollector) ? $routeCollector : new RouteCollector(new Std(), new GroupCountBased()); @@ -271,12 +262,12 @@ class Router $this->parameters = []; - $routeInfo = $dispatcher->dispatch($this->httpMethod, $cmd); + $routeInfo = $dispatcher->dispatch($this->args->getMethod(), $cmd); if ($routeInfo[0] === Dispatcher::FOUND) { $moduleClass = $routeInfo[1]; $this->parameters = $routeInfo[2]; } elseif ($routeInfo[0] === Dispatcher::METHOD_NOT_ALLOWED) { - if ($this->httpMethod === static::OPTIONS) { + if ($this->args->getMethod() === static::OPTIONS) { // Default response for HTTP OPTIONS requests in case there is no special treatment $moduleClass = Options::class; } else { diff --git a/src/BaseModule.php b/src/BaseModule.php index ae3b72140..bd096d94a 100644 --- a/src/BaseModule.php +++ b/src/BaseModule.php @@ -223,7 +223,7 @@ abstract class BaseModule implements ICanHandleRequests $this->profiler->set(microtime(true) - $timestamp, 'init'); - switch ($this->server['REQUEST_METHOD'] ?? Router::GET) { + switch ($this->args->getMethod() ?? Router::GET) { case Router::DELETE: $this->delete($request); break; diff --git a/src/Factory/Api/Mastodon/Error.php b/src/Factory/Api/Mastodon/Error.php index 0d9d40a60..0f08ee90b 100644 --- a/src/Factory/Api/Mastodon/Error.php +++ b/src/Factory/Api/Mastodon/Error.php @@ -47,7 +47,7 @@ class Error extends BaseFactory private function logError(int $errorno, string $error) { - $this->logger->info('API Error', ['no' => $errorno, 'error' => $error, 'method' => $this->server['REQUEST_METHOD'] ?? '', 'command' => $this->args->getQueryString(), 'user-agent' => $this->server['HTTP_USER_AGENT'] ?? '']); + $this->logger->info('API Error', ['no' => $errorno, 'error' => $error, 'method' => $this->args->getMethod(), 'command' => $this->args->getQueryString(), 'user-agent' => $this->server['HTTP_USER_AGENT'] ?? '']); } public function RecordNotFound() diff --git a/src/Module/BaseApi.php b/src/Module/BaseApi.php index 181a51835..b6824140d 100644 --- a/src/Module/BaseApi.php +++ b/src/Module/BaseApi.php @@ -82,7 +82,7 @@ class BaseApi extends BaseModule public function run(array $request = [], bool $scopecheck = true): ResponseInterface { if ($scopecheck) { - switch ($this->server['REQUEST_METHOD'] ?? Router::GET) { + switch ($this->args->getMethod()) { case Router::DELETE: case Router::PATCH: case Router::POST: diff --git a/src/Module/Special/HTTPException.php b/src/Module/Special/HTTPException.php index 34bb67538..95448606e 100644 --- a/src/Module/Special/HTTPException.php +++ b/src/Module/Special/HTTPException.php @@ -89,7 +89,7 @@ class HTTPException header($_SERVER["SERVER_PROTOCOL"] . ' ' . $e->getCode() . ' ' . $e->getDescription()); if ($e->getCode() >= 400) { - Logger::debug('Exit with error', ['code' => $e->getCode(), 'description' => $e->getDescription(), 'query' => DI::args()->getQueryString(), 'callstack' => System::callstack(20), 'method' => $_SERVER['REQUEST_METHOD'], 'agent' => $_SERVER['HTTP_USER_AGENT'] ?? '']); + Logger::debug('Exit with error', ['code' => $e->getCode(), 'description' => $e->getDescription(), 'query' => DI::args()->getQueryString(), 'callstack' => System::callstack(20), 'method' => DI::args()->getMethod(), 'agent' => $_SERVER['HTTP_USER_AGENT'] ?? '']); } $tpl = Renderer::getMarkupTemplate('exception.tpl'); From 5e859395023face01d03160f4807b85c64d081a9 Mon Sep 17 00:00:00 2001 From: Philipp Date: Sun, 2 Jan 2022 22:51:16 +0100 Subject: [PATCH 07/17] Introduce FixtureTest::useHttpMethod for manually overwrite the HTTP Method argument --- tests/FixtureTest.php | 18 ++++++++++++++++++ tests/src/Module/Api/ApiTest.php | 13 ++++++++++++- .../Friendica/DirectMessages/SearchTest.php | 6 +++--- .../Module/Api/Friendica/Photo/DeleteTest.php | 15 +++++++++++---- .../Api/Friendica/Photoalbum/DeleteTest.php | 13 ++++++++++--- .../Api/Friendica/Photoalbum/UpdateTest.php | 15 +++++++++++---- .../Api/GnuSocial/GnuSocial/ConfigTest.php | 3 +-- .../Accounts/VerifyCredentialsTest.php | 2 +- .../Twitter/Account/RateLimitStatusTest.php | 2 +- .../Api/Twitter/Account/UpdateProfileTest.php | 4 +++- .../Module/Api/Twitter/Blocks/ListsTest.php | 2 +- .../Api/Twitter/DirectMessages/AllTest.php | 2 +- .../DirectMessages/ConversationTest.php | 2 +- .../Api/Twitter/DirectMessages/DestroyTest.php | 10 +++++----- .../Api/Twitter/DirectMessages/InboxTest.php | 2 +- .../Api/Twitter/DirectMessages/NewDMTest.php | 10 +++++----- .../Api/Twitter/DirectMessages/SentTest.php | 4 ++-- .../Api/Twitter/Favorites/CreateTest.php | 13 ++++++++++--- .../Api/Twitter/Favorites/DestroyTest.php | 11 +++++++++-- tests/src/Module/Api/Twitter/FavoritesTest.php | 4 ++-- .../Module/Api/Twitter/Followers/ListsTest.php | 2 +- .../Module/Api/Twitter/Friends/ListsTest.php | 2 +- .../Api/Twitter/Friendships/IncomingTest.php | 2 +- .../Module/Api/Twitter/Lists/StatusesTest.php | 6 +++--- .../Module/Api/Twitter/Media/UploadTest.php | 15 +++++++++++---- .../Api/Twitter/Statuses/DestroyTest.php | 11 +++++++++-- .../Api/Twitter/Statuses/MentionsTest.php | 6 +++--- .../Statuses/NetworkPublicTimelineTest.php | 6 +++--- .../Api/Twitter/Statuses/RetweetTest.php | 13 ++++++++++--- .../Module/Api/Twitter/Statuses/ShowTest.php | 6 +++--- .../Module/Api/Twitter/Statuses/UpdateTest.php | 11 +++++++++-- .../Api/Twitter/Statuses/UserTimelineTest.php | 6 +++--- .../Module/Api/Twitter/Users/LookupTest.php | 4 ++-- .../Module/Api/Twitter/Users/SearchTest.php | 6 +++--- .../src/Module/Api/Twitter/Users/ShowTest.php | 4 ++-- tests/src/Module/Special/OptionsTest.php | 6 +++--- 36 files changed, 175 insertions(+), 82 deletions(-) diff --git a/tests/FixtureTest.php b/tests/FixtureTest.php index 17760c85d..bbf8cfd53 100644 --- a/tests/FixtureTest.php +++ b/tests/FixtureTest.php @@ -6,6 +6,8 @@ namespace Friendica\Test; use Dice\Dice; +use Friendica\App\Arguments; +use Friendica\App\Router; use Friendica\Core\Config\ValueObject\Cache; use Friendica\Core\Config\Capability\IManageConfigValues; use Friendica\Core\Session; @@ -50,4 +52,20 @@ abstract class FixtureTest extends DatabaseTest // Load the API dataset for the whole API $this->loadFixture(__DIR__ . '/datasets/api.fixture.php', $dba); } + + protected function useHttpMethod(string $method = Router::GET) + { + $server = $_SERVER; + $server['REQUEST_METHOD'] = $method; + + $this->dice = $this->dice + ->addRule(Arguments::class, [ + 'instanceOf' => Arguments::class, + 'call' => [ + ['determine', [$server, $_GET], Dice::CHAIN_CALL], + ], + ]); + + DI::init($this->dice); + } } diff --git a/tests/src/Module/Api/ApiTest.php b/tests/src/Module/Api/ApiTest.php index 890bba19b..6bddf35f3 100644 --- a/tests/src/Module/Api/ApiTest.php +++ b/tests/src/Module/Api/ApiTest.php @@ -21,7 +21,9 @@ namespace Friendica\Test\src\Module\Api; +use Dice\Dice; use Friendica\App; +use Friendica\App\Arguments; use Friendica\Capabilities\ICanCreateResponses; use Friendica\Core\Addon; use Friendica\Core\Hook; @@ -167,9 +169,18 @@ abstract class ApiTest extends FixtureTest { parent::setUp(); // TODO: Change the autogenerated stub + $server = $_SERVER; + $server['REQUEST_METHOD'] = App\Router::GET; + $this->dice = $this->dice ->addRule(Authentication::class, ['instanceOf' => AuthenticationDouble::class, 'shared' => true]) - ->addRule(App::class, ['instanceOf' => AppDouble::class, 'shared' => true]); + ->addRule(App::class, ['instanceOf' => AppDouble::class, 'shared' => true]) + ->addRule(Arguments::class, [ + 'instanceOf' => App\Arguments::class, + 'call' => [ + ['determine', [$server, $_GET], Dice::CHAIN_CALL], + ], + ]); DI::init($this->dice); // Manual override to bypass API authentication diff --git a/tests/src/Module/Api/Friendica/DirectMessages/SearchTest.php b/tests/src/Module/Api/Friendica/DirectMessages/SearchTest.php index af12be2c1..346697eb5 100644 --- a/tests/src/Module/Api/Friendica/DirectMessages/SearchTest.php +++ b/tests/src/Module/Api/Friendica/DirectMessages/SearchTest.php @@ -34,7 +34,7 @@ class SearchTest extends ApiTest { $directMessage = new DirectMessage(new NullLogger(), DI::dba(), DI::twitterUser()); - $response = (new Search($directMessage, DI::dba(), DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::GET])) + $response = (new Search($directMessage, DI::dba(), DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [])) ->run(); $json = $this->toJson($response); @@ -52,7 +52,7 @@ class SearchTest extends ApiTest $directMessage = new DirectMessage(new NullLogger(), DI::dba(), DI::twitterUser()); - $response = (new Search($directMessage, DI::dba(), DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::GET])) + $response = (new Search($directMessage, DI::dba(), DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [])) ->run([ 'searchstring' => 'item_body' ]); @@ -73,7 +73,7 @@ class SearchTest extends ApiTest { $directMessage = new DirectMessage(new NullLogger(), DI::dba(), DI::twitterUser()); - $response = (new Search($directMessage, DI::dba(), DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::GET])) + $response = (new Search($directMessage, DI::dba(), DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [])) ->run([ 'searchstring' => 'test' ]); diff --git a/tests/src/Module/Api/Friendica/Photo/DeleteTest.php b/tests/src/Module/Api/Friendica/Photo/DeleteTest.php index e80b863a2..e3e208ff0 100644 --- a/tests/src/Module/Api/Friendica/Photo/DeleteTest.php +++ b/tests/src/Module/Api/Friendica/Photo/DeleteTest.php @@ -29,10 +29,17 @@ use Friendica\Test\src\Module\Api\ApiTest; class DeleteTest extends ApiTest { + protected function setUp(): void + { + parent::setUp(); + + $this->useHttpMethod(Router::POST); + } + public function testEmpty() { $this->expectException(BadRequestException::class); - (new Delete(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::POST]))->run(); + (new Delete(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), []))->run(); } public function testWithoutAuthenticatedUser() @@ -43,14 +50,14 @@ class DeleteTest extends ApiTest public function testWrong() { $this->expectException(BadRequestException::class); - (new Delete(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::POST]))->run(['photo_id' => 1]); + (new Delete(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), []))->run(['photo_id' => 1]); } public function testValidWithPost() { $this->loadFixture(__DIR__ . '/../../../../../datasets/photo/photo.fixture.php', DI::dba()); - $response = (new Delete(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::POST])) + $response = (new Delete(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [])) ->run([ 'photo_id' => '709057080661a283a6aa598501504178' ]); @@ -65,7 +72,7 @@ class DeleteTest extends ApiTest { $this->loadFixture(__DIR__ . '/../../../../../datasets/photo/photo.fixture.php', DI::dba()); - $response = (new Delete(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::POST])) + $response = (new Delete(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [])) ->run([ 'photo_id' => '709057080661a283a6aa598501504178' ]); diff --git a/tests/src/Module/Api/Friendica/Photoalbum/DeleteTest.php b/tests/src/Module/Api/Friendica/Photoalbum/DeleteTest.php index 24c223ef8..6ce77f63a 100644 --- a/tests/src/Module/Api/Friendica/Photoalbum/DeleteTest.php +++ b/tests/src/Module/Api/Friendica/Photoalbum/DeleteTest.php @@ -29,10 +29,17 @@ use Friendica\Test\src\Module\Api\ApiTest; class DeleteTest extends ApiTest { + protected function setUp(): void + { + parent::setUp(); + + $this->useHttpMethod(Router::POST); + } + public function testEmpty() { $this->expectException(BadRequestException::class); - (new Delete(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::POST])) + (new Delete(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [])) ->run(); } @@ -40,7 +47,7 @@ class DeleteTest extends ApiTest public function testWrong() { $this->expectException(BadRequestException::class); - (new Delete(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::POST])) + (new Delete(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [])) ->run([ 'album' => 'album_name' ]); @@ -50,7 +57,7 @@ class DeleteTest extends ApiTest { $this->loadFixture(__DIR__ . '/../../../../../datasets/photo/photo.fixture.php', DI::dba()); - $response = (new Delete(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::POST])) + $response = (new Delete(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [])) ->run([ 'album' => 'test_album'] ); diff --git a/tests/src/Module/Api/Friendica/Photoalbum/UpdateTest.php b/tests/src/Module/Api/Friendica/Photoalbum/UpdateTest.php index 49197cbec..5f25a62ac 100644 --- a/tests/src/Module/Api/Friendica/Photoalbum/UpdateTest.php +++ b/tests/src/Module/Api/Friendica/Photoalbum/UpdateTest.php @@ -29,17 +29,24 @@ use Friendica\Test\src\Module\Api\ApiTest; class UpdateTest extends ApiTest { + protected function setUp(): void + { + parent::setUp(); + + $this->useHttpMethod(Router::POST); + } + public function testEmpty() { $this->expectException(BadRequestException::class); - (new Update(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::POST])) + (new Update(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [])) ->run(); } public function testTooFewArgs() { $this->expectException(BadRequestException::class); - (new Update(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::POST])) + (new Update(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [])) ->run([ 'album' => 'album_name' ]); @@ -48,7 +55,7 @@ class UpdateTest extends ApiTest public function testWrongUpdate() { $this->expectException(BadRequestException::class); - (new Update(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::POST])) + (new Update(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [])) ->run([ 'album' => 'album_name', 'album_new' => 'album_name' @@ -64,7 +71,7 @@ class UpdateTest extends ApiTest { $this->loadFixture(__DIR__ . '/../../../../../datasets/photo/photo.fixture.php', DI::dba()); - $response = (new Update(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::POST])) + $response = (new Update(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [])) ->run([ 'album' => 'test_album', 'album_new' => 'test_album_2' diff --git a/tests/src/Module/Api/GnuSocial/GnuSocial/ConfigTest.php b/tests/src/Module/Api/GnuSocial/GnuSocial/ConfigTest.php index 07e43479d..e45c70208 100644 --- a/tests/src/Module/Api/GnuSocial/GnuSocial/ConfigTest.php +++ b/tests/src/Module/Api/GnuSocial/GnuSocial/ConfigTest.php @@ -17,9 +17,8 @@ class ConfigTest extends ApiTest { DI::config()->set('system', 'ssl_policy', BaseURL::SSL_POLICY_FULL); - $response = (new Config(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::GET])) + $response = (new Config(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [])) ->run(); - $json = $this->toJson($response); self::assertEquals('localhost', $json->site->server); diff --git a/tests/src/Module/Api/Mastodon/Accounts/VerifyCredentialsTest.php b/tests/src/Module/Api/Mastodon/Accounts/VerifyCredentialsTest.php index 5942aa1d2..c7c3dabec 100644 --- a/tests/src/Module/Api/Mastodon/Accounts/VerifyCredentialsTest.php +++ b/tests/src/Module/Api/Mastodon/Accounts/VerifyCredentialsTest.php @@ -16,7 +16,7 @@ class VerifyCredentialsTest extends ApiTest */ public function testApiAccountVerifyCredentials() { - $response = (new VerifyCredentials(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::GET])) + $response = (new VerifyCredentials(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [])) ->run(); $json = $this->toJson($response); diff --git a/tests/src/Module/Api/Twitter/Account/RateLimitStatusTest.php b/tests/src/Module/Api/Twitter/Account/RateLimitStatusTest.php index 9b29d314b..3552179e1 100644 --- a/tests/src/Module/Api/Twitter/Account/RateLimitStatusTest.php +++ b/tests/src/Module/Api/Twitter/Account/RateLimitStatusTest.php @@ -12,7 +12,7 @@ class RateLimitStatusTest extends ApiTest { public function testWithJson() { - $response = (new RateLimitStatus(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::GET], ['extension' => 'json'])) + $response = (new RateLimitStatus(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [], ['extension' => 'json'])) ->run(); $result = $this->toJson($response); diff --git a/tests/src/Module/Api/Twitter/Account/UpdateProfileTest.php b/tests/src/Module/Api/Twitter/Account/UpdateProfileTest.php index 76cb27c91..bdcd54f0c 100644 --- a/tests/src/Module/Api/Twitter/Account/UpdateProfileTest.php +++ b/tests/src/Module/Api/Twitter/Account/UpdateProfileTest.php @@ -14,7 +14,9 @@ class UpdateProfileTest extends ApiTest */ public function testApiAccountUpdateProfile() { - $response = (new UpdateProfile(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::POST], ['extension' => 'json'])) + $this->useHttpMethod(Router::POST); + + $response = (new UpdateProfile(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [], ['extension' => 'json'])) ->run([ 'name' => 'new_name', 'description' => 'new_description' diff --git a/tests/src/Module/Api/Twitter/Blocks/ListsTest.php b/tests/src/Module/Api/Twitter/Blocks/ListsTest.php index 77c45ada8..be7d48ab5 100644 --- a/tests/src/Module/Api/Twitter/Blocks/ListsTest.php +++ b/tests/src/Module/Api/Twitter/Blocks/ListsTest.php @@ -14,7 +14,7 @@ class ListsTest extends ApiTest */ public function testApiStatusesFWithBlocks() { - $response = (new Lists(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::GET])) + $response = (new Lists(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [])) ->run(); $json = $this->toJson($response); diff --git a/tests/src/Module/Api/Twitter/DirectMessages/AllTest.php b/tests/src/Module/Api/Twitter/DirectMessages/AllTest.php index 63290368a..a721fdb5c 100644 --- a/tests/src/Module/Api/Twitter/DirectMessages/AllTest.php +++ b/tests/src/Module/Api/Twitter/DirectMessages/AllTest.php @@ -21,7 +21,7 @@ class AllTest extends ApiTest $directMessage = new DirectMessage(DI::logger(), DI::dba(), DI::twitterUser()); - $response = (new All($directMessage, DI::dba(), DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::GET], ['extension' => 'json'])) + $response = (new All($directMessage, DI::dba(), DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [], ['extension' => 'json'])) ->run(); $json = $this->toJson($response); diff --git a/tests/src/Module/Api/Twitter/DirectMessages/ConversationTest.php b/tests/src/Module/Api/Twitter/DirectMessages/ConversationTest.php index 5667b7276..c10fdde03 100644 --- a/tests/src/Module/Api/Twitter/DirectMessages/ConversationTest.php +++ b/tests/src/Module/Api/Twitter/DirectMessages/ConversationTest.php @@ -19,7 +19,7 @@ class ConversationTest extends ApiTest { $directMessage = new DirectMessage(DI::logger(), DI::dba(), DI::twitterUser()); - $response = (new Conversation($directMessage, DI::dba(), DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::GET], ['extension' => 'json'])) + $response = (new Conversation($directMessage, DI::dba(), DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [], ['extension' => 'json'])) ->run([ 'friendica_verbose' => true, ]); diff --git a/tests/src/Module/Api/Twitter/DirectMessages/DestroyTest.php b/tests/src/Module/Api/Twitter/DirectMessages/DestroyTest.php index b74322778..dadd556e4 100644 --- a/tests/src/Module/Api/Twitter/DirectMessages/DestroyTest.php +++ b/tests/src/Module/Api/Twitter/DirectMessages/DestroyTest.php @@ -18,7 +18,7 @@ class DestroyTest extends ApiTest public function testApiDirectMessagesDestroy() { $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class); - (new Destroy(DI::dba(), DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::GET], ['extension' => 'json'])) + (new Destroy(DI::dba(), DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [], ['extension' => 'json'])) ->run(); } @@ -29,7 +29,7 @@ class DestroyTest extends ApiTest */ public function testApiDirectMessagesDestroyWithVerbose() { - $response = (new Destroy(DI::dba(), DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::GET], ['extension' => 'json'])) + $response = (new Destroy(DI::dba(), DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [], ['extension' => 'json'])) ->run([ 'friendica_verbose' => true, ]); @@ -65,7 +65,7 @@ class DestroyTest extends ApiTest public function testApiDirectMessagesDestroyWithId() { $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class); - (new Destroy(DI::dba(), DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::GET], ['extension' => 'json'])) + (new Destroy(DI::dba(), DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [], ['extension' => 'json'])) ->run([ 'id' => 1 ]); @@ -78,7 +78,7 @@ class DestroyTest extends ApiTest */ public function testApiDirectMessagesDestroyWithIdAndVerbose() { - $response = (new Destroy(DI::dba(), DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::GET], ['extension' => 'json'])) + $response = (new Destroy(DI::dba(), DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [], ['extension' => 'json'])) ->run([ 'id' => 1, 'friendica_parenturi' => 'parent_uri', @@ -102,7 +102,7 @@ class DestroyTest extends ApiTest $ids = DBA::selectToArray('mail', ['id']); $id = $ids[0]['id']; - $response = (new Destroy(DI::dba(), DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::GET], ['extension' => 'json'])) + $response = (new Destroy(DI::dba(), DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [], ['extension' => 'json'])) ->run([ 'id' => $id, 'friendica_verbose' => true, diff --git a/tests/src/Module/Api/Twitter/DirectMessages/InboxTest.php b/tests/src/Module/Api/Twitter/DirectMessages/InboxTest.php index beb61ee96..9219bd6a8 100644 --- a/tests/src/Module/Api/Twitter/DirectMessages/InboxTest.php +++ b/tests/src/Module/Api/Twitter/DirectMessages/InboxTest.php @@ -21,7 +21,7 @@ class InboxTest extends ApiTest $directMessage = new DirectMessage(DI::logger(), DI::dba(), DI::twitterUser()); - $response = (new Inbox($directMessage, DI::dba(), DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::GET], ['extension' => 'json'])) + $response = (new Inbox($directMessage, DI::dba(), DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [], ['extension' => 'json'])) ->run(); $json = $this->toJson($response); diff --git a/tests/src/Module/Api/Twitter/DirectMessages/NewDMTest.php b/tests/src/Module/Api/Twitter/DirectMessages/NewDMTest.php index 3cae992fa..9d7ab8ce7 100644 --- a/tests/src/Module/Api/Twitter/DirectMessages/NewDMTest.php +++ b/tests/src/Module/Api/Twitter/DirectMessages/NewDMTest.php @@ -19,7 +19,7 @@ class NewDMTest extends ApiTest { $directMessage = new DirectMessage(DI::logger(), DI::dba(), DI::twitterUser()); - $response = (new NewDM($directMessage, DI::dba(), DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::GET], ['extension' => 'json'])) + $response = (new NewDM($directMessage, DI::dba(), DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [], ['extension' => 'json'])) ->run(); self::assertEmpty((string)$response->getBody()); @@ -51,7 +51,7 @@ class NewDMTest extends ApiTest { $directMessage = new DirectMessage(DI::logger(), DI::dba(), DI::twitterUser()); - $response = (new NewDM($directMessage, DI::dba(), DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::GET], ['extension' => 'json'])) + $response = (new NewDM($directMessage, DI::dba(), DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [], ['extension' => 'json'])) ->run([ 'text' => 'message_text', 'user_id' => 43 @@ -73,7 +73,7 @@ class NewDMTest extends ApiTest $directMessage = new DirectMessage(DI::logger(), DI::dba(), DI::twitterUser()); - $response = (new NewDM($directMessage, DI::dba(), DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::GET], ['extension' => 'json'])) + $response = (new NewDM($directMessage, DI::dba(), DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [], ['extension' => 'json'])) ->run([ 'text' => 'message_text', 'user_id' => 44 @@ -97,7 +97,7 @@ class NewDMTest extends ApiTest $directMessage = new DirectMessage(DI::logger(), DI::dba(), DI::twitterUser()); - $response = (new NewDM($directMessage, DI::dba(), DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::GET], ['extension' => 'json'])) + $response = (new NewDM($directMessage, DI::dba(), DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [], ['extension' => 'json'])) ->run([ 'text' => 'message_text', 'user_id' => 44, @@ -123,7 +123,7 @@ class NewDMTest extends ApiTest $directMessage = new DirectMessage(DI::logger(), DI::dba(), DI::twitterUser()); - $response = (new NewDM($directMessage, DI::dba(), DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::GET], ['extension' => 'rss'])) + $response = (new NewDM($directMessage, DI::dba(), DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [], ['extension' => 'rss'])) ->run([ 'text' => 'message_text', 'user_id' => 44, diff --git a/tests/src/Module/Api/Twitter/DirectMessages/SentTest.php b/tests/src/Module/Api/Twitter/DirectMessages/SentTest.php index ccea4fd53..2d02d37d5 100644 --- a/tests/src/Module/Api/Twitter/DirectMessages/SentTest.php +++ b/tests/src/Module/Api/Twitter/DirectMessages/SentTest.php @@ -19,7 +19,7 @@ class SentTest extends ApiTest { $directMessage = new DirectMessage(DI::logger(), DI::dba(), DI::twitterUser()); - $response = (new Sent($directMessage, DI::dba(), DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::GET], ['extension' => 'json'])) + $response = (new Sent($directMessage, DI::dba(), DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [], ['extension' => 'json'])) ->run([ 'friendica_verbose' => true, ]); @@ -39,7 +39,7 @@ class SentTest extends ApiTest { $directMessage = new DirectMessage(DI::logger(), DI::dba(), DI::twitterUser()); - $response = (new Sent($directMessage, DI::dba(), DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::GET], ['extension' => 'rss'])) + $response = (new Sent($directMessage, DI::dba(), DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [], ['extension' => 'rss'])) ->run(); self::assertXml((string)$response->getBody(), 'direct-messages'); diff --git a/tests/src/Module/Api/Twitter/Favorites/CreateTest.php b/tests/src/Module/Api/Twitter/Favorites/CreateTest.php index 1055dd9d1..640023883 100644 --- a/tests/src/Module/Api/Twitter/Favorites/CreateTest.php +++ b/tests/src/Module/Api/Twitter/Favorites/CreateTest.php @@ -11,6 +11,13 @@ use Friendica\Test\src\Module\Api\ApiTest; class CreateTest extends ApiTest { + protected function setUp(): void + { + parent::setUp(); + + $this->useHttpMethod(Router::POST); + } + /** * Test the api_favorites_create_destroy() function with an invalid ID. * @@ -20,7 +27,7 @@ class CreateTest extends ApiTest { $this->expectException(BadRequestException::class); - (new Create(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::POST])) + (new Create(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [])) ->run(); } @@ -31,7 +38,7 @@ class CreateTest extends ApiTest */ public function testApiFavoritesCreateDestroyWithCreateAction() { - $response = (new Create(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::POST])) + $response = (new Create(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [])) ->run([ 'id' => 3 ]); @@ -48,7 +55,7 @@ class CreateTest extends ApiTest */ public function testApiFavoritesCreateDestroyWithCreateActionAndRss() { - $response = (new Create(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::POST], ['extension' => ICanCreateResponses::TYPE_RSS])) + $response = (new Create(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [], ['extension' => ICanCreateResponses::TYPE_RSS])) ->run([ 'id' => 3 ]); diff --git a/tests/src/Module/Api/Twitter/Favorites/DestroyTest.php b/tests/src/Module/Api/Twitter/Favorites/DestroyTest.php index 65113f556..9b61e095d 100644 --- a/tests/src/Module/Api/Twitter/Favorites/DestroyTest.php +++ b/tests/src/Module/Api/Twitter/Favorites/DestroyTest.php @@ -10,6 +10,13 @@ use Friendica\Test\src\Module\Api\ApiTest; class DestroyTest extends ApiTest { + protected function setUp(): void + { + parent::setUp(); + + $this->useHttpMethod(Router::POST); + } + /** * Test the api_favorites_create_destroy() function with an invalid ID. * @@ -19,7 +26,7 @@ class DestroyTest extends ApiTest { $this->expectException(BadRequestException::class); - (new Destroy(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::POST])) + (new Destroy(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [])) ->run(); } @@ -30,7 +37,7 @@ class DestroyTest extends ApiTest */ public function testApiFavoritesCreateDestroyWithDestroyAction() { - $response = (new Destroy(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::POST])) + $response = (new Destroy(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [])) ->run([ 'id' => 3 ]); diff --git a/tests/src/Module/Api/Twitter/FavoritesTest.php b/tests/src/Module/Api/Twitter/FavoritesTest.php index 1f0faa971..34ba77e8c 100644 --- a/tests/src/Module/Api/Twitter/FavoritesTest.php +++ b/tests/src/Module/Api/Twitter/FavoritesTest.php @@ -17,7 +17,7 @@ class FavoritesTest extends ApiTest */ public function testApiFavorites() { - $response = (new Favorites(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::GET])) + $response = (new Favorites(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [])) ->run([ 'page' => -1, 'max_id' => 10, @@ -37,7 +37,7 @@ class FavoritesTest extends ApiTest */ public function testApiFavoritesWithRss() { - $response = (new Favorites(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::GET], [ + $response = (new Favorites(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [], [ 'extension' => ICanCreateResponses::TYPE_RSS ]))->run(); diff --git a/tests/src/Module/Api/Twitter/Followers/ListsTest.php b/tests/src/Module/Api/Twitter/Followers/ListsTest.php index e9946bb73..9c8110dfe 100644 --- a/tests/src/Module/Api/Twitter/Followers/ListsTest.php +++ b/tests/src/Module/Api/Twitter/Followers/ListsTest.php @@ -14,7 +14,7 @@ class ListsTest extends ApiTest */ public function testApiStatusesFWithFollowers() { - $response = (new Lists(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::GET])) + $response = (new Lists(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [])) ->run(); $json = $this->toJson($response); diff --git a/tests/src/Module/Api/Twitter/Friends/ListsTest.php b/tests/src/Module/Api/Twitter/Friends/ListsTest.php index 3628f5c96..2088f4893 100644 --- a/tests/src/Module/Api/Twitter/Friends/ListsTest.php +++ b/tests/src/Module/Api/Twitter/Friends/ListsTest.php @@ -16,7 +16,7 @@ class ListsTest extends ApiTest */ public function testApiStatusesFWithFriends() { - $response = (new Lists(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::GET])) + $response = (new Lists(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [])) ->run(); $json = $this->toJson($response); diff --git a/tests/src/Module/Api/Twitter/Friendships/IncomingTest.php b/tests/src/Module/Api/Twitter/Friendships/IncomingTest.php index d8e5cc3da..d0bf1ef21 100644 --- a/tests/src/Module/Api/Twitter/Friendships/IncomingTest.php +++ b/tests/src/Module/Api/Twitter/Friendships/IncomingTest.php @@ -16,7 +16,7 @@ class IncomingTest extends ApiTest */ public function testApiFriendshipsIncoming() { - $response = (new Incoming(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::GET])) + $response = (new Incoming(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [])) ->run(); $json = $this->toJson($response); diff --git a/tests/src/Module/Api/Twitter/Lists/StatusesTest.php b/tests/src/Module/Api/Twitter/Lists/StatusesTest.php index 66ebf8bbd..e2fc82648 100644 --- a/tests/src/Module/Api/Twitter/Lists/StatusesTest.php +++ b/tests/src/Module/Api/Twitter/Lists/StatusesTest.php @@ -19,7 +19,7 @@ class StatusesTest extends ApiTest { $this->expectException(BadRequestException::class); - (new Statuses(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::GET])) + (new Statuses(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [])) ->run(); } @@ -28,7 +28,7 @@ class StatusesTest extends ApiTest */ public function testApiListsStatusesWithListId() { - $response = (new Statuses(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::GET])) + $response = (new Statuses(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [])) ->run([ 'list_id' => 1, 'page' => -1, @@ -48,7 +48,7 @@ class StatusesTest extends ApiTest */ public function testApiListsStatusesWithListIdAndRss() { - $response = (new Statuses(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::GET], ['extension' => 'rss'])) + $response = (new Statuses(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [], ['extension' => 'rss'])) ->run([ 'list_id' => 1 ]); diff --git a/tests/src/Module/Api/Twitter/Media/UploadTest.php b/tests/src/Module/Api/Twitter/Media/UploadTest.php index b3516c307..74be1b993 100644 --- a/tests/src/Module/Api/Twitter/Media/UploadTest.php +++ b/tests/src/Module/Api/Twitter/Media/UploadTest.php @@ -13,6 +13,13 @@ use Friendica\Test\Util\AuthTestConfig; class UploadTest extends ApiTest { + protected function setUp(): void + { + parent::setUp(); + + $this->useHttpMethod(Router::POST); + } + /** * Test the \Friendica\Module\Api\Twitter\Media\Upload module. */ @@ -20,7 +27,7 @@ class UploadTest extends ApiTest { $this->expectException(BadRequestException::class); - (new Upload(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::POST])) + (new Upload(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [])) ->run(); } @@ -34,7 +41,7 @@ class UploadTest extends ApiTest $this->expectException(UnauthorizedException::class); AuthTestConfig::$authenticated = false; - (new Upload(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::POST])) + (new Upload(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [])) ->run(); } @@ -53,7 +60,7 @@ class UploadTest extends ApiTest ] ]; - (new Upload(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::POST])) + (new Upload(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [])) ->run(); } @@ -76,7 +83,7 @@ class UploadTest extends ApiTest ] ]; - $response = (new Upload(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::POST])) + $response = (new Upload(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [])) ->run(); $media = $this->toJson($response); diff --git a/tests/src/Module/Api/Twitter/Statuses/DestroyTest.php b/tests/src/Module/Api/Twitter/Statuses/DestroyTest.php index 0480ba8c2..be37ddf40 100644 --- a/tests/src/Module/Api/Twitter/Statuses/DestroyTest.php +++ b/tests/src/Module/Api/Twitter/Statuses/DestroyTest.php @@ -10,6 +10,13 @@ use Friendica\Test\src\Module\Api\ApiTest; class DestroyTest extends ApiTest { + protected function setUp(): void + { + parent::setUp(); + + $this->useHttpMethod(Router::POST); + } + /** * Test the api_statuses_destroy() function. * @@ -19,7 +26,7 @@ class DestroyTest extends ApiTest { $this->expectException(BadRequestException::class); - (new Destroy(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::POST])) + (new Destroy(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [])) ->run(); } @@ -45,7 +52,7 @@ class DestroyTest extends ApiTest */ public function testApiStatusesDestroyWithId() { - $response = (new Destroy(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::POST])) + $response = (new Destroy(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [])) ->run([ 'id' => 1 ]); diff --git a/tests/src/Module/Api/Twitter/Statuses/MentionsTest.php b/tests/src/Module/Api/Twitter/Statuses/MentionsTest.php index d92f26f72..5c72f4e13 100644 --- a/tests/src/Module/Api/Twitter/Statuses/MentionsTest.php +++ b/tests/src/Module/Api/Twitter/Statuses/MentionsTest.php @@ -17,7 +17,7 @@ class MentionsTest extends ApiTest */ public function testApiStatusesMentions() { - $response = (new Mentions(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::GET])) + $response = (new Mentions(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [])) ->run([ 'max_id' => 10 ]); @@ -35,7 +35,7 @@ class MentionsTest extends ApiTest */ public function testApiStatusesMentionsWithNegativePage() { - $response = (new Mentions(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::GET])) + $response = (new Mentions(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [])) ->run([ 'page' => -2 ]); @@ -67,7 +67,7 @@ class MentionsTest extends ApiTest */ public function testApiStatusesMentionsWithRss() { - $response = (new Mentions(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::GET], ['extension' => ICanCreateResponses::TYPE_RSS])) + $response = (new Mentions(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [], ['extension' => ICanCreateResponses::TYPE_RSS])) ->run([ 'page' => -2 ]); diff --git a/tests/src/Module/Api/Twitter/Statuses/NetworkPublicTimelineTest.php b/tests/src/Module/Api/Twitter/Statuses/NetworkPublicTimelineTest.php index a5217fb71..d5c5fb739 100644 --- a/tests/src/Module/Api/Twitter/Statuses/NetworkPublicTimelineTest.php +++ b/tests/src/Module/Api/Twitter/Statuses/NetworkPublicTimelineTest.php @@ -17,7 +17,7 @@ class NetworkPublicTimelineTest extends ApiTest */ public function testApiStatusesNetworkpublicTimeline() { - $response = (new NetworkPublicTimeline(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::GET])) + $response = (new NetworkPublicTimeline(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [])) ->run([ 'max_id' => 10 ]); @@ -39,7 +39,7 @@ class NetworkPublicTimelineTest extends ApiTest */ public function testApiStatusesNetworkpublicTimelineWithNegativePage() { - $response = (new NetworkPublicTimeline(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::GET])) + $response = (new NetworkPublicTimeline(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [])) ->run([ 'page' => -2 ]); @@ -75,7 +75,7 @@ class NetworkPublicTimelineTest extends ApiTest */ public function testApiStatusesNetworkpublicTimelineWithRss() { - $response = (new NetworkPublicTimeline(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::GET], [ + $response = (new NetworkPublicTimeline(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [], [ 'extension' => ICanCreateResponses::TYPE_RSS ]))->run([ 'page' => -2 diff --git a/tests/src/Module/Api/Twitter/Statuses/RetweetTest.php b/tests/src/Module/Api/Twitter/Statuses/RetweetTest.php index 1d93c0296..2f9944aab 100644 --- a/tests/src/Module/Api/Twitter/Statuses/RetweetTest.php +++ b/tests/src/Module/Api/Twitter/Statuses/RetweetTest.php @@ -10,6 +10,13 @@ use Friendica\Test\src\Module\Api\ApiTest; class RetweetTest extends ApiTest { + protected function setUp(): void + { + parent::setUp(); + + $this->useHttpMethod(Router::POST); + } + /** * Test the api_statuses_repeat() function. * @@ -19,7 +26,7 @@ class RetweetTest extends ApiTest { $this->expectException(BadRequestException::class); - (new Retweet(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::POST])) + (new Retweet(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [])) ->run(); } @@ -45,7 +52,7 @@ class RetweetTest extends ApiTest */ public function testApiStatusesRepeatWithId() { - $response = (new Retweet(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::POST])) + $response = (new Retweet(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [])) ->run([ 'id' => 1 ]); @@ -62,7 +69,7 @@ class RetweetTest extends ApiTest */ public function testApiStatusesRepeatWithSharedId() { - $response = (new Retweet(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::POST])) + $response = (new Retweet(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [])) ->run([ 'id' => 5 ]); diff --git a/tests/src/Module/Api/Twitter/Statuses/ShowTest.php b/tests/src/Module/Api/Twitter/Statuses/ShowTest.php index f9d302f0e..e114c0955 100644 --- a/tests/src/Module/Api/Twitter/Statuses/ShowTest.php +++ b/tests/src/Module/Api/Twitter/Statuses/ShowTest.php @@ -20,7 +20,7 @@ class ShowTest extends ApiTest $this->expectException(BadRequestException::class); - (new Show(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::GET])) + (new Show(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [])) ->run(); } @@ -31,7 +31,7 @@ class ShowTest extends ApiTest */ public function testApiStatusesShowWithId() { - $response = (new Show(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::GET])) + $response = (new Show(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [])) ->run([ 'id' => 1 ]); @@ -49,7 +49,7 @@ class ShowTest extends ApiTest */ public function testApiStatusesShowWithConversation() { - $response = (new Show(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::GET])) + $response = (new Show(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [])) ->run([ 'id' => 1, 'conversation' => 1 diff --git a/tests/src/Module/Api/Twitter/Statuses/UpdateTest.php b/tests/src/Module/Api/Twitter/Statuses/UpdateTest.php index f3f6b5a92..a42862731 100644 --- a/tests/src/Module/Api/Twitter/Statuses/UpdateTest.php +++ b/tests/src/Module/Api/Twitter/Statuses/UpdateTest.php @@ -9,6 +9,13 @@ use Friendica\Test\src\Module\Api\ApiTest; class UpdateTest extends ApiTest { + protected function setUp(): void + { + parent::setUp(); + + $this->useHttpMethod(Router::POST); + } + /** * Test the api_statuses_update() function. * @@ -28,7 +35,7 @@ class UpdateTest extends ApiTest ] ]; - $response = (new Update(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::POST])) + $response = (new Update(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [])) ->run([ 'status' => 'Status content #friendica', 'in_reply_to_status_id' => 0, @@ -50,7 +57,7 @@ class UpdateTest extends ApiTest */ public function testApiStatusesUpdateWithHtml() { - $response = (new Update(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::POST])) + $response = (new Update(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [])) ->run([ 'htmlstatus' => 'Status content', ]); diff --git a/tests/src/Module/Api/Twitter/Statuses/UserTimelineTest.php b/tests/src/Module/Api/Twitter/Statuses/UserTimelineTest.php index eb06133f3..416bbe657 100644 --- a/tests/src/Module/Api/Twitter/Statuses/UserTimelineTest.php +++ b/tests/src/Module/Api/Twitter/Statuses/UserTimelineTest.php @@ -17,7 +17,7 @@ class UserTimelineTest extends ApiTest */ public function testApiStatusesUserTimeline() { - $response = (new UserTimeline(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::GET])) + $response = (new UserTimeline(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [])) ->run([ 'user_id' => 42, 'max_id' => 10, @@ -42,7 +42,7 @@ class UserTimelineTest extends ApiTest */ public function testApiStatusesUserTimelineWithNegativePage() { - $response = (new UserTimeline(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::GET])) + $response = (new UserTimeline(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [])) ->run([ 'user_id' => 42, 'page' => -2, @@ -65,7 +65,7 @@ class UserTimelineTest extends ApiTest */ public function testApiStatusesUserTimelineWithRss() { - $response = (new UserTimeline(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::GET], [ + $response = (new UserTimeline(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [], [ 'extension' => ICanCreateResponses::TYPE_RSS ]))->run(); diff --git a/tests/src/Module/Api/Twitter/Users/LookupTest.php b/tests/src/Module/Api/Twitter/Users/LookupTest.php index 2c5739e82..fcff8b00e 100644 --- a/tests/src/Module/Api/Twitter/Users/LookupTest.php +++ b/tests/src/Module/Api/Twitter/Users/LookupTest.php @@ -19,7 +19,7 @@ class LookupTest extends ApiTest { $this->expectException(NotFoundException::class); - (new Lookup(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::GET])) + (new Lookup(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [])) ->run(); } @@ -30,7 +30,7 @@ class LookupTest extends ApiTest */ public function testApiUsersLookupWithUserId() { - $respone = (new Lookup(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::GET])) + $respone = (new Lookup(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [])) ->run([ 'user_id' => static::OTHER_USER['id'] ]); diff --git a/tests/src/Module/Api/Twitter/Users/SearchTest.php b/tests/src/Module/Api/Twitter/Users/SearchTest.php index c88999e45..2260aba43 100644 --- a/tests/src/Module/Api/Twitter/Users/SearchTest.php +++ b/tests/src/Module/Api/Twitter/Users/SearchTest.php @@ -18,7 +18,7 @@ class SearchTest extends ApiTest */ public function testApiUsersSearch() { - $respone = (new Search(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::GET])) + $respone = (new Search(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [])) ->run([ 'q' => static::OTHER_USER['name'] ]); @@ -35,7 +35,7 @@ class SearchTest extends ApiTest */ public function testApiUsersSearchWithXml() { - $respone = (new Search(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::GET], [ + $respone = (new Search(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [], [ 'extension' => ICanCreateResponses::TYPE_XML ]))->run([ 'q' => static::OTHER_USER['name'] @@ -53,7 +53,7 @@ class SearchTest extends ApiTest { $this->expectException(BadRequestException::class); - (new Search(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::GET])) + (new Search(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [])) ->run(); } } diff --git a/tests/src/Module/Api/Twitter/Users/ShowTest.php b/tests/src/Module/Api/Twitter/Users/ShowTest.php index dd63c3e51..5e1812dd0 100644 --- a/tests/src/Module/Api/Twitter/Users/ShowTest.php +++ b/tests/src/Module/Api/Twitter/Users/ShowTest.php @@ -17,7 +17,7 @@ class ShowTest extends ApiTest */ public function testApiUsersShow() { - $response = (new Show(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::GET])) + $response = (new Show(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [])) ->run(); $json = $this->toJson($response); @@ -37,7 +37,7 @@ class ShowTest extends ApiTest */ public function testApiUsersShowWithXml() { - $response = (new Show(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::GET], [ + $response = (new Show(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [], [ 'extension' => ICanCreateResponses::TYPE_XML ]))->run(); diff --git a/tests/src/Module/Special/OptionsTest.php b/tests/src/Module/Special/OptionsTest.php index ea5982e65..e6f798de1 100644 --- a/tests/src/Module/Special/OptionsTest.php +++ b/tests/src/Module/Special/OptionsTest.php @@ -2,8 +2,6 @@ namespace Friendica\Test\src\Module\Special; -use Friendica\App\Arguments; -use Friendica\App\Page; use Friendica\App\Router; use Friendica\Capabilities\ICanCreateResponses; use Friendica\DI; @@ -14,7 +12,9 @@ class OptionsTest extends FixtureTest { public function testOptions() { - $response = (new Options(DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::OPTIONS]))->run(); + $this->useHttpMethod(Router::OPTIONS); + + $response = (new Options(DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), []))->run(); self::assertEmpty((string)$response->getBody()); self::assertEquals(204, $response->getStatusCode()); From c1f2e391b00c2db19d6fac707b58e57a24d2c121 Mon Sep 17 00:00:00 2001 From: Philipp Date: Sun, 2 Jan 2022 22:54:07 +0100 Subject: [PATCH 08/17] Move Arguments reset to right place --- tests/FixtureTest.php | 11 ++++++++++- tests/src/Module/Api/ApiTest.php | 13 +------------ 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/tests/FixtureTest.php b/tests/FixtureTest.php index bbf8cfd53..4b7deb022 100644 --- a/tests/FixtureTest.php +++ b/tests/FixtureTest.php @@ -32,10 +32,19 @@ abstract class FixtureTest extends DatabaseTest { parent::setUp(); + $server = $_SERVER; + $server['REQUEST_METHOD'] = Router::GET; + $this->dice = (new Dice()) ->addRules(include __DIR__ . '/../static/dependencies.config.php') ->addRule(Database::class, ['instanceOf' => StaticDatabase::class, 'shared' => true]) - ->addRule(IHandleSessions::class, ['instanceOf' => Session\Type\Memory::class, 'shared' => true, 'call' => null]); + ->addRule(IHandleSessions::class, ['instanceOf' => Session\Type\Memory::class, 'shared' => true, 'call' => null]) + ->addRule(Arguments::class, [ + 'instanceOf' => Arguments::class, + 'call' => [ + ['determine', [$server, $_GET], Dice::CHAIN_CALL], + ], + ]); DI::init($this->dice); /** @var IManageConfigValues $config */ diff --git a/tests/src/Module/Api/ApiTest.php b/tests/src/Module/Api/ApiTest.php index 6bddf35f3..890bba19b 100644 --- a/tests/src/Module/Api/ApiTest.php +++ b/tests/src/Module/Api/ApiTest.php @@ -21,9 +21,7 @@ namespace Friendica\Test\src\Module\Api; -use Dice\Dice; use Friendica\App; -use Friendica\App\Arguments; use Friendica\Capabilities\ICanCreateResponses; use Friendica\Core\Addon; use Friendica\Core\Hook; @@ -169,18 +167,9 @@ abstract class ApiTest extends FixtureTest { parent::setUp(); // TODO: Change the autogenerated stub - $server = $_SERVER; - $server['REQUEST_METHOD'] = App\Router::GET; - $this->dice = $this->dice ->addRule(Authentication::class, ['instanceOf' => AuthenticationDouble::class, 'shared' => true]) - ->addRule(App::class, ['instanceOf' => AppDouble::class, 'shared' => true]) - ->addRule(Arguments::class, [ - 'instanceOf' => App\Arguments::class, - 'call' => [ - ['determine', [$server, $_GET], Dice::CHAIN_CALL], - ], - ]); + ->addRule(App::class, ['instanceOf' => AppDouble::class, 'shared' => true]); DI::init($this->dice); // Manual override to bypass API authentication From 9c8d9e83acf08135af42d40bcc9c0a1be9764a3c Mon Sep 17 00:00:00 2001 From: Philipp Date: Sun, 2 Jan 2022 23:05:12 +0100 Subject: [PATCH 09/17] Fix empty accounts[] bug --- src/Module/Api/Mastodon/Accounts/Following.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Module/Api/Mastodon/Accounts/Following.php b/src/Module/Api/Mastodon/Accounts/Following.php index 5176a08cd..8e05a9b71 100644 --- a/src/Module/Api/Mastodon/Accounts/Following.php +++ b/src/Module/Api/Mastodon/Accounts/Following.php @@ -73,6 +73,8 @@ class Following extends BaseApi $params['order'] = ['cid']; } + $accounts = []; + $followers = DBA::select('contact-relation', ['cid'], $condition, $params); while ($follower = DBA::fetch($followers)) { self::setBoundaries($follower['cid']); From 71272e07ee6f583c16ddc30db89dd176f0031aad Mon Sep 17 00:00:00 2001 From: Philipp Date: Sun, 2 Jan 2022 23:21:52 +0100 Subject: [PATCH 10/17] temporary merge response header into static *exit() methods because of compatibility issues --- src/Core/System.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Core/System.php b/src/Core/System.php index 562502895..fa3870313 100644 --- a/src/Core/System.php +++ b/src/Core/System.php @@ -315,7 +315,7 @@ class System Logger::debug('Exit with error', ['code' => $val, 'message' => $message, 'callstack' => System::callstack(20), 'method' => $_SERVER['REQUEST_METHOD'], 'agent' => $_SERVER['HTTP_USER_AGENT'] ?? '']); } header($_SERVER["SERVER_PROTOCOL"] . ' ' . $val . ' ' . $message); - + DI::page()->exit(DI::apiResponse()->generate()); echo $content; exit(); @@ -342,6 +342,7 @@ class System * @param integer $options JSON options */ public static function jsonExit($x, $content_type = 'application/json', int $options = 0) { + DI::page()->exit(DI::apiResponse()->generate()); header("Content-type: $content_type"); echo json_encode($x, $options); exit(); From dc46af5ea14a9ba0487c7e524477a6ab42e775d3 Mon Sep 17 00:00:00 2001 From: Philipp Date: Mon, 3 Jan 2022 19:19:47 +0100 Subject: [PATCH 11/17] Automatically return allowed HTTP methods for OPTIONS per specific endpoint --- src/App/Router.php | 67 ++++++++++--------- src/Module/Special/Options.php | 34 +++++++++- src/Util/Router/FriendicaGroupCountBased.php | 62 +++++++++++++++++ tests/src/Module/Special/OptionsTest.php | 20 +++++- .../Router/FriendicaGroupCountBasedTest.php | 28 ++++++++ 5 files changed, 177 insertions(+), 34 deletions(-) create mode 100644 src/Util/Router/FriendicaGroupCountBased.php create mode 100644 tests/src/Util/Router/FriendicaGroupCountBasedTest.php diff --git a/src/App/Router.php b/src/App/Router.php index 2984a8aca..fd1cec362 100644 --- a/src/App/Router.php +++ b/src/App/Router.php @@ -41,6 +41,7 @@ use Friendica\Module\Special\Options; use Friendica\Network\HTTPException; use Friendica\Network\HTTPException\MethodNotAllowedException; use Friendica\Network\HTTPException\NotFoundException; +use Friendica\Util\Router\FriendicaGroupCountBased; use Psr\Log\LoggerInterface; /** @@ -123,20 +124,18 @@ class Router */ public function __construct(array $server, string $baseRoutesFilepath, L10n $l10n, ICanCache $cache, ICanLock $lock, IManageConfigValues $config, Arguments $args, LoggerInterface $logger, Dice $dice, RouteCollector $routeCollector = null) { - $this->baseRoutesFilepath = $baseRoutesFilepath; - $this->l10n = $l10n; - $this->cache = $cache; - $this->lock = $lock; - $this->args = $args; - $this->config = $config; - $this->dice = $dice; - $this->server = $server; - $this->logger = $logger; + $this->baseRoutesFilepath = $baseRoutesFilepath; + $this->l10n = $l10n; + $this->cache = $cache; + $this->lock = $lock; + $this->args = $args; + $this->config = $config; + $this->dice = $dice; + $this->server = $server; + $this->logger = $logger; $this->dice_profiler_threshold = $config->get('system', 'dice_profiler_threshold', 0); - $this->routeCollector = isset($routeCollector) ? - $routeCollector : - new RouteCollector(new Std(), new GroupCountBased()); + $this->routeCollector = $routeCollector ?? new RouteCollector(new Std(), new GroupCountBased()); if ($this->baseRoutesFilepath && !file_exists($this->baseRoutesFilepath)) { throw new HTTPException\InternalServerErrorException('Routes file path does\'n exist.'); @@ -155,9 +154,7 @@ class Router */ public function loadRoutes(array $routes) { - $routeCollector = (isset($this->routeCollector) ? - $this->routeCollector : - new RouteCollector(new Std(), new GroupCountBased())); + $routeCollector = ($this->routeCollector ?? new RouteCollector(new Std(), new GroupCountBased())); $this->addRoutes($routeCollector, $routes); @@ -175,7 +172,10 @@ class Router if ($this->isGroup($config)) { $this->addGroup($route, $config, $routeCollector); } elseif ($this->isRoute($config)) { - $routeCollector->addRoute($config[1], $route, $config[0]); + // Always add the OPTIONS endpoint to a route + $httpMethods = (array) $config[1]; + $httpMethods[] = Router::OPTIONS; + $routeCollector->addRoute($httpMethods, $route, $config[0]); } else { throw new HTTPException\InternalServerErrorException("Wrong route config for route '" . print_r($route, true) . "'"); } @@ -258,23 +258,26 @@ class Router $cmd = $this->args->getCommand(); $cmd = '/' . ltrim($cmd, '/'); - $dispatcher = new Dispatcher\GroupCountBased($this->getCachedDispatchData()); + $dispatcher = new FriendicaGroupCountBased($this->getCachedDispatchData()); $this->parameters = []; - $routeInfo = $dispatcher->dispatch($this->args->getMethod(), $cmd); - if ($routeInfo[0] === Dispatcher::FOUND) { - $moduleClass = $routeInfo[1]; - $this->parameters = $routeInfo[2]; - } elseif ($routeInfo[0] === Dispatcher::METHOD_NOT_ALLOWED) { - if ($this->args->getMethod() === static::OPTIONS) { - // Default response for HTTP OPTIONS requests in case there is no special treatment - $moduleClass = Options::class; - } else { - throw new HTTPException\MethodNotAllowedException($this->l10n->t('Method not allowed for this module. Allowed method(s): %s', implode(', ', $routeInfo[1]))); - } + // Check if the HTTP method ist OPTIONS and return the special Options Module with the possible HTTP methods + if ($this->args->getMethod() === static::OPTIONS) { + $routeOptions = $dispatcher->getOptions($cmd); + + $moduleClass = Options::class; + $this->parameters = ['allowedMethods' => $routeOptions]; } else { - throw new HTTPException\NotFoundException($this->l10n->t('Page not found.')); + $routeInfo = $dispatcher->dispatch($this->args->getMethod(), $cmd); + if ($routeInfo[0] === Dispatcher::FOUND) { + $moduleClass = $routeInfo[1]; + $this->parameters = $routeInfo[2]; + } elseif ($routeInfo[0] === Dispatcher::METHOD_NOT_ALLOWED) { + throw new HTTPException\MethodNotAllowedException($this->l10n->t('Method not allowed for this module. Allowed method(s): %s', implode(', ', $routeInfo[1]))); + } else { + throw new HTTPException\NotFoundException($this->l10n->t('Page not found.')); + } } return $moduleClass; @@ -374,13 +377,13 @@ class Router */ private function getCachedDispatchData() { - $routerDispatchData = $this->cache->get('routerDispatchData'); + $routerDispatchData = $this->cache->get('routerDispatchData'); $lastRoutesFileModifiedTime = $this->cache->get('lastRoutesFileModifiedTime'); - $forceRecompute = false; + $forceRecompute = false; if ($this->baseRoutesFilepath) { $routesFileModifiedTime = filemtime($this->baseRoutesFilepath); - $forceRecompute = $lastRoutesFileModifiedTime != $routesFileModifiedTime; + $forceRecompute = $lastRoutesFileModifiedTime != $routesFileModifiedTime; } if (!$forceRecompute && $routerDispatchData) { diff --git a/src/Module/Special/Options.php b/src/Module/Special/Options.php index 327f746b0..ecc9e2e0c 100644 --- a/src/Module/Special/Options.php +++ b/src/Module/Special/Options.php @@ -1,16 +1,48 @@ . + * + */ namespace Friendica\Module\Special; use Friendica\App\Router; use Friendica\BaseModule; +/** + * Returns the allowed HTTP methods based on the route information + * + * It's a special class which shouldn't be called directly + * + * @see Router::getModuleClass() + */ class Options extends BaseModule { protected function options(array $request = []) { + $allowedMethods = $this->parameters['AllowedMethods'] ?? []; + + if (empty($allowedMethods)) { + $allowedMethods = Router::ALLOWED_METHODS; + } + // @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/OPTIONS - $this->response->setHeader(implode(',', Router::ALLOWED_METHODS), 'Allow'); + $this->response->setHeader(implode(',', $allowedMethods), 'Allow'); $this->response->setStatus(204); } } diff --git a/src/Util/Router/FriendicaGroupCountBased.php b/src/Util/Router/FriendicaGroupCountBased.php new file mode 100644 index 000000000..46e718eff --- /dev/null +++ b/src/Util/Router/FriendicaGroupCountBased.php @@ -0,0 +1,62 @@ +. + * + */ + +namespace Friendica\Util\Router; + +use FastRoute\Dispatcher\GroupCountBased; + +/** + * Extends the Fast-Router collector for getting the possible HTTP method options for a given URI + */ +class FriendicaGroupCountBased extends GroupCountBased +{ + /** + * Returns all possible HTTP methods for a given URI + * + * @param $uri + * + * @return array + * + * @todo Distinguish between an invalid route and the asterisk (*) default route + */ + public function getOptions($uri): array + { + $varRouteData = $this->variableRouteData; + + // Find allowed methods for this URI by matching against all other HTTP methods as well + $allowedMethods = []; + + foreach ($this->staticRouteMap as $method => $uriMap) { + if (isset($uriMap[$uri])) { + $allowedMethods[] = $method; + } + } + + foreach ($varRouteData as $method => $routeData) { + $result = $this->dispatchVariableRoute($routeData, $uri); + if ($result[0] === self::FOUND) { + $allowedMethods[] = $method; + } + } + + return $allowedMethods; + } +} diff --git a/tests/src/Module/Special/OptionsTest.php b/tests/src/Module/Special/OptionsTest.php index e6f798de1..98010cd18 100644 --- a/tests/src/Module/Special/OptionsTest.php +++ b/tests/src/Module/Special/OptionsTest.php @@ -10,7 +10,7 @@ use Friendica\Test\FixtureTest; class OptionsTest extends FixtureTest { - public function testOptions() + public function testOptionsAll() { $this->useHttpMethod(Router::OPTIONS); @@ -25,4 +25,22 @@ class OptionsTest extends FixtureTest ], $response->getHeaders()); self::assertEquals(implode(',', Router::ALLOWED_METHODS), $response->getHeaderLine('Allow')); } + + public function testOptionsSpecific() + { + $this->useHttpMethod(Router::OPTIONS); + + $response = (new Options(DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [], [ + 'AllowedMethods' => [Router::GET, Router::POST], + ]))->run(); + + self::assertEmpty((string)$response->getBody()); + self::assertEquals(204, $response->getStatusCode()); + self::assertEquals('No Content', $response->getReasonPhrase()); + self::assertEquals([ + 'Allow' => [implode(',', [Router::GET, Router::POST])], + ICanCreateResponses::X_HEADER => ['html'], + ], $response->getHeaders()); + self::assertEquals(implode(',', [Router::GET, Router::POST]), $response->getHeaderLine('Allow')); + } } diff --git a/tests/src/Util/Router/FriendicaGroupCountBasedTest.php b/tests/src/Util/Router/FriendicaGroupCountBasedTest.php new file mode 100644 index 000000000..62bf0d243 --- /dev/null +++ b/tests/src/Util/Router/FriendicaGroupCountBasedTest.php @@ -0,0 +1,28 @@ +addRoute('GET', '/get', Options::class); + $collector->addRoute('POST', '/post', Options::class); + $collector->addRoute('GET', '/multi', Options::class); + $collector->addRoute('POST', '/multi', Options::class); + + $dispatcher = new FriendicaGroupCountBased($collector->getData()); + + self::assertEquals(['GET'], $dispatcher->getOptions('/get')); + self::assertEquals(['POST'], $dispatcher->getOptions('/post')); + self::assertEquals(['GET', 'POST'], $dispatcher->getOptions('/multi')); + } +} From a3a32af5c1d47f036cade7db21a8bbf512d3e566 Mon Sep 17 00:00:00 2001 From: Philipp Date: Mon, 3 Jan 2022 19:26:22 +0100 Subject: [PATCH 12/17] Add feedback --- src/App/Arguments.php | 3 +++ src/BaseModule.php | 2 +- src/Module/Api/Mastodon/Accounts/Followers.php | 2 ++ 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/App/Arguments.php b/src/App/Arguments.php index ce2474083..4d386fc25 100644 --- a/src/App/Arguments.php +++ b/src/App/Arguments.php @@ -99,6 +99,9 @@ class Arguments return $this->argv; } + /** + * @return string The used HTTP method + */ public function getMethod() { return $this->method; diff --git a/src/BaseModule.php b/src/BaseModule.php index bd096d94a..c1a926a3a 100644 --- a/src/BaseModule.php +++ b/src/BaseModule.php @@ -223,7 +223,7 @@ abstract class BaseModule implements ICanHandleRequests $this->profiler->set(microtime(true) - $timestamp, 'init'); - switch ($this->args->getMethod() ?? Router::GET) { + switch ($this->args->getMethod()) { case Router::DELETE: $this->delete($request); break; diff --git a/src/Module/Api/Mastodon/Accounts/Followers.php b/src/Module/Api/Mastodon/Accounts/Followers.php index d5fef22a6..58d1f7d83 100644 --- a/src/Module/Api/Mastodon/Accounts/Followers.php +++ b/src/Module/Api/Mastodon/Accounts/Followers.php @@ -73,6 +73,8 @@ class Followers extends BaseApi $params['order'] = ['cid']; } + $accounts = []; + $followers = DBA::select('contact-relation', ['relation-cid'], $condition, $params); while ($follower = DBA::fetch($followers)) { self::setBoundaries($follower['relation-cid']); From 543e4be0a61adf0f7717d91dc4436a7be9725ad0 Mon Sep 17 00:00:00 2001 From: Philipp Date: Mon, 3 Jan 2022 19:30:27 +0100 Subject: [PATCH 13/17] Replace 'REQUEST_METHOD' with App\Arguments::getMethod() --- mod/pubsub.php | 2 +- mod/pubsubhubbub.php | 2 +- src/Core/System.php | 4 ++-- src/Util/HTTPSignature.php | 6 ++---- 4 files changed, 6 insertions(+), 8 deletions(-) diff --git a/mod/pubsub.php b/mod/pubsub.php index 7d5c70a54..e07b15583 100644 --- a/mod/pubsub.php +++ b/mod/pubsub.php @@ -53,7 +53,7 @@ function pubsub_init(App $a) $nick = ((DI::args()->getArgc() > 1) ? trim(DI::args()->getArgv()[1]) : ''); $contact_id = ((DI::args()->getArgc() > 2) ? intval(DI::args()->getArgv()[2]) : 0 ); - if ($_SERVER['REQUEST_METHOD'] === 'GET') { + if (DI::args()->getMethod() === App\Router::GET) { $hub_mode = trim($_GET['hub_mode'] ?? ''); $hub_topic = trim($_GET['hub_topic'] ?? ''); $hub_challenge = trim($_GET['hub_challenge'] ?? ''); diff --git a/mod/pubsubhubbub.php b/mod/pubsubhubbub.php index 583167bbb..361cb0a59 100644 --- a/mod/pubsubhubbub.php +++ b/mod/pubsubhubbub.php @@ -43,7 +43,7 @@ function pubsubhubbub_init(App $a) { // [hub_secret] => af11... // [hub_topic] => http://friendica.local/dfrn_poll/sazius - if ($_SERVER['REQUEST_METHOD'] === 'POST') { + if (DI::args()->getMethod() === App\Router::POST) { $hub_mode = $_POST['hub_mode'] ?? ''; $hub_callback = $_POST['hub_callback'] ?? ''; $hub_verify_token = $_POST['hub_verify_token'] ?? ''; diff --git a/src/Core/System.php b/src/Core/System.php index fa3870313..8a9cdfb96 100644 --- a/src/Core/System.php +++ b/src/Core/System.php @@ -312,7 +312,7 @@ class System public static function httpExit($val, $message = '', $content = '') { if ($val >= 400) { - Logger::debug('Exit with error', ['code' => $val, 'message' => $message, 'callstack' => System::callstack(20), 'method' => $_SERVER['REQUEST_METHOD'], 'agent' => $_SERVER['HTTP_USER_AGENT'] ?? '']); + Logger::debug('Exit with error', ['code' => $val, 'message' => $message, 'callstack' => System::callstack(20), 'method' => DI::args()->getMethod(), 'agent' => $_SERVER['HTTP_USER_AGENT'] ?? '']); } header($_SERVER["SERVER_PROTOCOL"] . ' ' . $val . ' ' . $message); DI::page()->exit(DI::apiResponse()->generate()); @@ -324,7 +324,7 @@ class System public static function jsonError($httpCode, $data, $content_type = 'application/json') { if ($httpCode >= 400) { - Logger::debug('Exit with error', ['code' => $httpCode, 'content_type' => $content_type, 'callstack' => System::callstack(20), 'method' => $_SERVER['REQUEST_METHOD'], 'agent' => $_SERVER['HTTP_USER_AGENT'] ?? '']); + Logger::debug('Exit with error', ['code' => $httpCode, 'content_type' => $content_type, 'callstack' => System::callstack(20), 'method' => DI::args()->getMethod(), 'agent' => $_SERVER['HTTP_USER_AGENT'] ?? '']); } header($_SERVER["SERVER_PROTOCOL"] . ' ' . $httpCode); self::jsonExit($data, $content_type); diff --git a/src/Util/HTTPSignature.php b/src/Util/HTTPSignature.php index de0ee9351..3e22820e5 100644 --- a/src/Util/HTTPSignature.php +++ b/src/Util/HTTPSignature.php @@ -28,9 +28,7 @@ use Friendica\DI; use Friendica\Model\APContact; use Friendica\Model\Contact; use Friendica\Model\User; -use Friendica\Network\HTTPClient\Response\CurlResult; use Friendica\Network\HTTPClient\Client\HttpClientOptions; -use Friendica\Network\HTTPClient\Capability\ICanHandleHttpResponses; /** * Implements HTTP Signatures per draft-cavage-http-signatures-07. @@ -66,7 +64,7 @@ class HTTPSignature // Decide if $data arrived via controller submission or curl. $headers = []; - $headers['(request-target)'] = strtolower($_SERVER['REQUEST_METHOD']).' '.$_SERVER['REQUEST_URI']; + $headers['(request-target)'] = strtolower(DI::args()->getMethod()).' '.$_SERVER['REQUEST_URI']; foreach ($_SERVER as $k => $v) { if (strpos($k, 'HTTP_') === 0) { @@ -493,7 +491,7 @@ class HTTPSignature } $headers = []; - $headers['(request-target)'] = strtolower($http_headers['REQUEST_METHOD']) . ' ' . parse_url($http_headers['REQUEST_URI'], PHP_URL_PATH); + $headers['(request-target)'] = strtolower(DI::args()->getMethod()) . ' ' . parse_url($http_headers['REQUEST_URI'], PHP_URL_PATH); // First take every header foreach ($http_headers as $k => $v) { From 37f850377e6c28bb58eb7c619c78040d4a816702 Mon Sep 17 00:00:00 2001 From: Philipp Date: Mon, 3 Jan 2022 19:55:47 +0100 Subject: [PATCH 14/17] Fix App routing --- src/App.php | 3 +-- src/Capabilities/ICanCreateResponses.php | 14 ++++++++------ src/Module/Special/Options.php | 2 ++ tests/src/Module/Special/OptionsTest.php | 5 +++-- 4 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/App.php b/src/App.php index c56222b63..f7c929820 100644 --- a/src/App.php +++ b/src/App.php @@ -710,8 +710,7 @@ class App $timestamp = microtime(true); $response = $module->run($input); $this->profiler->set(microtime(true) - $timestamp, 'content'); - if ($response->getHeaderLine(ICanCreateResponses::X_HEADER) === ICanCreateResponses::TYPE_HTML && - $response->getStatusCode() == 200) { + if ($response->getHeaderLine(ICanCreateResponses::X_HEADER) === ICanCreateResponses::TYPE_HTML) { $page->run($this, $this->baseURL, $this->args, $this->mode, $response, $this->l10n, $this->profiler, $this->config, $pconfig); } else { $page->exit($response); diff --git a/src/Capabilities/ICanCreateResponses.php b/src/Capabilities/ICanCreateResponses.php index 96149bab1..dbdc61d84 100644 --- a/src/Capabilities/ICanCreateResponses.php +++ b/src/Capabilities/ICanCreateResponses.php @@ -31,18 +31,20 @@ interface ICanCreateResponses */ const X_HEADER = 'X-RESPONSE-TYPE'; - const TYPE_HTML = 'html'; - const TYPE_XML = 'xml'; - const TYPE_JSON = 'json'; - const TYPE_ATOM = 'atom'; - const TYPE_RSS = 'rss'; + const TYPE_HTML = 'html'; + const TYPE_XML = 'xml'; + const TYPE_JSON = 'json'; + const TYPE_ATOM = 'atom'; + const TYPE_RSS = 'rss'; + const TYPE_BLANK = 'blank'; const ALLOWED_TYPES = [ self::TYPE_HTML, self::TYPE_XML, self::TYPE_JSON, self::TYPE_ATOM, - self::TYPE_RSS + self::TYPE_RSS, + self::TYPE_BLANK, ]; /** diff --git a/src/Module/Special/Options.php b/src/Module/Special/Options.php index ecc9e2e0c..389e7a239 100644 --- a/src/Module/Special/Options.php +++ b/src/Module/Special/Options.php @@ -23,6 +23,7 @@ namespace Friendica\Module\Special; use Friendica\App\Router; use Friendica\BaseModule; +use Friendica\Module\Response; /** * Returns the allowed HTTP methods based on the route information @@ -44,5 +45,6 @@ class Options extends BaseModule // @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/OPTIONS $this->response->setHeader(implode(',', $allowedMethods), 'Allow'); $this->response->setStatus(204); + $this->response->setType(Response::TYPE_BLANK); } } diff --git a/tests/src/Module/Special/OptionsTest.php b/tests/src/Module/Special/OptionsTest.php index 98010cd18..ab85e7f8c 100644 --- a/tests/src/Module/Special/OptionsTest.php +++ b/tests/src/Module/Special/OptionsTest.php @@ -21,7 +21,7 @@ class OptionsTest extends FixtureTest self::assertEquals('No Content', $response->getReasonPhrase()); self::assertEquals([ 'Allow' => [implode(',', Router::ALLOWED_METHODS)], - ICanCreateResponses::X_HEADER => ['html'], + ICanCreateResponses::X_HEADER => ['blank'], ], $response->getHeaders()); self::assertEquals(implode(',', Router::ALLOWED_METHODS), $response->getHeaderLine('Allow')); } @@ -39,7 +39,8 @@ class OptionsTest extends FixtureTest self::assertEquals('No Content', $response->getReasonPhrase()); self::assertEquals([ 'Allow' => [implode(',', [Router::GET, Router::POST])], - ICanCreateResponses::X_HEADER => ['html'], + ICanCreateResponses::X_HEADER => ['blank'], + 'Content-Type' ], $response->getHeaders()); self::assertEquals(implode(',', [Router::GET, Router::POST]), $response->getHeaderLine('Allow')); } From 35a2fd45afefc5511fabd692498518a0b384b379 Mon Sep 17 00:00:00 2001 From: Philipp Date: Mon, 3 Jan 2022 20:18:43 +0100 Subject: [PATCH 15/17] Make Response even more compatible .. --- src/Core/System.php | 23 ++++++++++------------- src/Module/Response.php | 3 +++ 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/Core/System.php b/src/Core/System.php index 8a9cdfb96..de0c80b3d 100644 --- a/src/Core/System.php +++ b/src/Core/System.php @@ -21,10 +21,9 @@ namespace Friendica\Core; -use Exception; -use Friendica\App; use Friendica\Core\Config\Capability\IManageConfigValues; use Friendica\DI; +use Friendica\Module\Response; use Friendica\Network\HTTPException\FoundException; use Friendica\Network\HTTPException\MovedPermanentlyException; use Friendica\Network\HTTPException\TemporaryRedirectException; @@ -292,11 +291,9 @@ class System Logger::notice('xml_status returning non_zero: ' . $st . " message=" . $message); } - header("Content-type: text/xml"); - - $xmldata = ["result" => $result]; - - echo XML::fromArray($xmldata, $xml); + DI::apiResponse()->setType(Response::TYPE_XML); + DI::apiResponse()->addContent(XML::fromArray(["result" => $result], $xml)); + DI::page()->exit(DI::apiResponse()->generate()); exit(); } @@ -314,9 +311,9 @@ class System if ($val >= 400) { Logger::debug('Exit with error', ['code' => $val, 'message' => $message, 'callstack' => System::callstack(20), 'method' => DI::args()->getMethod(), 'agent' => $_SERVER['HTTP_USER_AGENT'] ?? '']); } - header($_SERVER["SERVER_PROTOCOL"] . ' ' . $val . ' ' . $message); + DI::apiResponse()->setStatus($val, $message); + DI::apiResponse()->addContent($content); DI::page()->exit(DI::apiResponse()->generate()); - echo $content; exit(); } @@ -326,7 +323,7 @@ class System if ($httpCode >= 400) { Logger::debug('Exit with error', ['code' => $httpCode, 'content_type' => $content_type, 'callstack' => System::callstack(20), 'method' => DI::args()->getMethod(), 'agent' => $_SERVER['HTTP_USER_AGENT'] ?? '']); } - header($_SERVER["SERVER_PROTOCOL"] . ' ' . $httpCode); + DI::apiResponse()->setStatus($httpCode); self::jsonExit($data, $content_type); } @@ -342,9 +339,9 @@ class System * @param integer $options JSON options */ public static function jsonExit($x, $content_type = 'application/json', int $options = 0) { + DI::apiResponse()->setType(Response::TYPE_JSON, $content_type); + DI::apiResponse()->addContent(json_encode($x, $options)); DI::page()->exit(DI::apiResponse()->generate()); - header("Content-type: $content_type"); - echo json_encode($x, $options); exit(); } @@ -500,7 +497,7 @@ class System */ public static function htmlUpdateExit($o) { - header("Content-type: text/html"); + DI::apiResponse()->setType(Response::TYPE_HTML); echo "\r\n"; // We can remove this hack once Internet Explorer recognises HTML5 natively echo "
"; diff --git a/src/Module/Response.php b/src/Module/Response.php index db30a10d8..f9d46da83 100644 --- a/src/Module/Response.php +++ b/src/Module/Response.php @@ -96,6 +96,9 @@ class Response implements ICanCreateResponses } switch ($type) { + case static::TYPE_HTML: + $content_type = $content_type ?? 'text/html'; + break; case static::TYPE_JSON: $content_type = $content_type ?? 'application/json'; break; From 6dbbd081795fa1c8fe57db2248ac162efeeada88 Mon Sep 17 00:00:00 2001 From: Philipp Date: Tue, 4 Jan 2022 20:47:17 +0100 Subject: [PATCH 16/17] Use rawContent for Special Options to avoid a protected options() method --- src/App/Router.php | 2 +- src/BaseModule.php | 15 --------------- src/Module/Special/Options.php | 8 ++------ tests/src/Module/Special/OptionsTest.php | 1 - 4 files changed, 3 insertions(+), 23 deletions(-) diff --git a/src/App/Router.php b/src/App/Router.php index fd1cec362..c21dfd44d 100644 --- a/src/App/Router.php +++ b/src/App/Router.php @@ -262,7 +262,7 @@ class Router $this->parameters = []; - // Check if the HTTP method ist OPTIONS and return the special Options Module with the possible HTTP methods + // Check if the HTTP method is OPTIONS and return the special Options Module with the possible HTTP methods if ($this->args->getMethod() === static::OPTIONS) { $routeOptions = $dispatcher->getOptions($cmd); diff --git a/src/BaseModule.php b/src/BaseModule.php index c1a926a3a..5ac56533c 100644 --- a/src/BaseModule.php +++ b/src/BaseModule.php @@ -173,18 +173,6 @@ abstract class BaseModule implements ICanHandleRequests { } - /** - * Module OPTIONS method to process submitted data - * - * Extend this method if the module is supposed to process OPTIONS requests. - * Doesn't display any content - * - * @param string[] $request The $_REQUEST content - */ - protected function options(array $request = []) - { - } - /** * {@inheritDoc} */ @@ -237,9 +225,6 @@ abstract class BaseModule implements ICanHandleRequests case Router::PUT: $this->put($request); break; - case Router::OPTIONS: - $this->options($request); - break; } $timestamp = microtime(true); diff --git a/src/Module/Special/Options.php b/src/Module/Special/Options.php index 389e7a239..79ce5d0c2 100644 --- a/src/Module/Special/Options.php +++ b/src/Module/Special/Options.php @@ -34,13 +34,9 @@ use Friendica\Module\Response; */ class Options extends BaseModule { - protected function options(array $request = []) + protected function rawContent(array $request = []) { - $allowedMethods = $this->parameters['AllowedMethods'] ?? []; - - if (empty($allowedMethods)) { - $allowedMethods = Router::ALLOWED_METHODS; - } + $allowedMethods = $this->parameters['AllowedMethods'] ?? Router::ALLOWED_METHODS; // @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/OPTIONS $this->response->setHeader(implode(',', $allowedMethods), 'Allow'); diff --git a/tests/src/Module/Special/OptionsTest.php b/tests/src/Module/Special/OptionsTest.php index ab85e7f8c..460435ae2 100644 --- a/tests/src/Module/Special/OptionsTest.php +++ b/tests/src/Module/Special/OptionsTest.php @@ -40,7 +40,6 @@ class OptionsTest extends FixtureTest self::assertEquals([ 'Allow' => [implode(',', [Router::GET, Router::POST])], ICanCreateResponses::X_HEADER => ['blank'], - 'Content-Type' ], $response->getHeaders()); self::assertEquals(implode(',', [Router::GET, Router::POST]), $response->getHeaderLine('Allow')); } From 0e45f22b06a77fc020478d262f5e74ee35a94fe7 Mon Sep 17 00:00:00 2001 From: Philipp Date: Tue, 4 Jan 2022 20:50:20 +0100 Subject: [PATCH 17/17] optimizations --- src/App/Page.php | 1 - src/App/Router.php | 4 +--- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/src/App/Page.php b/src/App/Page.php index 9e59ab9ae..0d7b16436 100644 --- a/src/App/Page.php +++ b/src/App/Page.php @@ -32,7 +32,6 @@ use Friendica\Core\Hook; use Friendica\Core\L10n; use Friendica\Core\Renderer; use Friendica\Core\Theme; -use Friendica\DI; use Friendica\Network\HTTPException; use Friendica\Util\Network; use Friendica\Util\Strings; diff --git a/src/App/Router.php b/src/App/Router.php index c21dfd44d..6e390a84d 100644 --- a/src/App/Router.php +++ b/src/App/Router.php @@ -264,10 +264,8 @@ class Router // Check if the HTTP method is OPTIONS and return the special Options Module with the possible HTTP methods if ($this->args->getMethod() === static::OPTIONS) { - $routeOptions = $dispatcher->getOptions($cmd); - $moduleClass = Options::class; - $this->parameters = ['allowedMethods' => $routeOptions]; + $this->parameters = ['allowedMethods' => $dispatcher->getOptions($cmd)]; } else { $routeInfo = $dispatcher->dispatch($this->args->getMethod(), $cmd); if ($routeInfo[0] === Dispatcher::FOUND) {