Browse Source

Support "*" mapping in MockMvc filter registration

Prior to this commit, configuring a Servlet filter in MockMvc with a
defined mapping would only consider "/*" as a catch-all pattern.

This commit relaxes this rule by also accepting "*" mappings.

Closes gh-28041
pull/31469/head
Chen Jianbin 3 years ago committed by Brian Clozel
parent
commit
4bf995fa8b
  1. 8
      spring-test/src/main/java/org/springframework/test/web/servlet/setup/MockMvcFilterDecorator.java
  2. 2
      spring-test/src/test/java/org/springframework/test/web/servlet/setup/MockMvcFilterDecoratorTests.java

8
spring-test/src/main/java/org/springframework/test/web/servlet/setup/MockMvcFilterDecorator.java

@ -47,9 +47,11 @@ import org.springframework.web.util.UrlPathHelper; @@ -47,9 +47,11 @@ import org.springframework.web.util.UrlPathHelper;
*/
final class MockMvcFilterDecorator implements Filter {
private static final String EXTENSION_MAPPING_PATTERN = "*.";
private static final String ALL_MAPPING_PATTERN = "*";
private static final String PATH_MAPPING_PATTERN = "/*";
private static final String EXTENSION_MAPPING_PATTERN = ALL_MAPPING_PATTERN + ".";
private static final String PATH_MAPPING_PATTERN = "/" + ALL_MAPPING_PATTERN;
private final Filter delegate;
@ -103,7 +105,7 @@ final class MockMvcFilterDecorator implements Filter { @@ -103,7 +105,7 @@ final class MockMvcFilterDecorator implements Filter {
if (urlPattern.startsWith(EXTENSION_MAPPING_PATTERN)) {
this.endsWithMatches.add(urlPattern.substring(1));
}
else if (urlPattern.equals(PATH_MAPPING_PATTERN)) {
else if (urlPattern.equals(PATH_MAPPING_PATTERN) || urlPattern.equals(ALL_MAPPING_PATTERN)) {
this.startsWithMatches.add("");
}
else if (urlPattern.endsWith(PATH_MAPPING_PATTERN)) {

2
spring-test/src/test/java/org/springframework/test/web/servlet/setup/MockMvcFilterDecoratorTests.java

@ -95,7 +95,7 @@ public class MockMvcFilterDecoratorTests { @@ -95,7 +95,7 @@ public class MockMvcFilterDecoratorTests {
@Test
public void matchPathMappingAll() throws Exception {
assertFilterInvoked("/test", "/*");
assertFilterInvoked("/test", "*");
}
@Test

Loading…
Cancel
Save