diff --git a/src/App.php b/src/App.php index ea5840df..fcb53bf8 100755 --- a/src/App.php +++ b/src/App.php @@ -744,15 +744,7 @@ public function run(Request $request, Response $response): static $response->disablePayload(); } - if (null === $route && null !== self::$wildcardRoute) { - $route = self::$wildcardRoute; - $path = \parse_url($request->getURI(), PHP_URL_PATH); - $route->path($path); - } - - if (null !== $route) { - return $this->execute($route, $request); - } elseif (self::REQUEST_METHOD_OPTIONS == $method) { + if(self::REQUEST_METHOD_OPTIONS == $method) { try { foreach ($groups as $group) { foreach (self::$options as $option) { // Group options hooks @@ -780,6 +772,19 @@ public function run(Request $request, Response $response): static } } } + + return $this; + } + + if (null === $route && null !== self::$wildcardRoute) { + $route = self::$wildcardRoute; + $this->route = $route; + $path = \parse_url($request->getURI(), PHP_URL_PATH); + $route->path($path); + } + + if (null !== $route) { + return $this->execute($route, $request); } else { foreach (self::$errors as $error) { // Global error hooks if (in_array('*', $error->getGroups())) { diff --git a/src/Validator/Text.php b/src/Validator/Text.php index 18e6b5ea..7379e844 100644 --- a/src/Validator/Text.php +++ b/src/Validator/Text.php @@ -42,7 +42,7 @@ class Text extends Validator * @param int $min * @param string[] $allowList */ - public function __construct(int $length, int $min = 1, array $allowList = []) + public function __construct(int $length, int $min = 0, array $allowList = []) { $this->length = $length; $this->min = $min; diff --git a/tests/AppTest.php b/tests/AppTest.php index 98ba0b02..f31a81a1 100755 --- a/tests/AppTest.php +++ b/tests/AppTest.php @@ -577,10 +577,36 @@ public function testWildcardRoute(): void $_SERVER['REQUEST_METHOD'] = 'GET'; $_SERVER['REQUEST_URI'] = '/unknown_path'; + App::init() + ->action(function () { + $route = $this->app->getRoute(); + App::setResource('myRoute', fn () => $route); + }); + + App::options() + ->inject('request') + ->inject('response') + ->action(function (Request $request, Response $response) { + $origin = $request->getOrigin(); + $response + ->addHeader('Server', 'Appwrite') + ->addHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, PATCH, DELETE') + ->addHeader('Access-Control-Allow-Headers', 'Origin, Cookie, Set-Cookie, X-Requested-With, Content-Type, Access-Control-Allow-Origin, Access-Control-Request-Headers, Accept, X-Appwrite-Project, X-Appwrite-Key, X-Appwrite-Locale, X-Appwrite-Mode, X-Appwrite-JWT, X-Appwrite-Response-Format, X-SDK-Version, X-SDK-Name, X-SDK-Language, X-SDK-Platform, X-Appwrite-ID, Content-Range, Range, Cache-Control, Expires, Pragma, X-Fallback-Cookies') + ->addHeader('Access-Control-Expose-Headers', 'X-Fallback-Cookies') + ->addHeader('Access-Control-Allow-Origin', $origin) + ->addHeader('Access-Control-Allow-Credentials', 'true') + ->noContent(); + }); + App::wildcard() + ->inject('myRoute') ->inject('response') - ->action(function ($response) { - $response->send('HELLO'); + ->action(function (mixed $myRoute, $response) { + if($myRoute == null) { + $response->send('ROUTE IS NULL!'); + } else { + $response->send('HELLO'); + } }); \ob_start(); @@ -588,9 +614,18 @@ public function testWildcardRoute(): void $result = \ob_get_contents(); \ob_end_clean(); + $this->assertEquals('HELLO', $result); + + \ob_start(); + $req = new Request(); + $req = $req->setMethod('OPTIONS'); + @$this->app->run($req, new Response()); + $result = \ob_get_contents(); + \ob_end_clean(); + + $this->assertEquals('', $result); + $_SERVER['REQUEST_METHOD'] = $method; $_SERVER['REQUEST_URI'] = $uri; - - $this->assertEquals('HELLO', $result); } } \ No newline at end of file