diff --git a/tests/RouterHeadToGetDisabledTest.php b/tests/RouterHeadToGetDisabledTest.php new file mode 100644 index 0000000..2859b18 --- /dev/null +++ b/tests/RouterHeadToGetDisabledTest.php @@ -0,0 +1,56 @@ +>): IResolver), + )> { + return vec[ + tuple('simple regexp', $map ==> new SimpleRegexpResolver($map)), + tuple('prefix matching', PrefixMatchingResolver::fromFlatMap<>), + ]; + } + + <> + public function testMethodNotAllowedResponses( + string $_name, + (function( + dict>, + ): IResolver) $factory, + ): void { + $map = dict[ + HttpMethod::GET => dict['/get' => 'get'], + HttpMethod::HEAD => dict['/head' => 'head'], + ]; + + $router = $this->getRouter()->setResolver($factory($map)); + + // GET -> HEAD ( no re-routing ), deviating from the default behavior. + $e = expect(() ==> $router->routeMethodAndPath(HttpMethod::HEAD, '/get')) + ->toThrow(MethodNotAllowedException::class); + expect($e->getAllowedMethods())->toBeSame(keyset[HttpMethod::GET]); + + // GET -> HEAD + $e = expect(() ==> $router->routeMethodAndPath(HttpMethod::GET, '/head')) + ->toThrow(MethodNotAllowedException::class); + expect($e->getAllowedMethods())->toBeSame(keyset[HttpMethod::HEAD]); + } + + private function getRouter(): NoHeadGetRewriteRouter { + return new NoHeadGetRewriteRouter(dict[]); + } +} diff --git a/tests/lib/NoHeadGetRewriteRouter.php b/tests/lib/NoHeadGetRewriteRouter.php new file mode 100644 index 0000000..ce24899 --- /dev/null +++ b/tests/lib/NoHeadGetRewriteRouter.php @@ -0,0 +1,43 @@ + extends BaseRouter { + public function __construct( + private dict $routes, + private ?IResolver $resolver = null, + ) { + parent::__construct(shape('use_get_responder_for_head' => false)); + } + + <<__Override>> + protected function getRoutes(): ImmMap> { + return ImmMap { + HttpMethod::GET => new ImmMap($this->routes), + }; + } + + public function setResolver(IResolver $resolver): this { + $this->resolver = $resolver; + return $this; + } + + <<__Override>> + protected function getResolver(): IResolver { + $r = $this->resolver; + if ($r === null) { + return parent::getResolver(); + } + return $r; + } +}