Empty path mapping behaves consistently

An empty path mapping in an @RequestMapping now consistently matches to
empty paths regardless of whether there are both type and method level,
annotations, or method-level only.

Closes gh-22543
This commit is contained in:
Rossen Stoyanchev
2019-04-09 15:03:24 -04:00
parent f839c1f9cd
commit d1f888a7a9
17 changed files with 96 additions and 41 deletions

View File

@@ -115,9 +115,7 @@ public class PatternsRequestConditionTests {
assertEquals("/{foo}", match.getPatterns().iterator().next());
}
// SPR-8410
@Test
@Test // SPR-8410
public void matchSuffixPatternUsingFileExtensions() {
String[] patterns = new String[] {"/jobs/{jobName}"};
List<String> extensions = Arrays.asList("json");
@@ -183,6 +181,19 @@ public class PatternsRequestConditionTests {
assertNull(match);
}
@Test // gh-22543
public void matchWithEmptyPatterns() {
PatternsRequestCondition condition = new PatternsRequestCondition();
assertEquals(new PatternsRequestCondition(""), condition);
assertNotNull(condition.getMatchingCondition(new MockHttpServletRequest("GET", "")));
assertNull(condition.getMatchingCondition(new MockHttpServletRequest("GET", "/anything")));
condition = condition.combine(new PatternsRequestCondition());
assertEquals(new PatternsRequestCondition(""), condition);
assertNotNull(condition.getMatchingCondition(new MockHttpServletRequest("GET", "")));
assertNull(condition.getMatchingCondition(new MockHttpServletRequest("GET", "/anything")));
}
@Test
public void compareEqualPatterns() {
PatternsRequestCondition c1 = new PatternsRequestCondition("/foo*");

View File

@@ -295,14 +295,12 @@ public class RequestMappingInfoHandlerMappingTests {
assertEquals("/{path1}/2", request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE));
}
@Test
@Test // gh-22543
public void handleMatchBestMatchingPatternAttributeNoPatternsDefined() {
RequestMappingInfo key = RequestMappingInfo.paths().build();
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/1/2");
this.handlerMapping.handleMatch(key, "/1/2", request);
assertEquals("/1/2", request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE));
String path = "";
MockHttpServletRequest request = new MockHttpServletRequest("GET", path);
this.handlerMapping.handleMatch(RequestMappingInfo.paths().build(), path, request);
assertEquals(path, request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE));
}
@Test

View File

@@ -49,7 +49,7 @@ public class RequestMappingInfoTests {
public void createEmpty() {
RequestMappingInfo info = paths().build();
assertEquals(0, info.getPatternsCondition().getPatterns().size());
assertEquals(Collections.singleton(""), info.getPatternsCondition().getPatterns()); // gh-22543
assertEquals(0, info.getMethodsCondition().getMethods().size());
assertEquals(true, info.getConsumesCondition().isEmpty());
assertEquals(true, info.getProducesCondition().isEmpty());

View File

@@ -796,13 +796,19 @@ public class ServletAnnotationControllerHandlerMethodTests extends AbstractServl
.hasMessageContaining("Ambiguous mapping");
}
@Test
@Test // gh-22543
public void unmappedPathMapping() throws Exception {
initServletWithControllers(UnmappedPathController.class);
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/bogus-unmapped");
MockHttpServletResponse response = new MockHttpServletResponse();
getServlet().service(request, response);
assertEquals(404, response.getStatus());
request = new MockHttpServletRequest("GET", "");
response = new MockHttpServletResponse();
getServlet().service(request, response);
assertEquals(200, response.getStatus());
assertEquals("get", response.getContentAsString());
}