This repository has been archived by the owner on Jun 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Write tests for HEAD -/-> GET rewrite option
- Loading branch information
Showing
2 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?hh // strict | ||
/* | ||
* Copyright (c) 2015-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
*/ | ||
|
||
namespace Facebook\HackRouter; | ||
|
||
use function Facebook\FBExpect\expect; | ||
use type Facebook\HackRouter\Tests\NoHeadGetRewriteRouter; | ||
use type Facebook\HackTest\DataProvider; | ||
|
||
final class RouterHeadToGetDisabledTest extends \Facebook\HackTest\HackTest { | ||
public function getAllResolvers(): vec<( | ||
string, | ||
(function(dict<HttpMethod, dict<string, string>>): IResolver<string>), | ||
)> { | ||
return vec[ | ||
tuple('simple regexp', $map ==> new SimpleRegexpResolver($map)), | ||
tuple('prefix matching', PrefixMatchingResolver::fromFlatMap<>), | ||
]; | ||
} | ||
|
||
<<DataProvider('getAllResolvers')>> | ||
public function testMethodNotAllowedResponses( | ||
string $_name, | ||
(function( | ||
dict<HttpMethod, dict<string, string>>, | ||
): IResolver<string>) $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<string> { | ||
return new NoHeadGetRewriteRouter(dict[]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?hh // strict | ||
/* | ||
* Copyright (c) 2015-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
*/ | ||
|
||
namespace Facebook\HackRouter\Tests; | ||
|
||
use type Facebook\HackRouter\{BaseRouter, HttpMethod, IResolver}; | ||
|
||
final class NoHeadGetRewriteRouter<T> extends BaseRouter<T> { | ||
public function __construct( | ||
private dict<string, T> $routes, | ||
private ?IResolver<T> $resolver = null, | ||
) { | ||
parent::__construct(shape('use_get_responder_for_head' => false)); | ||
} | ||
|
||
<<__Override>> | ||
protected function getRoutes(): ImmMap<HttpMethod, ImmMap<string, T>> { | ||
return ImmMap { | ||
HttpMethod::GET => new ImmMap($this->routes), | ||
}; | ||
} | ||
|
||
public function setResolver(IResolver<T> $resolver): this { | ||
$this->resolver = $resolver; | ||
return $this; | ||
} | ||
|
||
<<__Override>> | ||
protected function getResolver(): IResolver<T> { | ||
$r = $this->resolver; | ||
if ($r === null) { | ||
return parent::getResolver(); | ||
} | ||
return $r; | ||
} | ||
} |