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/App/Arguments.php b/src/App/Arguments.php index ef1ed9285..4d386fc25 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,14 @@ class Arguments return $this->argv; } + /** + * @return string The used HTTP method + */ + public function getMethod() + { + return $this->method; + } + /** * @return int The count of arguments of this call */ @@ -199,6 +212,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/src/App/Page.php b/src/App/Page.php index be5f8dc1e..0d7b16436 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 %s %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/App/Router.php b/src/App/Router.php index bbc3dd348..6e390a84d 100644 --- a/src/App/Router.php +++ b/src/App/Router.php @@ -37,10 +37,11 @@ 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 Friendica\Util\Router\FriendicaGroupCountBased; use Psr\Log\LoggerInterface; /** @@ -74,11 +75,6 @@ class Router /** @var RouteCollector */ protected $routeCollector; - /** - * @var string The HTTP method - */ - private $httpMethod; - /** * @var array Module parameters */ @@ -128,31 +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); - $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) ? - $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.'); @@ -171,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); @@ -191,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) . "'"); } @@ -274,18 +258,24 @@ 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->httpMethod, $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]))); + // Check if the HTTP method is OPTIONS and return the special Options Module with the possible HTTP methods + if ($this->args->getMethod() === static::OPTIONS) { + $moduleClass = Options::class; + $this->parameters = ['allowedMethods' => $dispatcher->getOptions($cmd)]; } 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; @@ -385,13 +375,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/BaseModule.php b/src/BaseModule.php index 08efff3d7..5ac56533c 100644 --- a/src/BaseModule.php +++ b/src/BaseModule.php @@ -179,23 +179,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'); @@ -211,7 +211,7 @@ abstract class BaseModule implements ICanHandleRequests $this->profiler->set(microtime(true) - $timestamp, 'init'); - switch ($this->server['REQUEST_METHOD'] ?? Router::GET) { + switch ($this->args->getMethod()) { case Router::DELETE: $this->delete($request); break; diff --git a/src/Capabilities/ICanCreateResponses.php b/src/Capabilities/ICanCreateResponses.php index f0dac471f..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, ]; /** @@ -70,6 +72,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/Core/System.php b/src/Core/System.php index 562502895..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(); } @@ -312,11 +309,11 @@ 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); - - echo $content; + DI::apiResponse()->setStatus($val, $message); + DI::apiResponse()->addContent($content); + DI::page()->exit(DI::apiResponse()->generate()); exit(); } @@ -324,9 +321,9 @@ 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); + DI::apiResponse()->setStatus($httpCode); self::jsonExit($data, $content_type); } @@ -342,8 +339,9 @@ class System * @param integer $options JSON options */ public static function jsonExit($x, $content_type = 'application/json', int $options = 0) { - header("Content-type: $content_type"); - echo json_encode($x, $options); + DI::apiResponse()->setType(Response::TYPE_JSON, $content_type); + DI::apiResponse()->addContent(json_encode($x, $options)); + DI::page()->exit(DI::apiResponse()->generate()); exit(); } @@ -499,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/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/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']); 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']); 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/Response.php b/src/Module/Response.php index dc11c9908..f9d46da83 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} */ @@ -92,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; @@ -111,6 +118,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 +143,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, '1.1', $this->reason); } } 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'); diff --git a/src/Module/Special/Options.php b/src/Module/Special/Options.php new file mode 100644 index 000000000..79ce5d0c2 --- /dev/null +++ b/src/Module/Special/Options.php @@ -0,0 +1,46 @@ +. + * + */ + +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 + * + * It's a special class which shouldn't be called directly + * + * @see Router::getModuleClass() + */ +class Options extends BaseModule +{ + protected function rawContent(array $request = []) + { + $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'); + $this->response->setStatus(204); + $this->response->setType(Response::TYPE_BLANK); + } +} 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) { 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/FixtureTest.php b/tests/FixtureTest.php index 17760c85d..4b7deb022 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; @@ -30,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 */ @@ -50,4 +61,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/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' + ], + ], ]; } 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 new file mode 100644 index 000000000..460435ae2 --- /dev/null +++ b/tests/src/Module/Special/OptionsTest.php @@ -0,0 +1,46 @@ +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()); + self::assertEquals('No Content', $response->getReasonPhrase()); + self::assertEquals([ + 'Allow' => [implode(',', Router::ALLOWED_METHODS)], + ICanCreateResponses::X_HEADER => ['blank'], + ], $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 => ['blank'], + ], $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')); + } +}