1818use type Facebook\Experimental\Http\Message\HTTPMethod ;
1919
2020final class RouterTest extends \Facebook \HackTest \ HackTest {
21- const keyset < string >
22- MAP = keyset [
23- ' /foo' ,
24- ' /foo/' ,
25- ' /foo/bar' ,
26- ' /foo/bar/{baz}' ,
27- ' /foo/{bar}' ,
28- ' /foo/{bar}/baz' ,
29- ' /foo/{bar}{baz:.+}' ,
30- ' /food/{noms}' ,
31- ' /bar/{herp:\\ d+}' ,
32- ' /bar/{herp}' ,
33- ' /unique/{foo}/bar' ,
34- ' /optional_suffix_[foo]' ,
35- ' /optional_suffix[/]' ,
36- ' /optional_suffixes/[herp[/derp]]' ,
37- ' /manual/en/{LegacyID}.php' ,
38- ];
21+ const keyset < string > MAP = keyset [
22+ ' /foo' ,
23+ ' /foo/' ,
24+ ' /foo/bar' ,
25+ ' /foo/bar/{baz}' ,
26+ ' /foo/{bar}' ,
27+ ' /foo/{bar}/baz' ,
28+ ' /foo/{bar}{baz:.+}' ,
29+ ' /food/{noms}' ,
30+ ' /bar/{herp:\\ d+}' ,
31+ ' /bar/{herp}' ,
32+ ' /unique/{foo}/bar' ,
33+ ' /optional_suffix_[foo]' ,
34+ ' /optional_suffix[/]' ,
35+ ' /optional_suffixes/[herp[/derp]]' ,
36+ ' /manual/en/{LegacyID}.php' ,
37+ ];
3938
4039 public function expectedMatches (
4140 ): varray <(string , string , dict <string , string >)> {
@@ -124,35 +123,45 @@ public function expectedMatchesWithResolvers(
124123 <<DataProvider (' getAllResolvers' )>>
125124 public function testMethodNotAllowedResponses (
126125 string $_name ,
127- (function (dict <HttpMethod , dict <string , string >>): IResolver <string >)
128- $factory ,
126+ (function (
127+ dict <HttpMethod , dict <string , string >>,
128+ ): IResolver <string >) $factory ,
129129 ): void {
130130 $map = dict [
131131 HttpMethod :: GET => dict [
132- ' getonly ' => ' getonly ' ,
132+ ' /get ' => ' get ' ,
133133 ],
134134 HttpMethod :: HEAD => dict [
135- ' headonly ' => ' headonly ' ,
135+ ' /head ' => ' head ' ,
136136 ],
137137 HttpMethod :: POST => dict [
138- ' postonly ' => ' postonly ' ,
138+ ' /post ' => ' post ' ,
139139 ],
140140 ];
141141
142142 $router = $this -> getRouter()-> setResolver($factory ($map ));
143143
144- list ($responder , $_data ) =
145- $router -> routeMethodAndPath(HttpMethod :: HEAD , ' getonly' );
146- expect ($responder )-> toBeSame(' getonly' );
147- expect (() ==> $router -> routeMethodAndPath(HttpMethod :: GET , ' headonly' ))-> toThrow(
148- MethodNotAllowedException :: class ,
149- );
150- expect (() ==> $router -> routeMethodAndPath(HttpMethod :: HEAD , ' postonly' ))-> toThrow(
151- MethodNotAllowedException :: class ,
152- );
153- expect (() ==> $router -> routeMethodAndPath(HttpMethod :: GET , ' postonly' ))-> toThrow(
154- MethodNotAllowedException :: class ,
144+ // HEAD -> GET ( re-routing )
145+ list ($responder , $_data ) = $router -> routeMethodAndPath(
146+ HttpMethod :: HEAD ,
147+ ' /get' ,
155148 );
149+ expect ($responder )-> toBeSame(' get' );
150+
151+ // GET -> HEAD
152+ $e = expect (() ==> $router -> routeMethodAndPath(HttpMethod :: GET , ' /head' ))
153+ -> toThrow(MethodNotAllowedException :: class );
154+ expect ($e -> getAllowedMethods())-> toBeSame(keyset [HttpMethod :: HEAD ]);
155+
156+ // HEAD -> POST
157+ $e = expect (() ==> $router -> routeMethodAndPath(HttpMethod :: HEAD , ' /post' ))
158+ -> toThrow(MethodNotAllowedException :: class );
159+ expect ($e -> getAllowedMethods())-> toBeSame(keyset [HttpMethod :: POST ]);
160+
161+ // GET -> POST
162+ $e = expect (() ==> $router -> routeMethodAndPath(HttpMethod :: GET , ' /post' ))
163+ -> toThrow(MethodNotAllowedException :: class );
164+ expect ($e -> getAllowedMethods())-> toEqual(keyset [HttpMethod :: POST ]);
156165 }
157166
158167 <<DataProvider (' expectedMatches' )>>
@@ -161,8 +170,8 @@ public function testMatchesPattern(
161170 string $expected_responder ,
162171 dict <string , string > $expected_data ,
163172 ): void {
164- list ($actual_responder , $actual_data ) =
165- $this -> getRouter() -> routeMethodAndPath(HttpMethod :: GET , $in );
173+ list ($actual_responder , $actual_data ) = $this -> getRouter()
174+ -> routeMethodAndPath(HttpMethod :: GET , $in );
166175 expect ($actual_responder )-> toBeSame($expected_responder );
167176 expect (dict ($actual_data ))-> toBeSame($expected_data );
168177 }
@@ -199,8 +208,10 @@ public function testRequestResponseInterfacesSupport(
199208 dict <string , string > $_expected_data ,
200209 ): void {
201210 $router = $this -> getRouter();
202- list ($direct_responder , $direct_data ) =
203- $router -> routeMethodAndPath(HttpMethod :: GET , $path );
211+ list ($direct_responder , $direct_data ) = $router -> routeMethodAndPath(
212+ HttpMethod :: GET ,
213+ $path ,
214+ );
204215
205216 expect ($path [0 ])-> toBeSame(' /' );
206217
@@ -217,21 +228,20 @@ public function testRequestResponseInterfacesSupport(
217228 <<DataProvider (' getAllResolvers' )>>
218229 public function testNotFound (
219230 string $_resolver_name ,
220- (function (dict <HttpMethod , dict <string , string >>): IResolver <string >)
221- $factory ,
231+ (function (
232+ dict <HttpMethod , dict <string , string >>,
233+ ): IResolver <string >) $factory ,
222234 ): void {
223235 $router = $this -> getRouter()-> setResolver($factory (dict []));
224- expect (() ==> $router -> routeMethodAndPath(HttpMethod :: GET , ' /__404' ))-> toThrow(
225- NotFoundException :: class ,
226- );
236+ expect (() ==> $router -> routeMethodAndPath(HttpMethod :: GET , ' /__404' ))
237+ -> toThrow(NotFoundException :: class );
227238
228239 $router = $this -> getRouter()
229240 -> setResolver($factory (dict [
230241 HttpMethod :: GET => dict [' /foo' => ' /foo' ],
231242 ]));
232- expect (() ==> $router -> routeMethodAndPath(HttpMethod :: GET , ' /__404' ))-> toThrow(
233- NotFoundException :: class ,
234- );
243+ expect (() ==> $router -> routeMethodAndPath(HttpMethod :: GET , ' /__404' ))
244+ -> toThrow(NotFoundException :: class );
235245 }
236246
237247 public function testMethodNotAllowed (): void {
0 commit comments