Skip to content

Commit 173e11a

Browse files
committed
unit tests for HTTP method filtering in MappedInterceptor
- Verify inclusion of single and multiple HTTP methods - Verify exclusion of single and multiple HTTP methods - Test combined include and exclude HTTP method filters - Confirm correct matching behavior with URL path patterns - Validate interceptor lifecycle method delegation (preHandle, postHandle, afterCompletion) - All tests pass successfully, ensuring robust coverage of method filtering logic Signed-off-by: SRIRAM9487 <[email protected]>
1 parent 3b695ca commit 173e11a

File tree

1 file changed

+98
-10
lines changed

1 file changed

+98
-10
lines changed

spring-webmvc/src/test/java/org/springframework/web/servlet/handler/MappedInterceptorTests.java

Lines changed: 98 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,12 @@
2626
import org.junit.jupiter.api.Named;
2727
import org.junit.jupiter.api.Test;
2828

29+
import org.springframework.http.HttpMethod;
2930
import org.springframework.util.PathMatcher;
3031
import org.springframework.web.servlet.HandlerInterceptor;
3132
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
3233
import org.springframework.web.testfixture.servlet.MockHttpServletRequest;
34+
import org.springframework.web.util.ServletRequestPathUtils;
3335

3436
import static org.assertj.core.api.Assertions.assertThat;
3537
import static org.mockito.ArgumentMatchers.any;
@@ -50,30 +52,37 @@ private static Stream<Named<Function<String, MockHttpServletRequest>>> pathPatte
5052
return PathPatternsTestUtils.requestArguments();
5153
}
5254

55+
private MockHttpServletRequest requestWithMethod(String method) {
56+
MockHttpServletRequest request = new MockHttpServletRequest();
57+
request.setRequestURI("/some/path");
58+
request.setMethod(method);
59+
ServletRequestPathUtils.parseAndCache(request);
60+
return request;
61+
}
5362

5463
@PathPatternsParameterizedTest
5564
void noPatterns(Function<String, MockHttpServletRequest> requestFactory) {
56-
MappedInterceptor interceptor = new MappedInterceptor(null, null, delegate);
65+
MappedInterceptor interceptor = new MappedInterceptor(null, null,null,null, delegate);
5766
assertThat(interceptor.matches(requestFactory.apply("/foo"))).isTrue();
5867
}
5968

6069
@PathPatternsParameterizedTest
6170
void includePattern(Function<String, MockHttpServletRequest> requestFactory) {
62-
MappedInterceptor interceptor = new MappedInterceptor(new String[] { "/foo/*" }, null, delegate);
71+
MappedInterceptor interceptor = new MappedInterceptor(new String[] { "/foo/*" }, null,null,null, delegate);
6372

6473
assertThat(interceptor.matches(requestFactory.apply("/foo/bar"))).isTrue();
6574
assertThat(interceptor.matches(requestFactory.apply("/bar/foo"))).isFalse();
6675
}
6776

6877
@PathPatternsParameterizedTest
6978
void includePatternWithMatrixVariables(Function<String, MockHttpServletRequest> requestFactory) {
70-
MappedInterceptor interceptor = new MappedInterceptor(new String[] { "/foo*/*" }, null, delegate);
79+
MappedInterceptor interceptor = new MappedInterceptor(new String[] { "/foo*/*" },null,null, null, delegate);
7180
assertThat(interceptor.matches(requestFactory.apply("/foo;q=1/bar;s=2"))).isTrue();
7281
}
7382

