mirror of
https://github.com/spring-projects/spring-framework
synced 2026-06-08 17:33:33 +00:00
Update contribution
Closes gh-35273
This commit is contained in:
+30
-36
@@ -83,8 +83,9 @@ public class InterceptorRegistration {
|
||||
* @since 5.0.3
|
||||
*/
|
||||
public InterceptorRegistration addPathPatterns(List<String> patterns) {
|
||||
this.includePatterns = (this.includePatterns != null ?
|
||||
this.includePatterns : new ArrayList<>(patterns.size()));
|
||||
if (this.includePatterns == null) {
|
||||
this.includePatterns = new ArrayList<>(patterns.size());
|
||||
}
|
||||
this.includePatterns.addAll(patterns);
|
||||
return this;
|
||||
}
|
||||
@@ -105,16 +106,16 @@ public class InterceptorRegistration {
|
||||
* @since 5.0.3
|
||||
*/
|
||||
public InterceptorRegistration excludePathPatterns(List<String> patterns) {
|
||||
this.excludePatterns = (this.excludePatterns != null ?
|
||||
this.excludePatterns : new ArrayList<>(patterns.size()));
|
||||
if (this.excludePatterns == null) {
|
||||
this.excludePatterns = new ArrayList<>(patterns.size());
|
||||
}
|
||||
this.excludePatterns.addAll(patterns);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add HTTP methods the interceptor should be included for.
|
||||
* <p>Only requests with these HTTP methods will be intercepted.
|
||||
* @since 7.0.x
|
||||
* Add HTTP methods for requests the interceptor should be included in.
|
||||
* @since 7.0
|
||||
*/
|
||||
public InterceptorRegistration includeHttpMethods(HttpMethod... httpMethods) {
|
||||
return includeHttpMethods(Arrays.asList(httpMethods));
|
||||
@@ -122,31 +123,33 @@ public class InterceptorRegistration {
|
||||
|
||||
/**
|
||||
* List-based variant of {@link #includeHttpMethods(HttpMethod...)}.
|
||||
* @since 7.0.x
|
||||
* @since 7.0
|
||||
*/
|
||||
public InterceptorRegistration includeHttpMethods(List<HttpMethod> httpMethods) {
|
||||
this.includeHttpMethods = (this.includeHttpMethods != null ?
|
||||
this.includeHttpMethods : new ArrayList<>(httpMethods.size()));
|
||||
if (this.includeHttpMethods == null) {
|
||||
this.includeHttpMethods = new ArrayList<>(httpMethods.size());
|
||||
}
|
||||
this.includeHttpMethods.addAll(httpMethods);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add HTTP methods the interceptor should be excluded from.
|
||||
* Add HTTP methods for requests the interceptor should be excluded from.
|
||||
* <p>Requests with these HTTP methods will be ignored by the interceptor.
|
||||
* @since 7.0.x
|
||||
* @since 7.0
|
||||
*/
|
||||
public InterceptorRegistration excludeHttpMethods(HttpMethod... httpMethods){
|
||||
return this.excludeHttpMethods(Arrays.asList(httpMethods));
|
||||
public InterceptorRegistration excludeHttpMethods(HttpMethod... httpMethods) {
|
||||
return excludeHttpMethods(Arrays.asList(httpMethods));
|
||||
}
|
||||
|
||||
/**
|
||||
* List-based variant of {@link #excludeHttpMethods(HttpMethod...)}.
|
||||
* @since 7.0.x
|
||||
* @since 7.0
|
||||
*/
|
||||
public InterceptorRegistration excludeHttpMethods(List<HttpMethod> httpMethods){
|
||||
this.excludeHttpMethods = (this.excludeHttpMethods != null ?
|
||||
this.excludeHttpMethods : new ArrayList<>(httpMethods.size()));
|
||||
public InterceptorRegistration excludeHttpMethods(List<HttpMethod> httpMethods) {
|
||||
if (this.excludeHttpMethods == null) {
|
||||
this.excludeHttpMethods = new ArrayList<>(httpMethods.size());
|
||||
}
|
||||
this.excludeHttpMethods.addAll(httpMethods);
|
||||
return this;
|
||||
}
|
||||
@@ -175,7 +178,7 @@ public class InterceptorRegistration {
|
||||
* Specify an order position to be used. Default is 0.
|
||||
* @since 4.3.23
|
||||
*/
|
||||
public InterceptorRegistration order(int order){
|
||||
public InterceptorRegistration order(int order) {
|
||||
this.order = order;
|
||||
return this;
|
||||
}
|
||||
@@ -194,27 +197,18 @@ public class InterceptorRegistration {
|
||||
@SuppressWarnings("removal")
|
||||
protected Object getInterceptor() {
|
||||
|
||||
if (this.includePatterns == null && this.excludePatterns == null && this.includeHttpMethods == null && this.excludeHttpMethods == null) {
|
||||
if (this.includePatterns == null && this.excludePatterns == null &&
|
||||
this.includeHttpMethods == null && this.excludeHttpMethods == null) {
|
||||
|
||||
return this.interceptor;
|
||||
}
|
||||
|
||||
HttpMethod[] includeMethodsArray = (this.includeHttpMethods != null) ?
|
||||
this.includeHttpMethods.toArray(new HttpMethod[0]) : null;
|
||||
|
||||
HttpMethod[] excludeMethodsArray = (this.excludeHttpMethods != null) ?
|
||||
this.excludeHttpMethods.toArray(new HttpMethod[0]) : null;
|
||||
|
||||
String[] includePattersArray = StringUtils.toStringArray(this.includePatterns);
|
||||
|
||||
String[] excludePattersArray = StringUtils.toStringArray(this.excludePatterns);
|
||||
|
||||
|
||||
MappedInterceptor mappedInterceptor = new MappedInterceptor(
|
||||
includePattersArray,
|
||||
excludePattersArray,
|
||||
includeMethodsArray,
|
||||
excludeMethodsArray,
|
||||
this.interceptor);
|
||||
StringUtils.toStringArray(this.includePatterns),
|
||||
StringUtils.toStringArray(this.excludePatterns),
|
||||
(this.includeHttpMethods != null) ? this.includeHttpMethods.toArray(new HttpMethod[0]) : null,
|
||||
(this.excludeHttpMethods != null) ? this.excludeHttpMethods.toArray(new HttpMethod[0]) : null,
|
||||
this.interceptor, null);
|
||||
|
||||
if (this.pathMatcher != null) {
|
||||
mappedInterceptor.setPathMatcher(this.pathMatcher);
|
||||
|
||||
+48
-99
@@ -68,9 +68,9 @@ public final class MappedInterceptor implements HandlerInterceptor {
|
||||
|
||||
private final PatternAdapter @Nullable [] excludePatterns;
|
||||
|
||||
private final MethodAdapter @Nullable [] includeHttpMethods;
|
||||
private final HttpMethod @Nullable [] includeHttpMethods;
|
||||
|
||||
private final MethodAdapter @Nullable [] excludeHttpMethods;
|
||||
private final HttpMethod @Nullable [] excludeHttpMethods;
|
||||
|
||||
private PathMatcher pathMatcher = defaultPathMatcher;
|
||||
|
||||
@@ -78,28 +78,40 @@ public final class MappedInterceptor implements HandlerInterceptor {
|
||||
|
||||
|
||||
/**
|
||||
* Create an instance with the given include and exclude patterns along with
|
||||
* the target interceptor for the mappings.
|
||||
* @param includePatterns patterns to which requests must match, or null to
|
||||
* match all paths
|
||||
* @param excludePatterns patterns to which requests must not match
|
||||
* @param includeHttpMethods http methods to which request must match, or null to match all paths
|
||||
* @param excludeHttpMethods http methods to which request must not match
|
||||
* Create an instance with the given include and exclude patterns and HTTP methods.
|
||||
* @param includePatterns patterns to match, or null to match all paths
|
||||
* @param excludePatterns patterns for which requests must not match
|
||||
* @param includeHttpMethods the HTTP methods to match, or null for all methods
|
||||
* @param excludeHttpMethods the ßHTTP methods to which requests must not match
|
||||
* @param interceptor the target interceptor
|
||||
* @param parser a parser to use to pre-parse patterns into {@link PathPattern};
|
||||
* when not provided, {@link PathPatternParser#defaultInstance} is used.
|
||||
* @since 5.3
|
||||
* @since 7.0
|
||||
*/
|
||||
public MappedInterceptor(String @Nullable [] includePatterns, String @Nullable [] excludePatterns, HttpMethod @Nullable [] includeHttpMethods, HttpMethod @Nullable [] excludeHttpMethods,
|
||||
public MappedInterceptor(String @Nullable [] includePatterns, String @Nullable [] excludePatterns,
|
||||
HttpMethod @Nullable [] includeHttpMethods, HttpMethod @Nullable [] excludeHttpMethods,
|
||||
HandlerInterceptor interceptor, @Nullable PathPatternParser parser) {
|
||||
|
||||
this.includePatterns = PatternAdapter.initPatterns(includePatterns, parser);
|
||||
this.excludePatterns = PatternAdapter.initPatterns(excludePatterns, parser);
|
||||
this.includeHttpMethods = MethodAdapter.initHttpMethods(includeHttpMethods);
|
||||
this.excludeHttpMethods = MethodAdapter.initHttpMethods(excludeHttpMethods);
|
||||
this.includeHttpMethods = includeHttpMethods;
|
||||
this.excludeHttpMethods = excludeHttpMethods;
|
||||
this.interceptor = interceptor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Variation of
|
||||
* {@link #MappedInterceptor(String[], String[], HttpMethod[], HttpMethod[], HandlerInterceptor, PathPatternParser)}
|
||||
* without HTTP methods.
|
||||
* @since 5.3
|
||||
* @deprecated in favor of the constructor variant with HTTP methods
|
||||
*/
|
||||
@Deprecated(since = "7.0", forRemoval = true)
|
||||
public MappedInterceptor(String @Nullable [] includePatterns, String @Nullable [] excludePatterns,
|
||||
HandlerInterceptor interceptor, @Nullable PathPatternParser parser) {
|
||||
|
||||
this(includePatterns, excludePatterns, null, null, interceptor, parser);
|
||||
}
|
||||
|
||||
/**
|
||||
* Variant of
|
||||
@@ -107,16 +119,7 @@ public final class MappedInterceptor implements HandlerInterceptor {
|
||||
* with include patterns only.
|
||||
*/
|
||||
public MappedInterceptor(String @Nullable [] includePatterns, HandlerInterceptor interceptor) {
|
||||
this(includePatterns, null, null, null, interceptor);
|
||||
}
|
||||
|
||||
/**
|
||||
* Variant of
|
||||
* {@link #MappedInterceptor(String[], String[], HttpMethod[], HttpMethod[], HandlerInterceptor, PathPatternParser)}
|
||||
* with include methods only.
|
||||
*/
|
||||
public MappedInterceptor(HttpMethod @Nullable [] includeHttpMethods, HandlerInterceptor interceptor) {
|
||||
this(null, null, includeHttpMethods, null, interceptor);
|
||||
this(includePatterns, null, null, null, interceptor, null);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -124,10 +127,10 @@ public final class MappedInterceptor implements HandlerInterceptor {
|
||||
* {@link #MappedInterceptor(String[], String[], HttpMethod[], HttpMethod[], HandlerInterceptor, PathPatternParser)}
|
||||
* without a provided parser.
|
||||
*/
|
||||
public MappedInterceptor(String @Nullable [] includePatterns, String @Nullable [] excludePatterns, HttpMethod @Nullable [] includeHttpMethods, HttpMethod @Nullable [] excludeHttpMethods,
|
||||
public MappedInterceptor(String @Nullable [] includePatterns, String @Nullable [] excludePatterns,
|
||||
HandlerInterceptor interceptor) {
|
||||
|
||||
this(includePatterns, excludePatterns,includeHttpMethods,excludeHttpMethods, interceptor, null);
|
||||
this(includePatterns, excludePatterns, null, null, interceptor, null);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -136,26 +139,18 @@ public final class MappedInterceptor implements HandlerInterceptor {
|
||||
* with a {@link WebRequestInterceptor} as the target.
|
||||
*/
|
||||
public MappedInterceptor(String @Nullable [] includePatterns, WebRequestInterceptor interceptor) {
|
||||
this(includePatterns, null,null,null, interceptor);
|
||||
this(includePatterns, null, interceptor);
|
||||
}
|
||||
|
||||
/**
|
||||
* Variant of
|
||||
* {@link #MappedInterceptor(String[], String[], HttpMethod[], HttpMethod[], HandlerInterceptor, PathPatternParser)}
|
||||
* with a {@link WebRequestInterceptor} as the target.
|
||||
*/
|
||||
public MappedInterceptor(HttpMethod @Nullable [] includeHttpMethods, WebRequestInterceptor interceptor) {
|
||||
this(null, null,includeHttpMethods ,null, interceptor);
|
||||
}
|
||||
|
||||
/**
|
||||
* Variant of
|
||||
* {@link #MappedInterceptor(String[], String[], HttpMethod[], HttpMethod[] , HandlerInterceptor, PathPatternParser)}
|
||||
* with a {@link WebRequestInterceptor} as the target.
|
||||
*/
|
||||
public MappedInterceptor(String @Nullable [] includePatterns, String @Nullable [] excludePatterns, HttpMethod @Nullable [] includeHttpMethods, HttpMethod @Nullable [] excludeHttpMethods,
|
||||
public MappedInterceptor(String @Nullable [] includePatterns, String @Nullable [] excludePatterns,
|
||||
WebRequestInterceptor interceptor) {
|
||||
|
||||
this(includePatterns, excludePatterns,includeHttpMethods,excludeHttpMethods, new WebRequestHandlerInterceptorAdapter(interceptor));
|
||||
this(includePatterns, excludePatterns, null, null, new WebRequestHandlerInterceptorAdapter(interceptor), null);
|
||||
}
|
||||
|
||||
|
||||
@@ -228,7 +223,7 @@ public final class MappedInterceptor implements HandlerInterceptor {
|
||||
*/
|
||||
public boolean matches(HttpServletRequest request) {
|
||||
Object path = ServletRequestPathUtils.getCachedPath(request);
|
||||
HttpMethod method = HttpMethod.valueOf(request.getMethod());
|
||||
HttpMethod httpMethod = HttpMethod.valueOf(request.getMethod());
|
||||
if (this.pathMatcher != defaultPathMatcher) {
|
||||
path = path.toString();
|
||||
}
|
||||
@@ -241,33 +236,16 @@ public final class MappedInterceptor implements HandlerInterceptor {
|
||||
}
|
||||
}
|
||||
if (!ObjectUtils.isEmpty(this.excludeHttpMethods)) {
|
||||
for (MethodAdapter adapter : this.excludeHttpMethods) {
|
||||
if (adapter.match(method)){
|
||||
for (HttpMethod excluded : this.excludeHttpMethods) {
|
||||
if (excluded == httpMethod) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (ObjectUtils.isEmpty(this.includePatterns) && ObjectUtils.isEmpty(this.includeHttpMethods)) {
|
||||
return true;
|
||||
}
|
||||
if (!ObjectUtils.isEmpty(this.includePatterns) && ObjectUtils.isEmpty(this.includeHttpMethods)) {
|
||||
if (!ObjectUtils.isEmpty(this.includePatterns)) {
|
||||
boolean match = false;
|
||||
for (PatternAdapter adapter : this.includePatterns) {
|
||||
if (adapter.match(path, isPathContainer, this.pathMatcher)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!ObjectUtils.isEmpty(this.includeHttpMethods) && ObjectUtils.isEmpty(this.includePatterns)) {
|
||||
for (MethodAdapter adapter : this.includeHttpMethods) {
|
||||
if (adapter.match(method)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!ObjectUtils.isEmpty(this.includePatterns) && !ObjectUtils.isEmpty(this.includeHttpMethods)) {
|
||||
boolean match = false;
|
||||
for (MethodAdapter methodAdapter : this.includeHttpMethods) {
|
||||
if (methodAdapter.match(method)) {
|
||||
match = true;
|
||||
break;
|
||||
}
|
||||
@@ -275,13 +253,20 @@ public final class MappedInterceptor implements HandlerInterceptor {
|
||||
if (!match) {
|
||||
return false;
|
||||
}
|
||||
for (PatternAdapter pathAdapter : this.includePatterns) {
|
||||
if (pathAdapter.match(path, isPathContainer, pathMatcher)) {
|
||||
return true;
|
||||
}
|
||||
if (!ObjectUtils.isEmpty(this.includeHttpMethods)) {
|
||||
boolean match = false;
|
||||
for (HttpMethod included : this.includeHttpMethods) {
|
||||
if (included == httpMethod) {
|
||||
match = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!match) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -365,40 +350,4 @@ public final class MappedInterceptor implements HandlerInterceptor {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adapts {@link HttpMethod} instances for internal matching purposes.
|
||||
*
|
||||
* <p>Encapsulates an {@link HttpMethod} and provides matching functionality.
|
||||
* Also provides a utility method to initialize arrays of {@code MethodAdapter}
|
||||
* instances from arrays of {@link HttpMethod}.</p>
|
||||
*
|
||||
* @since 7.0.x
|
||||
*/
|
||||
private static class MethodAdapter {
|
||||
|
||||
private final @Nullable HttpMethod httpMethod;
|
||||
|
||||
public MethodAdapter(@Nullable HttpMethod httpMethod) {
|
||||
this.httpMethod = httpMethod;
|
||||
}
|
||||
|
||||
public boolean match(HttpMethod method) {
|
||||
return this.httpMethod == method;
|
||||
}
|
||||
|
||||
public @Nullable HttpMethod getHttpMethod() {
|
||||
return this.httpMethod;
|
||||
}
|
||||
|
||||
private static MethodAdapter @Nullable [] initHttpMethods(HttpMethod @Nullable [] methods) {
|
||||
if (ObjectUtils.isEmpty(methods)) {
|
||||
return null;
|
||||
}
|
||||
return Arrays.stream(methods)
|
||||
.map(MethodAdapter::new)
|
||||
.toArray(MethodAdapter[]::new);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+43
-81
@@ -27,6 +27,7 @@ import org.junit.jupiter.api.Named;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.util.PathMatcher;
|
||||
import org.springframework.web.servlet.HandlerInterceptor;
|
||||
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
|
||||
@@ -52,23 +53,16 @@ class MappedInterceptorTests {
|
||||
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
|
||||
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();
|
||||
}
|
||||
|
||||
@PathPatternsParameterizedTest
|
||||
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("/bar/foo"))).isFalse();
|
||||
@@ -76,13 +70,13 @@ class MappedInterceptorTests {
|
||||
|
||||
@PathPatternsParameterizedTest
|
||||
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();
|
||||
}
|
||||
|
||||
@PathPatternsParameterizedTest
|
||||
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("/admin/foo"))).isFalse();
|
||||
@@ -91,7 +85,7 @@ class MappedInterceptorTests {
|
||||
@PathPatternsParameterizedTest
|
||||
void includeAndExcludePatterns(Function<String, MockHttpServletRequest> requestFactory) {
|
||||
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("/admin/foo"))).isFalse();
|
||||
@@ -99,7 +93,7 @@ class MappedInterceptorTests {
|
||||
|
||||
@PathPatternsParameterizedTest // gh-26690
|
||||
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/path3"))).isFalse();
|
||||
@@ -109,97 +103,65 @@ class MappedInterceptorTests {
|
||||
@SuppressWarnings("removal")
|
||||
@PathPatternsParameterizedTest
|
||||
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());
|
||||
|
||||
assertThat(interceptor.matches(requestFactory.apply("/foo/123"))).isTrue();
|
||||
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
|
||||
void includeMultipleMethods(){
|
||||
MappedInterceptor interceptor = new MappedInterceptor(null, null,new HttpMethod[]{HttpMethod.GET,HttpMethod.POST},null, delegate);
|
||||
assertThat(interceptor.matches(requestWithMethod("GET"))).isTrue();
|
||||
assertThat(interceptor.matches(requestWithMethod("HEAD"))).isFalse();
|
||||
assertThat(interceptor.matches(requestWithMethod("POST"))).isTrue();
|
||||
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();
|
||||
testHttpMethods(
|
||||
new HttpMethod[] {HttpMethod.GET, HttpMethod.POST},
|
||||
new HttpMethod[] {},
|
||||
"GET", "POST");
|
||||
}
|
||||
|
||||
@Test
|
||||
void excludeMethods(){
|
||||
MappedInterceptor interceptor = new MappedInterceptor(null, null,null,new HttpMethod[]{HttpMethod.GET}, delegate);
|
||||
assertThat(interceptor.matches(requestWithMethod("GET"))).isFalse();
|
||||
assertThat(interceptor.matches(requestWithMethod("HEAD"))).isTrue();
|
||||
assertThat(interceptor.matches(requestWithMethod("POST"))).isTrue();
|
||||
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();
|
||||
void excludeMultipleMethods() {
|
||||
testHttpMethods(
|
||||
new HttpMethod[] {},
|
||||
new HttpMethod[] {HttpMethod.GET, HttpMethod.POST, HttpMethod.OPTIONS},
|
||||
"HEAD", "PUT", "DELETE", "TRACE", "PATCH");
|
||||
}
|
||||
|
||||
@Test
|
||||
void excludeMultipleMethods(){
|
||||
MappedInterceptor interceptor = new MappedInterceptor(null, null,null,new HttpMethod[]{HttpMethod.GET,HttpMethod.POST,HttpMethod.OPTIONS}, delegate);
|
||||
assertThat(interceptor.matches(requestWithMethod("GET"))).isFalse();
|
||||
assertThat(interceptor.matches(requestWithMethod("HEAD"))).isTrue();
|
||||
assertThat(interceptor.matches(requestWithMethod("POST"))).isFalse();
|
||||
assertThat(interceptor.matches(requestWithMethod("PUT"))).isTrue();
|
||||
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();
|
||||
private void testHttpMethods(HttpMethod[] include, HttpMethod[] exclude, String... expected) {
|
||||
MappedInterceptor interceptor = new MappedInterceptor(null, null, include, exclude, delegate, null);
|
||||
for (HttpMethod httpMethod : HttpMethod.values()) {
|
||||
boolean matches = ObjectUtils.containsElement(expected, httpMethod.name());
|
||||
assertThat(interceptor.matches(requestWithMethod(httpMethod.name())))
|
||||
.as("Expected " + httpMethod + " to " + (matches ? "" : "not ") + "match")
|
||||
.isEqualTo(matches);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void includeMethodsAndExcludeMethods(){
|
||||
MappedInterceptor interceptor = new MappedInterceptor(null, null,new HttpMethod[]{HttpMethod.GET,HttpMethod.POST},new HttpMethod[]{HttpMethod.OPTIONS}, delegate);
|
||||
assertThat(interceptor.matches(requestWithMethod("GET"))).isTrue();
|
||||
assertThat(interceptor.matches(requestWithMethod("HEAD"))).isFalse();
|
||||
assertThat(interceptor.matches(requestWithMethod("POST"))).isTrue();
|
||||
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();
|
||||
private MockHttpServletRequest requestWithMethod(String method) {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
request.setRequestURI("/some/path");
|
||||
request.setMethod(method);
|
||||
ServletRequestPathUtils.parseAndCache(request);
|
||||
return request;
|
||||
}
|
||||
|
||||
@PathPatternsParameterizedTest
|
||||
void includePatternAndIncludeMethods(Function<String, MockHttpServletRequest> requestFactory) {
|
||||
MappedInterceptor interceptor = new MappedInterceptor(new String[] { "/foo/*" }, null,new HttpMethod[]{HttpMethod.GET},null, delegate);
|
||||
void includePatternAndHttMethods(Function<String, MockHttpServletRequest> requestFactory) {
|
||||
|
||||
assertThat(interceptor.matches(requestFactory.apply("/foo/bar"))).isTrue();
|
||||
assertThat(interceptor.matches(requestFactory.apply("/bar/foo"))).isFalse();
|
||||
MappedInterceptor getInterceptor = new MappedInterceptor(
|
||||
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
|
||||
void preHandle() throws Exception {
|
||||
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());
|
||||
}
|
||||
@@ -208,7 +170,7 @@ class MappedInterceptorTests {
|
||||
void postHandle() throws Exception {
|
||||
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());
|
||||
}
|
||||
@@ -217,7 +179,7 @@ class MappedInterceptorTests {
|
||||
void afterCompletion() throws Exception {
|
||||
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());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user