Support for direct path lookups in WebFlux

Closes gh-22961
This commit is contained in:
Rossen Stoyanchev
2020-07-09 11:26:04 +03:00
parent 47a3a5c970
commit 0584c289ab
7 changed files with 188 additions and 72 deletions

View File

@@ -17,6 +17,7 @@
package org.springframework.web.servlet.handler;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
@@ -51,7 +52,7 @@ import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
@SuppressWarnings("unused")
public class HandlerMethodMappingTests {
private AbstractHandlerMethodMapping<String> mapping;
private MyHandlerMethodMapping mapping;
private MyHandler handler;
@@ -78,13 +79,15 @@ public class HandlerMethodMappingTests {
@Test
public void directMatch() throws Exception {
String key = "foo";
this.mapping.registerMapping(key, this.handler, this.method1);
this.mapping.registerMapping("/foo", this.handler, this.method1);
this.mapping.registerMapping("/fo*", this.handler, this.method2);
MockHttpServletRequest request = new MockHttpServletRequest("GET", key);
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
HandlerMethod result = this.mapping.getHandlerInternal(request);
assertThat(result.getMethod()).isEqualTo(method1);
assertThat(request.getAttribute(HandlerMapping.BEST_MATCHING_HANDLER_ATTRIBUTE)).isEqualTo(result);
assertThat(this.mapping.getMatches()).containsExactly("/foo");
}
@Test
@@ -99,7 +102,7 @@ public class HandlerMethodMappingTests {
}
@Test
public void ambiguousMatch() throws Exception {
public void ambiguousMatch() {
this.mapping.registerMapping("/f?o", this.handler, this.method1);
this.mapping.registerMapping("/fo?", this.handler, this.method2);
@@ -127,7 +130,7 @@ public class HandlerMethodMappingTests {
}
@Test
public void registerMapping() throws Exception {
public void registerMapping() {
String key1 = "/foo";
String key2 = "/foo*";
@@ -136,7 +139,7 @@ public class HandlerMethodMappingTests {
// Direct URL lookup
List<String> directUrlMatches = this.mapping.getMappingRegistry().getMappingsByUrl(key1);
List<String> directUrlMatches = this.mapping.getMappingRegistry().getMappingsByDirectPath(key1);
assertThat(directUrlMatches).isNotNull();
assertThat(directUrlMatches.size()).isEqualTo(1);
assertThat(directUrlMatches.get(0)).isEqualTo(key1);
@@ -170,7 +173,7 @@ public class HandlerMethodMappingTests {
}
@Test
public void registerMappingWithSameMethodAndTwoHandlerInstances() throws Exception {
public void registerMappingWithSameMethodAndTwoHandlerInstances() {
String key1 = "foo";
String key2 = "bar";
@@ -186,7 +189,7 @@ public class HandlerMethodMappingTests {
// Direct URL lookup
List<String> directUrlMatches = this.mapping.getMappingRegistry().getMappingsByUrl(key1);
List<String> directUrlMatches = this.mapping.getMappingRegistry().getMappingsByDirectPath(key1);
assertThat(directUrlMatches).isNotNull();
assertThat(directUrlMatches.size()).isEqualTo(1);
assertThat(directUrlMatches.get(0)).isEqualTo(key1);
@@ -222,7 +225,7 @@ public class HandlerMethodMappingTests {
this.mapping.unregisterMapping(key);
assertThat(mapping.getHandlerInternal(new MockHttpServletRequest("GET", key))).isNull();
assertThat(this.mapping.getMappingRegistry().getMappingsByUrl(key)).isNull();
assertThat(this.mapping.getMappingRegistry().getMappingsByDirectPath(key)).isNull();
assertThat(this.mapping.getMappingRegistry().getHandlerMethodsByMappingName(this.method1.getName())).isNull();
assertThat(this.mapping.getMappingRegistry().getCorsConfiguration(handlerMethod)).isNull();
}
@@ -253,26 +256,30 @@ public class HandlerMethodMappingTests {
private PathMatcher pathMatcher = new AntPathMatcher();
private final List<String> matches = new ArrayList<>();
public MyHandlerMethodMapping() {
setHandlerMethodMappingNamingStrategy(new SimpleMappingNamingStrategy());
}
public List<String> getMatches() {
return this.matches;
}
@Override
protected boolean isHandler(Class<?> beanType) {
return true;
}
@Override
protected String getMappingForMethod(Method method, Class<?> handlerType) {
String methodName = method.getName();
return methodName.startsWith("handler") ? methodName : null;
protected Set<String> getDirectPaths(String mapping) {
return (pathMatcher.isPattern(mapping) ? Collections.emptySet() : Collections.singleton(mapping));
}
@Override
@SuppressWarnings("deprecation")
protected Set<String> getMappingPathPatterns(String key) {
return (this.pathMatcher.isPattern(key) ? Collections.<String>emptySet() : Collections.singleton(key));
protected String getMappingForMethod(Method method, Class<?> handlerType) {
String methodName = method.getName();
return methodName.startsWith("handler") ? methodName : null;
}
@Override
@@ -285,7 +292,11 @@ public class HandlerMethodMappingTests {
@Override
protected String getMatchingMapping(String pattern, HttpServletRequest request) {
String lookupPath = this.pathHelper.getLookupPathForRequest(request);
return this.pathMatcher.match(pattern, lookupPath) ? pattern : null;
String match = (this.pathMatcher.match(pattern, lookupPath) ? pattern : null);
if (match != null) {
this.matches.add(match);
}
return match;
}
@Override