|
|
|
@ -27,6 +27,7 @@ import org.junit.jupiter.api.Named; |
|
|
|
import org.junit.jupiter.api.Test; |
|
|
|
import org.junit.jupiter.api.Test; |
|
|
|
|
|
|
|
|
|
|
|
import org.springframework.http.HttpMethod; |
|
|
|
import org.springframework.http.HttpMethod; |
|
|
|
|
|
|
|
import org.springframework.util.ObjectUtils; |
|
|
|
import org.springframework.util.PathMatcher; |
|
|
|
import org.springframework.util.PathMatcher; |
|
|
|
import org.springframework.web.servlet.HandlerInterceptor; |
|
|
|
import org.springframework.web.servlet.HandlerInterceptor; |
|
|
|
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor; |
|
|
|
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor; |
|
|
|
@ -52,23 +53,16 @@ class MappedInterceptorTests { |
|
|
|
return PathPatternsTestUtils.requestArguments(); |
|
|
|
return PathPatternsTestUtils.requestArguments(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private MockHttpServletRequest requestWithMethod(String method) { |
|
|
|
|
|
|
|
MockHttpServletRequest request = new MockHttpServletRequest(); |
|
|
|
|
|
|
|
request.setRequestURI("/some/path"); |
|
|
|
|
|
|
|
request.setMethod(method); |
|
|
|
|
|
|
|
ServletRequestPathUtils.parseAndCache(request); |
|
|
|
|
|
|
|
return request; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@PathPatternsParameterizedTest |
|
|
|
@PathPatternsParameterizedTest |
|
|
|
void noPatterns(Function<String, MockHttpServletRequest> requestFactory) { |
|
|
|
void noPatterns(Function<String, MockHttpServletRequest> requestFactory) { |
|
|
|
MappedInterceptor interceptor = new MappedInterceptor(null, null,null,null, delegate); |
|
|
|
MappedInterceptor interceptor = new MappedInterceptor(null, null, delegate); |
|
|
|
assertThat(interceptor.matches(requestFactory.apply("/foo"))).isTrue(); |
|
|
|
assertThat(interceptor.matches(requestFactory.apply("/foo"))).isTrue(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@PathPatternsParameterizedTest |
|
|
|
@PathPatternsParameterizedTest |
|
|
|
void includePattern(Function<String, MockHttpServletRequest> requestFactory) { |
|
|
|
void includePattern(Function<String, MockHttpServletRequest> requestFactory) { |
|
|
|
MappedInterceptor interceptor = new MappedInterceptor(new String[] { "/foo/*" }, null,null,null, delegate); |
|
|
|
MappedInterceptor interceptor = new MappedInterceptor(new String[] { "/foo/*" }, null, delegate); |
|
|
|
|
|
|
|
|
|
|
|
assertThat(interceptor.matches(requestFactory.apply("/foo/bar"))).isTrue(); |
|
|
|
assertThat(interceptor.matches(requestFactory.apply("/foo/bar"))).isTrue(); |
|
|
|
assertThat(interceptor.matches(requestFactory.apply("/bar/foo"))).isFalse(); |
|
|
|
assertThat(interceptor.matches(requestFactory.apply("/bar/foo"))).isFalse(); |
|
|
|
@ -76,13 +70,13 @@ class MappedInterceptorTests { |
|
|
|
|
|
|
|
|
|
|
|
@PathPatternsParameterizedTest |
|
|
|
@PathPatternsParameterizedTest |
|
|
|
void includePatternWithMatrixVariables(Function<String, MockHttpServletRequest> requestFactory) { |
|
|
|
void includePatternWithMatrixVariables(Function<String, MockHttpServletRequest> requestFactory) { |
|
|
|
MappedInterceptor interceptor = new MappedInterceptor(new String[] { "/foo*/*" },null,null, null, delegate); |
|
|
|
MappedInterceptor interceptor = new MappedInterceptor(new String[] { "/foo*/*" }, null, delegate); |
|
|
|
assertThat(interceptor.matches(requestFactory.apply("/foo;q=1/bar;s=2"))).isTrue(); |
|
|
|
assertThat(interceptor.matches(requestFactory.apply("/foo;q=1/bar;s=2"))).isTrue(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@PathPatternsParameterizedTest |
|
|
|
@PathPatternsParameterizedTest |
|
|
|
void excludePattern(Function<String, MockHttpServletRequest> requestFactory) { |
|
|
|
void excludePattern(Function<String, MockHttpServletRequest> requestFactory) { |
|
|
|
MappedInterceptor interceptor = new MappedInterceptor(null, new String[] { "/admin/**" },null,null, delegate); |
|
|
|
MappedInterceptor interceptor = new MappedInterceptor(null, new String[] { "/admin/**" }, delegate); |
|
|
|
|
|
|
|
|
|
|
|
assertThat(interceptor.matches(requestFactory.apply("/foo"))).isTrue(); |
|
|
|
assertThat(interceptor.matches(requestFactory.apply("/foo"))).isTrue(); |
|
|
|
assertThat(interceptor.matches(requestFactory.apply("/admin/foo"))).isFalse(); |
|
|
|
assertThat(interceptor.matches(requestFactory.apply("/admin/foo"))).isFalse(); |
|
|
|
@ -91,7 +85,7 @@ class MappedInterceptorTests { |
|
|
|
@PathPatternsParameterizedTest |
|
|
|
@PathPatternsParameterizedTest |
|
|
|
void includeAndExcludePatterns(Function<String, MockHttpServletRequest> requestFactory) { |
|
|
|
void includeAndExcludePatterns(Function<String, MockHttpServletRequest> requestFactory) { |
|
|
|
MappedInterceptor interceptor = |
|
|
|
MappedInterceptor interceptor = |
|
|
|
new MappedInterceptor(new String[] { "/**" }, new String[] { "/admin/**" },null,null, delegate); |
|
|
|
new MappedInterceptor(new String[] { "/**" }, new String[] { "/admin/**" }, delegate); |
|
|
|
|
|
|
|
|
|
|
|
assertThat(interceptor.matches(requestFactory.apply("/foo"))).isTrue(); |
|
|
|
assertThat(interceptor.matches(requestFactory.apply("/foo"))).isTrue(); |
|
|
|
assertThat(interceptor.matches(requestFactory.apply("/admin/foo"))).isFalse(); |
|
|
|
assertThat(interceptor.matches(requestFactory.apply("/admin/foo"))).isFalse(); |
|
|
|
@ -99,7 +93,7 @@ class MappedInterceptorTests { |
|
|
|
|
|
|
|
|
|
|
|
@PathPatternsParameterizedTest // gh-26690
|
|
|
|
@PathPatternsParameterizedTest // gh-26690
|
|
|
|
void includePatternWithFallbackOnPathMatcher(Function<String, MockHttpServletRequest> requestFactory) { |
|
|
|
void includePatternWithFallbackOnPathMatcher(Function<String, MockHttpServletRequest> requestFactory) { |
|
|
|
MappedInterceptor interceptor = new MappedInterceptor(new String[] { "/path1/**/path2" },null,null, null, delegate); |
|
|
|
MappedInterceptor interceptor = new MappedInterceptor(new String[] { "/path1/**/path2" }, null, delegate); |
|
|
|
|
|
|
|
|
|
|
|
assertThat(interceptor.matches(requestFactory.apply("/path1/foo/bar/path2"))).isTrue(); |
|
|
|
assertThat(interceptor.matches(requestFactory.apply("/path1/foo/bar/path2"))).isTrue(); |
|
|
|
assertThat(interceptor.matches(requestFactory.apply("/path1/foo/bar/path3"))).isFalse(); |
|
|
|
assertThat(interceptor.matches(requestFactory.apply("/path1/foo/bar/path3"))).isFalse(); |
|
|
|
@ -109,97 +103,65 @@ class MappedInterceptorTests { |
|
|
|
@SuppressWarnings("removal") |
|
|
|
@SuppressWarnings("removal") |
|
|
|
@PathPatternsParameterizedTest |
|
|
|
@PathPatternsParameterizedTest |
|
|
|
void customPathMatcher(Function<String, MockHttpServletRequest> requestFactory) { |
|
|
|
void customPathMatcher(Function<String, MockHttpServletRequest> requestFactory) { |
|
|
|
MappedInterceptor interceptor = new MappedInterceptor(new String[] { "/foo/[0-9]*" },null,null, null, delegate); |
|
|
|
MappedInterceptor interceptor = new MappedInterceptor(new String[] { "/foo/[0-9]*" }, null, delegate); |
|
|
|
interceptor.setPathMatcher(new TestPathMatcher()); |
|
|
|
interceptor.setPathMatcher(new TestPathMatcher()); |
|
|
|
|
|
|
|
|
|
|
|
assertThat(interceptor.matches(requestFactory.apply("/foo/123"))).isTrue(); |
|
|
|
assertThat(interceptor.matches(requestFactory.apply("/foo/123"))).isTrue(); |
|
|
|
assertThat(interceptor.matches(requestFactory.apply("/foo/bar"))).isFalse(); |
|
|
|
assertThat(interceptor.matches(requestFactory.apply("/foo/bar"))).isFalse(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
void includeMethods(){ |
|
|
|
|
|
|
|
MappedInterceptor interceptor = new MappedInterceptor(null, null,new HttpMethod[]{HttpMethod.GET},null, delegate); |
|
|
|
|
|
|
|
assertThat(interceptor.matches(requestWithMethod("GET"))).isTrue(); |
|
|
|
|
|
|
|
assertThat(interceptor.matches(requestWithMethod("HEAD"))).isFalse(); |
|
|
|
|
|
|
|
assertThat(interceptor.matches(requestWithMethod("POST"))).isFalse(); |
|
|
|
|
|
|
|
assertThat(interceptor.matches(requestWithMethod("PUT"))).isFalse(); |
|
|
|
|
|
|
|
assertThat(interceptor.matches(requestWithMethod("DELETE"))).isFalse(); |
|
|
|
|
|
|
|
assertThat(interceptor.matches(requestWithMethod("CONNECT"))).isFalse(); |
|
|
|
|
|
|
|
assertThat(interceptor.matches(requestWithMethod("OPTIONS"))).isFalse(); |
|
|
|
|
|
|
|
assertThat(interceptor.matches(requestWithMethod("TRACE"))).isFalse(); |
|
|
|
|
|
|
|
assertThat(interceptor.matches(requestWithMethod("PATCH"))).isFalse(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
void includeMultipleMethods(){ |
|
|
|
void includeMultipleMethods(){ |
|
|
|
MappedInterceptor interceptor = new MappedInterceptor(null, null,new HttpMethod[]{HttpMethod.GET,HttpMethod.POST},null, delegate); |
|
|
|
testHttpMethods( |
|
|
|
assertThat(interceptor.matches(requestWithMethod("GET"))).isTrue(); |
|
|
|
new HttpMethod[] {HttpMethod.GET, HttpMethod.POST}, |
|
|
|
assertThat(interceptor.matches(requestWithMethod("HEAD"))).isFalse(); |
|
|
|
new HttpMethod[] {}, |
|
|
|
assertThat(interceptor.matches(requestWithMethod("POST"))).isTrue(); |
|
|
|
"GET", "POST"); |
|
|
|
assertThat(interceptor.matches(requestWithMethod("PUT"))).isFalse(); |
|
|
|
|
|
|
|
assertThat(interceptor.matches(requestWithMethod("DELETE"))).isFalse(); |
|
|
|
|
|
|
|
assertThat(interceptor.matches(requestWithMethod("CONNECT"))).isFalse(); |
|
|
|
|
|
|
|
assertThat(interceptor.matches(requestWithMethod("OPTIONS"))).isFalse(); |
|
|
|
|
|
|
|
assertThat(interceptor.matches(requestWithMethod("TRACE"))).isFalse(); |
|
|
|
|
|
|
|
assertThat(interceptor.matches(requestWithMethod("PATCH"))).isFalse(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
void excludeMethods(){ |
|
|
|
void excludeMultipleMethods() { |
|
|
|
MappedInterceptor interceptor = new MappedInterceptor(null, null,null,new HttpMethod[]{HttpMethod.GET}, delegate); |
|
|
|
testHttpMethods( |
|
|
|
assertThat(interceptor.matches(requestWithMethod("GET"))).isFalse(); |
|
|
|
new HttpMethod[] {}, |
|
|
|
assertThat(interceptor.matches(requestWithMethod("HEAD"))).isTrue(); |
|
|
|
new HttpMethod[] {HttpMethod.GET, HttpMethod.POST, HttpMethod.OPTIONS}, |
|
|
|
assertThat(interceptor.matches(requestWithMethod("POST"))).isTrue(); |
|
|
|
"HEAD", "PUT", "DELETE", "TRACE", "PATCH"); |
|
|
|
assertThat(interceptor.matches(requestWithMethod("PUT"))).isTrue(); |
|
|
|
|
|
|
|
assertThat(interceptor.matches(requestWithMethod("DELETE"))).isTrue(); |
|
|
|
|
|
|
|
assertThat(interceptor.matches(requestWithMethod("CONNECT"))).isTrue(); |
|
|
|
|
|
|
|
assertThat(interceptor.matches(requestWithMethod("OPTIONS"))).isTrue(); |
|
|
|
|
|
|
|
assertThat(interceptor.matches(requestWithMethod("TRACE"))).isTrue(); |
|
|
|
|
|
|
|
assertThat(interceptor.matches(requestWithMethod("PATCH"))).isTrue(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
private void testHttpMethods(HttpMethod[] include, HttpMethod[] exclude, String... expected) { |
|
|
|
void excludeMultipleMethods(){ |
|
|
|
MappedInterceptor interceptor = new MappedInterceptor(null, null, include, exclude, delegate, null); |
|
|
|
MappedInterceptor interceptor = new MappedInterceptor(null, null,null,new HttpMethod[]{HttpMethod.GET,HttpMethod.POST,HttpMethod.OPTIONS}, delegate); |
|
|
|
for (HttpMethod httpMethod : HttpMethod.values()) { |
|
|
|
assertThat(interceptor.matches(requestWithMethod("GET"))).isFalse(); |
|
|
|
boolean matches = ObjectUtils.containsElement(expected, httpMethod.name()); |
|
|
|
assertThat(interceptor.matches(requestWithMethod("HEAD"))).isTrue(); |
|
|
|
assertThat(interceptor.matches(requestWithMethod(httpMethod.name()))) |
|
|
|
assertThat(interceptor.matches(requestWithMethod("POST"))).isFalse(); |
|
|
|
.as("Expected " + httpMethod + " to " + (matches ? "" : "not ") + "match") |
|
|
|
assertThat(interceptor.matches(requestWithMethod("PUT"))).isTrue(); |
|
|
|
.isEqualTo(matches); |
|
|
|
assertThat(interceptor.matches(requestWithMethod("DELETE"))).isTrue(); |
|
|
|
} |
|
|
|
assertThat(interceptor.matches(requestWithMethod("CONNECT"))).isTrue(); |
|
|
|
|
|
|
|
assertThat(interceptor.matches(requestWithMethod("OPTIONS"))).isFalse(); |
|
|
|
|
|
|
|
assertThat(interceptor.matches(requestWithMethod("TRACE"))).isTrue(); |
|
|
|
|
|
|
|
assertThat(interceptor.matches(requestWithMethod("PATCH"))).isTrue(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
private MockHttpServletRequest requestWithMethod(String method) { |
|
|
|
void includeMethodsAndExcludeMethods(){ |
|
|
|
MockHttpServletRequest request = new MockHttpServletRequest(); |
|
|
|
MappedInterceptor interceptor = new MappedInterceptor(null, null,new HttpMethod[]{HttpMethod.GET,HttpMethod.POST},new HttpMethod[]{HttpMethod.OPTIONS}, delegate); |
|
|
|
request.setRequestURI("/some/path"); |
|
|
|
assertThat(interceptor.matches(requestWithMethod("GET"))).isTrue(); |
|
|
|
request.setMethod(method); |
|
|
|
assertThat(interceptor.matches(requestWithMethod("HEAD"))).isFalse(); |
|
|
|
ServletRequestPathUtils.parseAndCache(request); |
|
|
|
assertThat(interceptor.matches(requestWithMethod("POST"))).isTrue(); |
|
|
|
return request; |
|
|
|
assertThat(interceptor.matches(requestWithMethod("PUT"))).isFalse(); |
|
|
|
|
|
|
|
assertThat(interceptor.matches(requestWithMethod("DELETE"))).isFalse(); |
|
|
|
|
|
|
|
assertThat(interceptor.matches(requestWithMethod("CONNECT"))).isFalse(); |
|
|
|
|
|
|
|
assertThat(interceptor.matches(requestWithMethod("OPTIONS"))).isFalse(); |
|
|
|
|
|
|
|
assertThat(interceptor.matches(requestWithMethod("TRACE"))).isFalse(); |
|
|
|
|
|
|
|
assertThat(interceptor.matches(requestWithMethod("PATCH"))).isFalse(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@PathPatternsParameterizedTest |
|
|
|
@PathPatternsParameterizedTest |
|
|
|
void includePatternAndIncludeMethods(Function<String, MockHttpServletRequest> requestFactory) { |
|
|
|
void includePatternAndHttMethods(Function<String, MockHttpServletRequest> requestFactory) { |
|
|
|
MappedInterceptor interceptor = new MappedInterceptor(new String[] { "/foo/*" }, null,new HttpMethod[]{HttpMethod.GET},null, delegate); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assertThat(interceptor.matches(requestFactory.apply("/foo/bar"))).isTrue(); |
|
|
|
MappedInterceptor getInterceptor = new MappedInterceptor( |
|
|
|
assertThat(interceptor.matches(requestFactory.apply("/bar/foo"))).isFalse(); |
|
|
|
new String[] {"/foo/*"}, null, new HttpMethod[] {HttpMethod.GET}, null, delegate, null); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
MappedInterceptor putInterceptor = new MappedInterceptor( |
|
|
|
|
|
|
|
new String[] {"/foo/*"}, null, new HttpMethod[] {HttpMethod.PUT}, null, delegate, null); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assertThat(getInterceptor.matches(requestFactory.apply("/foo/bar"))).isTrue(); |
|
|
|
|
|
|
|
assertThat(putInterceptor.matches(requestFactory.apply("/foo/bar"))).isFalse(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
void preHandle() throws Exception { |
|
|
|
void preHandle() throws Exception { |
|
|
|
HandlerInterceptor delegate = mock(); |
|
|
|
HandlerInterceptor delegate = mock(); |
|
|
|
|
|
|
|
|
|
|
|
new MappedInterceptor(null,null,null,null, delegate).preHandle(mock(), mock(), null); |
|
|
|
new MappedInterceptor(null,null, delegate).preHandle(mock(), mock(), null); |
|
|
|
|
|
|
|
|
|
|
|
then(delegate).should().preHandle(any(HttpServletRequest.class), any(HttpServletResponse.class), any()); |
|
|
|
then(delegate).should().preHandle(any(HttpServletRequest.class), any(HttpServletResponse.class), any()); |
|
|
|
} |
|
|
|
} |
|
|
|
@ -208,7 +170,7 @@ class MappedInterceptorTests { |
|
|
|
void postHandle() throws Exception { |
|
|
|
void postHandle() throws Exception { |
|
|
|
HandlerInterceptor delegate = mock(); |
|
|
|
HandlerInterceptor delegate = mock(); |
|
|
|
|
|
|
|
|
|
|
|
new MappedInterceptor(null,null,null,null, delegate).postHandle(mock(), mock(), null, mock()); |
|
|
|
new MappedInterceptor(null,null, delegate).postHandle(mock(), mock(), null, mock()); |
|
|
|
|
|
|
|
|
|
|
|
then(delegate).should().postHandle(any(), any(), any(), any()); |
|
|
|
then(delegate).should().postHandle(any(), any(), any(), any()); |
|
|
|
} |
|
|
|
} |
|
|
|
@ -217,7 +179,7 @@ class MappedInterceptorTests { |
|
|
|
void afterCompletion() throws Exception { |
|
|
|
void afterCompletion() throws Exception { |
|
|
|
HandlerInterceptor delegate = mock(); |
|
|
|
HandlerInterceptor delegate = mock(); |
|
|
|
|
|
|
|
|
|
|
|
new MappedInterceptor(null,null,null,null, delegate).afterCompletion(mock(), mock(), null, mock()); |
|
|
|
new MappedInterceptor(null,null, delegate).afterCompletion(mock(), mock(), null, mock()); |
|
|
|
|
|
|
|
|
|
|
|
then(delegate).should().afterCompletion(any(), any(), any(), any()); |
|
|
|
then(delegate).should().afterCompletion(any(), any(), any(), any()); |
|
|
|
} |
|
|
|
} |
|
|
|
|