18
18
use type Facebook\Experimental\Http\Message\HTTPMethod ;
19
19
20
20
final 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
+ ];
39
38
40
39
public function expectedMatches (
41
40
): varray <(string , string , dict <string , string >)> {
@@ -124,35 +123,45 @@ public function expectedMatchesWithResolvers(
124
123
<<DataProvider (' getAllResolvers' )>>
125
124
public function testMethodNotAllowedResponses (
126
125
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 ,
129
129
): void {
130
130
$map = dict [
131
131
HttpMethod :: GET => dict [
132
- ' getonly ' => ' getonly ' ,
132
+ ' /get ' => ' get ' ,
133
133
],
134
134
HttpMethod :: HEAD => dict [
135
- ' headonly ' => ' headonly ' ,
135
+ ' /head ' => ' head ' ,
136
136
],
137
137
HttpMethod :: POST => dict [
138
- ' postonly ' => ' postonly ' ,
138
+ ' /post ' => ' post ' ,
139
139
],
140
140
];
141
141
142
142
$router = $this -> getRouter()-> setResolver($factory ($map ));
143
143
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' ,
155
148
);
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 ]);
156
165
}
157
166
158
167
<<DataProvider (' expectedMatches' )>>
@@ -161,8 +170,8 @@ public function testMatchesPattern(
161
170
string $expected_responder ,
162
171
dict <string , string > $expected_data ,
163
172
): 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 );
166
175
expect ($actual_responder )-> toBeSame($expected_responder );
167
176
expect (dict ($actual_data ))-> toBeSame($expected_data );
168
177
}
@@ -199,8 +208,10 @@ public function testRequestResponseInterfacesSupport(
199
208
dict <string , string > $_expected_data ,
200
209
): void {
201
210
$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
+ );
204
215
205
216
expect ($path [0 ])-> toBeSame(' /' );
206
217
@@ -217,21 +228,20 @@ public function testRequestResponseInterfacesSupport(
217
228
<<DataProvider (' getAllResolvers' )>>
218
229
public function testNotFound (
219
230
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 ,
222
234
): void {
223
235
$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 );
227
238
228
239
$router = $this -> getRouter()
229
240
-> setResolver($factory (dict [
230
241
HttpMethod :: GET => dict [' /foo' => ' /foo' ],
231
242
]));
232
- expect (() ==> $router -> routeMethodAndPath(HttpMethod :: GET , ' /__404' ))-> toThrow(
233
- NotFoundException :: class ,
234
- );
243
+ expect (() ==> $router -> routeMethodAndPath(HttpMethod :: GET , ' /__404' ))
244
+ -> toThrow(NotFoundException :: class );
235
245
}
236
246
237
247
public function testMethodNotAllowed (): void {
0 commit comments