16
16
17
17
package org .springframework .web .servlet .resource ;
18
18
19
- import static org .junit .Assert .assertEquals ;
20
- import static org .junit .Assert .assertSame ;
21
- import static org .junit .Assert .assertTrue ;
22
-
23
19
import java .util .ArrayList ;
24
20
import java .util .Arrays ;
25
21
import java .util .List ;
26
-
27
22
import javax .servlet .http .HttpServletResponse ;
28
23
29
24
import org .junit .Before ;
30
25
import org .junit .Test ;
26
+
31
27
import org .springframework .core .io .ClassPathResource ;
32
28
import org .springframework .core .io .Resource ;
33
29
import org .springframework .core .io .UrlResource ;
37
33
import org .springframework .web .HttpRequestMethodNotSupportedException ;
38
34
import org .springframework .web .servlet .HandlerMapping ;
39
35
36
+ import static org .junit .Assert .*;
37
+
40
38
/**
41
39
* @author Keith Donald
42
40
* @author Jeremy Grelle
@@ -127,7 +125,6 @@ public void getResourceFromSubDirectoryOfAlternatePath() throws Exception {
127
125
assertEquals ("function foo() { console.log(\" hello world\" ); }" , response .getContentAsString ());
128
126
}
129
127
130
-
131
128
@ Test
132
129
public void invalidPath () throws Exception {
133
130
MockHttpServletRequest request = new MockHttpServletRequest ();
@@ -159,6 +156,18 @@ public void invalidPath() throws Exception {
159
156
testInvalidPath (location , "url:" + secretPath , request , response );
160
157
}
161
158
159
+ private void testInvalidPath (Resource location , String requestPath ,
160
+ MockHttpServletRequest request , MockHttpServletResponse response ) throws Exception {
161
+
162
+ request .setAttribute (HandlerMapping .PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE , requestPath );
163
+ response = new MockHttpServletResponse ();
164
+ this .handler .handleRequest (request , response );
165
+ if (!location .createRelative (requestPath ).exists () && !requestPath .contains (":" )) {
166
+ fail (requestPath + " doesn't actually exist as a relative path" );
167
+ }
168
+ assertEquals (404 , response .getStatus ());
169
+ }
170
+
162
171
@ Test
163
172
public void ignoreInvalidEscapeSequence () throws Exception {
164
173
MockHttpServletRequest request = new MockHttpServletRequest ();
@@ -192,7 +201,7 @@ public void processPath() throws Exception {
192
201
assertEquals ("/foo/bar" , this .handler .processPath (" // /// //// foo/bar" ));
193
202
assertEquals ("/foo/bar" , this .handler .processPath ((char ) 1 + " / " + (char ) 127 + " // foo/bar" ));
194
203
195
- // root ot empty path
204
+ // root or empty path
196
205
assertEquals ("" , this .handler .processPath (" " ));
197
206
assertEquals ("/" , this .handler .processPath ("/" ));
198
207
assertEquals ("/" , this .handler .processPath ("///" ));
@@ -243,15 +252,15 @@ public void missingResourcePath() throws Exception {
243
252
assertEquals (404 , response .getStatus ());
244
253
}
245
254
246
- @ Test (expected = IllegalStateException .class )
255
+ @ Test (expected = IllegalStateException .class )
247
256
public void noPathWithinHandlerMappingAttribute () throws Exception {
248
257
MockHttpServletRequest request = new MockHttpServletRequest ();
249
258
request .setMethod ("GET" );
250
259
MockHttpServletResponse response = new MockHttpServletResponse ();
251
260
handler .handleRequest (request , response );
252
261
}
253
262
254
- @ Test (expected = HttpRequestMethodNotSupportedException .class )
263
+ @ Test (expected = HttpRequestMethodNotSupportedException .class )
255
264
public void unsupportedHttpMethod () throws Exception {
256
265
MockHttpServletRequest request = new MockHttpServletRequest ();
257
266
request .setAttribute (HandlerMapping .PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE , "/foo.css" );
@@ -270,16 +279,6 @@ public void resourceNotFound() throws Exception {
270
279
assertEquals (404 , response .getStatus ());
271
280
}
272
281
273
- private void testInvalidPath (Resource location , String requestPath ,
274
- MockHttpServletRequest request , MockHttpServletResponse response ) throws Exception {
275
-
276
- request .setAttribute (HandlerMapping .PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE , requestPath );
277
- response = new MockHttpServletResponse ();
278
- this .handler .handleRequest (request , response );
279
- assertTrue (location .createRelative (requestPath ).exists ());
280
- assertEquals (404 , response .getStatus ());
281
- }
282
-
283
282
284
283
private static class TestServletContext extends MockServletContext {
285
284
0 commit comments