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

Commit

Permalink
Write tests for HEAD -/-> GET rewrite option
Browse files Browse the repository at this point in the history
  • Loading branch information
lexidor committed May 25, 2023
1 parent 8c86992 commit 3c0d7d9
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
56 changes: 56 additions & 0 deletions tests/RouterHeadToGetDisabledTest.php
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[]);
}
}
43 changes: 43 additions & 0 deletions tests/lib/NoHeadGetRewriteRouter.php
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;
}
}

0 comments on commit 3c0d7d9

Please sign in to comment.