Skip to content
This repository was archived by the owner on Jun 1, 2023. It is now read-only.

Commit 3c0d7d9

Browse files
committed
Write tests for HEAD -/-> GET rewrite option
1 parent 8c86992 commit 3c0d7d9

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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\Tests;
12+
13+
use type Facebook\HackRouter\{BaseRouter, HttpMethod, IResolver};
14+
15+
final class NoHeadGetRewriteRouter<T> extends BaseRouter<T> {
16+
public function __construct(
17+
private dict<string, T> $routes,
18+
private ?IResolver<T> $resolver = null,
19+
) {
20+
parent::__construct(shape('use_get_responder_for_head' => false));
21+
}
22+
23+
<<__Override>>
24+
protected function getRoutes(): ImmMap<HttpMethod, ImmMap<string, T>> {
25+
return ImmMap {
26+
HttpMethod::GET => new ImmMap($this->routes),
27+
};
28+
}
29+
30+
public function setResolver(IResolver<T> $resolver): this {
31+
$this->resolver = $resolver;
32+
return $this;
33+
}
34+
35+
<<__Override>>
36+
protected function getResolver(): IResolver<T> {
37+
$r = $this->resolver;
38+
if ($r === null) {
39+
return parent::getResolver();
40+
}
41+
return $r;
42+
}
43+
}

0 commit comments

Comments
 (0)