|
29 | 29 | import org.junit.jupiter.api.extension.ExtendWith;
|
30 | 30 |
|
31 | 31 | import org.springframework.core.io.ClassPathResource;
|
| 32 | +import org.springframework.core.io.FileSystemResource; |
32 | 33 | import org.springframework.core.io.Resource;
|
33 | 34 | import org.springframework.core.io.UrlResource;
|
34 | 35 | import org.springframework.http.HttpMethod;
|
|
38 | 39 | import org.springframework.web.HttpRequestMethodNotSupportedException;
|
39 | 40 | import org.springframework.web.accept.ContentNegotiationManager;
|
40 | 41 | import org.springframework.web.accept.ContentNegotiationManagerFactoryBean;
|
| 42 | +import org.springframework.web.context.support.StaticWebApplicationContext; |
41 | 43 | import org.springframework.web.servlet.HandlerMapping;
|
42 | 44 | import org.springframework.web.testfixture.servlet.MockHttpServletRequest;
|
43 | 45 | import org.springframework.web.testfixture.servlet.MockHttpServletResponse;
|
@@ -723,6 +725,25 @@ public void ignoreLastModified() throws Exception {
|
723 | 725 | assertThat(this.response.getContentAsString()).isEqualTo("h1 { color:red; }");
|
724 | 726 | }
|
725 | 727 |
|
| 728 | + @Test |
| 729 | + public void servletContextRootValidation() { |
| 730 | + StaticWebApplicationContext context = new StaticWebApplicationContext() { |
| 731 | + @Override |
| 732 | + public Resource getResource(String location) { |
| 733 | + return new FileSystemResource("/"); |
| 734 | + } |
| 735 | + }; |
| 736 | + |
| 737 | + ResourceHttpRequestHandler handler = new ResourceHttpRequestHandler(); |
| 738 | + handler.setLocationValues(Collections.singletonList("/")); |
| 739 | + handler.setApplicationContext(context); |
| 740 | + |
| 741 | + assertThatIllegalStateException().isThrownBy(handler::afterPropertiesSet) |
| 742 | + .withMessage("The String-based location \"/\" should be relative to the web application root but " + |
| 743 | + "resolved to a Resource of type: class org.springframework.core.io.FileSystemResource. " + |
| 744 | + "If this is intentional, please pass it as a pre-configured Resource via setLocations."); |
| 745 | + } |
| 746 | + |
726 | 747 |
|
727 | 748 | private long resourceLastModified(String resourceName) throws IOException {
|
728 | 749 | return new ClassPathResource(resourceName, getClass()).getFile().lastModified();
|
|
0 commit comments