7483
@PathPatternsParameterizedTest
7584
void excludePattern(Function<String, MockHttpServletRequest> requestFactory) {
76-
MappedInterceptor interceptor = new MappedInterceptor(null, new String[] { "/admin/**" }, delegate);
85+
MappedInterceptor interceptor = new MappedInterceptor(null, new String[] { "/admin/**" },null,null, delegate);
7786

7887
assertThat(interceptor.matches(requestFactory.apply("/foo"))).isTrue();
7988
assertThat(interceptor.matches(requestFactory.apply("/admin/foo"))).isFalse();
@@ -82,15 +91,15 @@ void excludePattern(Function<String, MockHttpServletRequest> requestFactory) {
8291
@PathPatternsParameterizedTest
8392
void includeAndExcludePatterns(Function<String, MockHttpServletRequest> requestFactory) {
8493
MappedInterceptor interceptor =
85-
new MappedInterceptor(new String[] { "/**" }, new String[] { "/admin/**" }, delegate);
94+
new MappedInterceptor(new String[] { "/**" }, new String[] { "/admin/**" },null,null, delegate);
8695

8796
assertThat(interceptor.matches(requestFactory.apply("/foo"))).isTrue();
8897
assertThat(interceptor.matches(requestFactory.apply("/admin/foo"))).isFalse();
8998
}
9099

91100
@PathPatternsParameterizedTest // gh-26690
92101
void includePatternWithFallbackOnPathMatcher(Function<String, MockHttpServletRequest> requestFactory) {
93-
MappedInterceptor interceptor = new MappedInterceptor(new String[] { "/path1/**/path2" }, null, delegate);
102+
MappedInterceptor interceptor = new MappedInterceptor(new String[] { "/path1/**/path2" },null,null, null, delegate);
94103

95104
assertThat(interceptor.matches(requestFactory.apply("/path1/foo/bar/path2"))).isTrue();
96105
assertThat(interceptor.matches(requestFactory.apply("/path1/foo/bar/path3"))).isFalse();
@@ -100,18 +109,97 @@ void includePatternWithFallbackOnPathMatcher(Function<String, MockHttpServletReq
100109
@SuppressWarnings("removal")
101110
@PathPatternsParameterizedTest
102111
void customPathMatcher(Function<String, MockHttpServletRequest> requestFactory) {
103-
MappedInterceptor interceptor = new MappedInterceptor(new String[] { "/foo/[0-9]*" }, null, delegate);
112+
MappedInterceptor interceptor = new MappedInterceptor(new String[] { "/foo/[0-9]*" },null,null, null, delegate);
104113
interceptor.setPathMatcher(new TestPathMatcher());
105114

106115
assertThat(interceptor.matches(requestFactory.apply("/foo/123"))).isTrue();
107116
assertThat(interceptor.matches(requestFactory.apply("/foo/bar"))).isFalse();
108117
}
109118

119+
@Test
120+
void includeMethods(){
121+
MappedInterceptor interceptor = new MappedInterceptor(null, null,new HttpMethod[]{HttpMethod.GET},null, delegate);
122+
assertThat(interceptor.matches(requestWithMethod("GET"))).isTrue();
123+
assertThat(interceptor.matches(requestWithMethod("HEAD"))).isFalse();
124+
assertThat(interceptor.matches(requestWithMethod("POST"))).isFalse();
125+
assertThat(interceptor.matches(requestWithMethod("PUT"))).isFalse();
126+
assertThat(interceptor.matches(requestWithMethod("DELETE"))).isFalse();
127+
assertThat(interceptor.matches(requestWithMethod("CONNECT"))).isFalse();
128+
assertThat(interceptor.matches(requestWithMethod("OPTIONS"))).isFalse();
129+
assertThat(interceptor.matches(requestWithMethod("TRACE"))).isFalse();
130+
assertThat(interceptor.matches(requestWithMethod("PATCH"))).isFalse();
131+
}
132+
133+
@Test
134+
void includeMultipleMethods(){
135+
MappedInterceptor interceptor = new MappedInterceptor(null, null,new HttpMethod[]{HttpMethod.GET,HttpMethod.POST},null, delegate);
136+
assertThat(interceptor.matches(requestWithMethod("GET"))).isTrue();
137+
assertThat(interceptor.matches(requestWithMethod("HEAD"))).isFalse();
138+
assertThat(interceptor.matches(requestWithMethod("POST"))).isTrue();
139+
assertThat(interceptor.matches(requestWithMethod("PUT"))).isFalse();
140+
assertThat(interceptor.matches(requestWithMethod("DELETE"))).isFalse();
141+
assertThat(interceptor.matches(requestWithMethod("CONNECT"))).isFalse();
142+
assertThat(interceptor.matches(requestWithMethod("OPTIONS"))).isFalse();
143+
assertThat(interceptor.matches(requestWithMethod("TRACE"))).isFalse();
144+
assertThat(interceptor.matches(requestWithMethod("PATCH"))).isFalse();
145+
}
146+
147+
@Test
148+
void excludeMethods(){
149+
MappedInterceptor interceptor = new MappedInterceptor(null, null,null,new HttpMethod[]{HttpMethod.GET}, delegate);
150+
assertThat(interceptor.matches(requestWithMethod("GET"))).isFalse();
151+
assertThat(interceptor.matches(requestWithMethod("HEAD"))).isTrue();
152+
assertThat(interceptor.matches(requestWithMethod("POST"))).isTrue();
153+
assertThat(interceptor.matches(requestWithMethod("PUT"))).isTrue();
154+
assertThat(interceptor.matches(requestWithMethod("DELETE"))).isTrue();
155+
assertThat(interceptor.matches(requestWithMethod("CONNECT"))).isTrue();
156+
assertThat(interceptor.matches(requestWithMethod("OPTIONS"))).isTrue();
157+
assertThat(interceptor.matches(requestWithMethod("TRACE"))).isTrue();
158+
assertThat(interceptor.matches(requestWithMethod("PATCH"))).isTrue();
159+
}
160+
161+
@Test
162+
void excludeMultipleMethods(){
163+
MappedInterceptor interceptor = new MappedInterceptor(null, null,null,new HttpMethod[]{HttpMethod.GET,HttpMethod.POST,HttpMethod.OPTIONS}, delegate);
164+
assertThat(interceptor.matches(requestWithMethod("GET"))).isFalse();
165+
assertThat(interceptor.matches(requestWithMethod("HEAD"))).isTrue();
166+
assertThat(interceptor.matches(requestWithMethod("POST"))).isFalse();
167+
assertThat(interceptor.matches(requestWithMethod("PUT"))).isTrue();
168+
assertThat(interceptor.matches(requestWithMethod("DELETE"))).isTrue();
169+
assertThat(interceptor.matches(requestWithMethod("CONNECT"))).isTrue();
170+
assertThat(interceptor.matches(requestWithMethod("OPTIONS"))).isFalse();
171+
assertThat(interceptor.matches(requestWithMethod("TRACE"))).isTrue();
172+
assertThat(interceptor.matches(requestWithMethod("PATCH"))).isTrue();
173+
}
174+
175+
@Test
176+
void includeMethodsAndExcludeMethods(){
177+
MappedInterceptor interceptor = new MappedInterceptor(null, null,new HttpMethod[]{HttpMethod.GET,HttpMethod.POST},new HttpMethod[]{HttpMethod.OPTIONS}, delegate);
178+
assertThat(interceptor.matches(requestWithMethod("GET"))).isTrue();
179+
assertThat(interceptor.matches(requestWithMethod("HEAD"))).isFalse();
180+
assertThat(interceptor.matches(requestWithMethod("POST"))).isTrue();
181+
assertThat(interceptor.matches(requestWithMethod("PUT"))).isFalse();
182+
assertThat(interceptor.matches(requestWithMethod("DELETE"))).isFalse();
183+
assertThat(interceptor.matches(requestWithMethod("CONNECT"))).isFalse();
184+
assertThat(interceptor.matches(requestWithMethod("OPTIONS"))).isFalse();
185+
assertThat(interceptor.matches(requestWithMethod("TRACE"))).isFalse();
186+
assertThat(interceptor.matches(requestWithMethod("PATCH"))).isFalse();
187+
}
188+
189+
@PathPatternsParameterizedTest
190+
void includePatternAndIncludeMethods(Function<String, MockHttpServletRequest> requestFactory) {
191+
MappedInterceptor interceptor = new MappedInterceptor(new String[] { "/foo/*" }, null,new HttpMethod[]{HttpMethod.GET},null, delegate);
192+
193+
assertThat(interceptor.matches(requestFactory.apply("/foo/bar"))).isTrue();
194+
assertThat(interceptor.matches(requestFactory.apply("/bar/foo"))).isFalse();
195+
}
196+
197+
110198
@Test
111199
void preHandle() throws Exception {
112200
HandlerInterceptor delegate = mock();
113201

114-
new MappedInterceptor(null, delegate).preHandle(mock(), mock(), null);
202+
new MappedInterceptor(null,null,null,null, delegate).preHandle(mock(), mock(), null);
115203

116204
then(delegate).should().preHandle(any(HttpServletRequest.class), any(HttpServletResponse.class), any());
117205
}
@@ -120,7 +208,7 @@ void preHandle() throws Exception {
120208
void postHandle() throws Exception {
121209
HandlerInterceptor delegate = mock();
122210

123-
new MappedInterceptor(null, delegate).postHandle(mock(), mock(), null, mock());
211+
new MappedInterceptor(null,null,null,null, delegate).postHandle(mock(), mock(), null, mock());
124212

125213
then(delegate).should().postHandle(any(), any(), any(), any());
126214
}
@@ -129,7 +217,7 @@ void postHandle() throws Exception {
129217
void afterCompletion() throws Exception {
130218
HandlerInterceptor delegate = mock();
131219

132-
new MappedInterceptor(null, delegate).afterCompletion(mock(), mock(), null, mock());
220+
new MappedInterceptor(null,null,null,null, delegate).afterCompletion(mock(), mock(), null, mock());
133221

134222
then(delegate).should().afterCompletion(any(), any(), any(), any());
135223
}

0 commit comments

Comments
 (0)