26
26
import org .junit .jupiter .api .Named ;
27
27
import org .junit .jupiter .api .Test ;
28
28
29
+ import org .springframework .http .HttpMethod ;
29
30
import org .springframework .util .PathMatcher ;
30
31
import org .springframework .web .servlet .HandlerInterceptor ;
31
32
import org .springframework .web .servlet .i18n .LocaleChangeInterceptor ;
32
33
import org .springframework .web .testfixture .servlet .MockHttpServletRequest ;
34
+ import org .springframework .web .util .ServletRequestPathUtils ;
33
35
34
36
import static org .assertj .core .api .Assertions .assertThat ;
35
37
import static org .mockito .ArgumentMatchers .any ;
@@ -50,30 +52,37 @@ private static Stream<Named<Function<String, MockHttpServletRequest>>> pathPatte
50
52
return PathPatternsTestUtils .requestArguments ();
51
53
}
52
54
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
+ }
53
62
54
63
@ PathPatternsParameterizedTest
55
64
void noPatterns (Function <String , MockHttpServletRequest > requestFactory ) {
56
- MappedInterceptor interceptor = new MappedInterceptor (null , null , delegate );
65
+ MappedInterceptor interceptor = new MappedInterceptor (null , null ,null , null , delegate );
57
66
assertThat (interceptor .matches (requestFactory .apply ("/foo" ))).isTrue ();
58
67
}
59
68
60
69
@ PathPatternsParameterizedTest
61
70
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 );
63
72
64
73
assertThat (interceptor .matches (requestFactory .apply ("/foo/bar" ))).isTrue ();
65
74
assertThat (interceptor .matches (requestFactory .apply ("/bar/foo" ))).isFalse ();
66
75
}
67
76
68
77
@ PathPatternsParameterizedTest
69
78
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 );
71
80
assertThat (interceptor .matches (requestFactory .apply ("/foo;q=1/bar;s=2" ))).isTrue ();
72
81
}
73
82
74
83
@ PathPatternsParameterizedTest
75
84
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 );
77
86
78
87
assertThat (interceptor .matches (requestFactory .apply ("/foo" ))).isTrue ();
79
88
assertThat (interceptor .matches (requestFactory .apply ("/admin/foo" ))).isFalse ();
@@ -82,15 +91,15 @@ void excludePattern(Function<String, MockHttpServletRequest> requestFactory) {
82
91
@ PathPatternsParameterizedTest
83
92
void includeAndExcludePatterns (Function <String , MockHttpServletRequest > requestFactory ) {
84
93
MappedInterceptor interceptor =
85
- new MappedInterceptor (new String [] { "/**" }, new String [] { "/admin/**" }, delegate );
94
+ new MappedInterceptor (new String [] { "/**" }, new String [] { "/admin/**" },null , null , delegate );
86
95
87
96
assertThat (interceptor .matches (requestFactory .apply ("/foo" ))).isTrue ();
88
97
assertThat (interceptor .matches (requestFactory .apply ("/admin/foo" ))).isFalse ();
89
98
}
90
99
91
100
@ PathPatternsParameterizedTest // gh-26690
92
101
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 );
94
103
95
104
assertThat (interceptor .matches (requestFactory .apply ("/path1/foo/bar/path2" ))).isTrue ();
96
105
assertThat (interceptor .matches (requestFactory .apply ("/path1/foo/bar/path3" ))).isFalse ();
@@ -100,18 +109,97 @@ void includePatternWithFallbackOnPathMatcher(Function<String, MockHttpServletReq
100
109
@ SuppressWarnings ("removal" )
101
110
@ PathPatternsParameterizedTest
102
111
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 );
104
113
interceptor .setPathMatcher (new TestPathMatcher ());
105
114
106
115
assertThat (interceptor .matches (requestFactory .apply ("/foo/123" ))).isTrue ();
107
116
assertThat (interceptor .matches (requestFactory .apply ("/foo/bar" ))).isFalse ();
108
117
}
109
118
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
+
110
198
@ Test
111
199
void preHandle () throws Exception {
112
200
HandlerInterceptor delegate = mock ();
113
201
114
- new MappedInterceptor (null , delegate ).preHandle (mock (), mock (), null );
202
+ new MappedInterceptor (null ,null , null , null , delegate ).preHandle (mock (), mock (), null );
115
203
116
204
then (delegate ).should ().preHandle (any (HttpServletRequest .class ), any (HttpServletResponse .class ), any ());
117
205
}
@@ -120,7 +208,7 @@ void preHandle() throws Exception {
120
208
void postHandle () throws Exception {
121
209
HandlerInterceptor delegate = mock ();
122
210
123
- new MappedInterceptor (null , delegate ).postHandle (mock (), mock (), null , mock ());
211
+ new MappedInterceptor (null ,null , null , null , delegate ).postHandle (mock (), mock (), null , mock ());
124
212
125
213
then (delegate ).should ().postHandle (any (), any (), any (), any ());
126
214
}
@@ -129,7 +217,7 @@ void postHandle() throws Exception {
129
217
void afterCompletion () throws Exception {
130
218
HandlerInterceptor delegate = mock ();
131
219
132
- new MappedInterceptor (null , delegate ).afterCompletion (mock (), mock (), null , mock ());
220
+ new MappedInterceptor (null ,null , null , null , delegate ).afterCompletion (mock (), mock (), null , mock ());
133
221
134
222
then (delegate ).should ().afterCompletion (any (), any (), any (), any ());
135
223
}
0 commit comments