39
39
import org .springframework .http .MediaType ;
40
40
import org .springframework .mock .http .server .reactive .test .MockServerHttpRequest ;
41
41
import org .springframework .stereotype .Controller ;
42
+ import org .springframework .util .ClassUtils ;
42
43
import org .springframework .util .MultiValueMap ;
43
44
import org .springframework .web .bind .annotation .GetMapping ;
44
45
import org .springframework .web .bind .annotation .PutMapping ;
60
61
import static org .junit .Assert .assertEquals ;
61
62
import static org .junit .Assert .assertNotNull ;
62
63
import static org .junit .Assert .assertNull ;
64
+ import static org .junit .Assert .assertSame ;
63
65
import static org .junit .Assert .assertThat ;
64
66
import static org .springframework .mock .http .server .reactive .test .MockServerHttpRequest .get ;
65
67
import static org .springframework .mock .http .server .reactive .test .MockServerHttpRequest .method ;
69
71
import static org .springframework .web .method .MvcAnnotationPredicates .getMapping ;
70
72
import static org .springframework .web .method .MvcAnnotationPredicates .requestMapping ;
71
73
import static org .springframework .web .method .ResolvableMethod .on ;
74
+ import static org .springframework .web .reactive .HandlerMapping .BEST_MATCHING_HANDLER_ATTRIBUTE ;
75
+ import static org .springframework .web .reactive .HandlerMapping .BEST_MATCHING_PATTERN_ATTRIBUTE ;
72
76
import static org .springframework .web .reactive .result .method .RequestMappingInfo .paths ;
73
77
74
78
/**
77
81
*/
78
82
public class RequestMappingInfoHandlerMappingTests {
79
83
84
+ private static final HandlerMethod handlerMethod = new HandlerMethod (new TestController (),
85
+ ClassUtils .getMethod (TestController .class , "dummy" ));
86
+
80
87
private TestRequestMappingInfoHandlerMapping handlerMapping ;
81
88
82
89
@@ -169,8 +176,8 @@ public void getHandlerTestInvalidContentType() throws Exception {
169
176
Mono <Object > mono = this .handlerMapping .getHandler (exchange );
170
177
171
178
assertError (mono , UnsupportedMediaTypeStatusException .class ,
172
- ex -> assertEquals ("Response status 415 with reason \" Invalid mime type \" bogus\" : does not contain '/' \" " ,
173
- ex .getMessage ()));
179
+ ex -> assertEquals ("Response status 415 with reason \" Invalid mime type \" bogus\" : " +
180
+ "does not contain '/' \" " , ex .getMessage ()));
174
181
}
175
182
176
183
@ Test // SPR-8462
@@ -219,7 +226,7 @@ public void handleMatchUriTemplateVariables() throws Exception {
219
226
String lookupPath = exchange .getRequest ().getPath ().pathWithinApplication ().value ();
220
227
221
228
RequestMappingInfo key = paths ("/{path1}/{path2}" ).build ();
222
- this .handlerMapping .handleMatch (key , lookupPath , exchange );
229
+ this .handlerMapping .handleMatch (key , handlerMethod , lookupPath , exchange );
223
230
224
231
String name = HandlerMapping .URI_TEMPLATE_VARIABLES_ATTRIBUTE ;
225
232
Map <String , String > uriVariables = (Map <String , String >) exchange .getAttributes ().get (name );
@@ -236,7 +243,7 @@ public void handleMatchUriTemplateVariablesDecode() throws Exception {
236
243
ServerWebExchange exchange = method (HttpMethod .GET , url ).toExchange ();
237
244
238
245
String lookupPath = exchange .getRequest ().getPath ().pathWithinApplication ().value ();
239
- this .handlerMapping .handleMatch (key , lookupPath , exchange );
246
+ this .handlerMapping .handleMatch (key , handlerMethod , lookupPath , exchange );
240
247
241
248
String name = HandlerMapping .URI_TEMPLATE_VARIABLES_ATTRIBUTE ;
242
249
@ SuppressWarnings ("unchecked" )
@@ -252,22 +259,23 @@ public void handleMatchBestMatchingPatternAttribute() throws Exception {
252
259
RequestMappingInfo key = paths ("/{path1}/2" , "/**" ).build ();
253
260
ServerWebExchange exchange = get ("/1/2" ).toExchange ();
254
261
String lookupPath = exchange .getRequest ().getPath ().pathWithinApplication ().value ();
255
- this .handlerMapping .handleMatch (key , lookupPath , exchange );
262
+ this .handlerMapping .handleMatch (key , handlerMethod , lookupPath , exchange );
256
263
257
- PathPattern bestMatch = (PathPattern ) exchange .getAttributes ()
258
- .get (HandlerMapping .BEST_MATCHING_PATTERN_ATTRIBUTE );
264
+ PathPattern bestMatch = (PathPattern ) exchange .getAttributes ().get (BEST_MATCHING_PATTERN_ATTRIBUTE );
259
265
assertEquals ("/{path1}/2" , bestMatch .getPatternString ());
266
+
267
+ HandlerMethod mapped = (HandlerMethod ) exchange .getAttributes ().get (BEST_MATCHING_HANDLER_ATTRIBUTE );
268
+ assertSame (handlerMethod , mapped );
260
269
}
261
270
262
271
@ Test
263
272
public void handleMatchBestMatchingPatternAttributeNoPatternsDefined () throws Exception {
264
273
RequestMappingInfo key = paths ().build ();
265
274
ServerWebExchange exchange = get ("/1/2" ).toExchange ();
266
275
String lookupPath = exchange .getRequest ().getPath ().pathWithinApplication ().value ();
267
- this .handlerMapping .handleMatch (key , lookupPath , exchange );
276
+ this .handlerMapping .handleMatch (key , handlerMethod , lookupPath , exchange );
268
277
269
- PathPattern bestMatch = (PathPattern ) exchange .getAttributes ()
270
- .get (HandlerMapping .BEST_MATCHING_PATTERN_ATTRIBUTE );
278
+ PathPattern bestMatch = (PathPattern ) exchange .getAttributes ().get (BEST_MATCHING_PATTERN_ATTRIBUTE );
271
279
assertEquals ("/1/2" , bestMatch .getPatternString ());
272
280
}
273
281
@@ -330,7 +338,6 @@ private <T> void assertError(Mono<Object> mono, final Class<T> exceptionClass, f
330
338
.consumeErrorWith (error -> {
331
339
assertEquals (exceptionClass , error .getClass ());
332
340
consumer .accept ((T ) error );
333
-
334
341
})
335
342
.verify ();
336
343
}
@@ -375,19 +382,19 @@ private void testMediaTypeNotAcceptable(String url) throws Exception {
375
382
private void handleMatch (ServerWebExchange exchange , String pattern ) {
376
383
RequestMappingInfo info = paths (pattern ).build ();
377
384
String lookupPath = exchange .getRequest ().getPath ().pathWithinApplication ().value ();
378
- this .handlerMapping .handleMatch (info , lookupPath , exchange );
385
+ this .handlerMapping .handleMatch (info , handlerMethod , lookupPath , exchange );
379
386
}
380
387
381
388
@ SuppressWarnings ("unchecked" )
382
389
private MultiValueMap <String , String > getMatrixVariables (ServerWebExchange exchange , String uriVarName ) {
383
- String attrName = HandlerMapping . MATRIX_VARIABLES_ATTRIBUTE ;
384
- return (( Map < String , MultiValueMap < String , String >>) exchange . getAttributes (). get (attrName )).get (uriVarName );
390
+ return (( Map < String , MultiValueMap < String , String >>) exchange . getAttributes ()
391
+ . get (HandlerMapping . MATRIX_VARIABLES_ATTRIBUTE )).get (uriVarName );
385
392
}
386
393
387
394
@ SuppressWarnings ("unchecked" )
388
395
private Map <String , String > getUriTemplateVariables (ServerWebExchange exchange ) {
389
- String attrName = HandlerMapping . URI_TEMPLATE_VARIABLES_ATTRIBUTE ;
390
- return ( Map < String , String >) exchange . getAttributes (). get (attrName );
396
+ return ( Map < String , String >) exchange . getAttributes ()
397
+ . get (HandlerMapping . URI_TEMPLATE_VARIABLES_ATTRIBUTE );
391
398
}
392
399
393
400
@@ -446,6 +453,8 @@ public HttpHeaders fooOptions() {
446
453
headers .add ("Allow" , "PUT,POST" );
447
454
return headers ;
448
455
}
456
+
457
+ public void dummy () { }
449
458
}
450
459
451
460
0 commit comments