|
| 1 | +<?hh // strict |
| 2 | +/* |
| 3 | + * Copyright (c) 2015-present, Facebook, Inc. |
| 4 | + * All rights reserved. |
| 5 | + * |
| 6 | + * This source code is licensed under the MIT license found in the |
| 7 | + * LICENSE file in the root directory of this source tree. |
| 8 | + * |
| 9 | + */ |
| 10 | + |
| 11 | +namespace Facebook\HackRouter; |
| 12 | + |
| 13 | +use function Facebook\FBExpect\expect; |
| 14 | +use type Facebook\HackRouter\Tests\NoHeadGetRewriteRouter; |
| 15 | +use type Facebook\HackTest\DataProvider; |
| 16 | + |
| 17 | +final class RouterHeadToGetDisabledTest extends \Facebook\HackTest\HackTest { |
| 18 | + public function getAllResolvers(): vec<( |
| 19 | + string, |
| 20 | + (function(dict<HttpMethod, dict<string, string>>): IResolver<string>), |
| 21 | + )> { |
| 22 | + return vec[ |
| 23 | + tuple('simple regexp', $map ==> new SimpleRegexpResolver($map)), |
| 24 | + tuple('prefix matching', PrefixMatchingResolver::fromFlatMap<>), |
| 25 | + ]; |
| 26 | + } |
| 27 | + |
| 28 | + <<DataProvider('getAllResolvers')>> |
| 29 | + public function testMethodNotAllowedResponses( |
| 30 | + string $_name, |
| 31 | + (function( |
| 32 | + dict<HttpMethod, dict<string, string>>, |
| 33 | + ): IResolver<string>) $factory, |
| 34 | + ): void { |
| 35 | + $map = dict[ |
| 36 | + HttpMethod::GET => dict['/get' => 'get'], |
| 37 | + HttpMethod::HEAD => dict['/head' => 'head'], |
| 38 | + ]; |
| 39 | + |
| 40 | + $router = $this->getRouter()->setResolver($factory($map)); |
| 41 | + |
| 42 | + // GET -> HEAD ( no re-routing ), deviating from the default behavior. |
| 43 | + $e = expect(() ==> $router->routeMethodAndPath(HttpMethod::HEAD, '/get')) |
| 44 | + ->toThrow(MethodNotAllowedException::class); |
| 45 | + expect($e->getAllowedMethods())->toBeSame(keyset[HttpMethod::GET]); |
| 46 | + |
| 47 | + // GET -> HEAD |
| 48 | + $e = expect(() ==> $router->routeMethodAndPath(HttpMethod::GET, '/head')) |
| 49 | + ->toThrow(MethodNotAllowedException::class); |
| 50 | + expect($e->getAllowedMethods())->toBeSame(keyset[HttpMethod::HEAD]); |
| 51 | + } |
| 52 | + |
| 53 | + private function getRouter(): NoHeadGetRewriteRouter<string> { |
| 54 | + return new NoHeadGetRewriteRouter(dict[]); |
| 55 | + } |
| 56 | +} |
0 commit comments