|
| 1 | +/* |
| 2 | + * Copyright 2002-2021 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.web.servlet.resource; |
| 17 | + |
| 18 | +import java.io.IOException; |
| 19 | +import java.net.MalformedURLException; |
| 20 | +import java.net.URI; |
| 21 | +import java.net.URL; |
| 22 | +import java.nio.charset.StandardCharsets; |
| 23 | +import java.util.stream.Stream; |
| 24 | + |
| 25 | +import javax.servlet.ServletException; |
| 26 | + |
| 27 | +import org.junit.jupiter.params.ParameterizedTest; |
| 28 | +import org.junit.jupiter.params.provider.Arguments; |
| 29 | +import org.junit.jupiter.params.provider.MethodSource; |
| 30 | + |
| 31 | +import org.springframework.core.io.ClassPathResource; |
| 32 | +import org.springframework.core.io.FileSystemResource; |
| 33 | +import org.springframework.core.io.UrlResource; |
| 34 | +import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; |
| 35 | +import org.springframework.web.servlet.DispatcherServlet; |
| 36 | +import org.springframework.web.servlet.config.annotation.EnableWebMvc; |
| 37 | +import org.springframework.web.servlet.config.annotation.PathMatchConfigurer; |
| 38 | +import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; |
| 39 | +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; |
| 40 | +import org.springframework.web.testfixture.servlet.MockHttpServletRequest; |
| 41 | +import org.springframework.web.testfixture.servlet.MockHttpServletResponse; |
| 42 | +import org.springframework.web.testfixture.servlet.MockServletConfig; |
| 43 | +import org.springframework.web.testfixture.servlet.MockServletContext; |
| 44 | +import org.springframework.web.util.UriUtils; |
| 45 | +import org.springframework.web.util.pattern.PathPatternParser; |
| 46 | + |
| 47 | +import static org.assertj.core.api.Assertions.assertThat; |
| 48 | +import static org.junit.jupiter.params.provider.Arguments.arguments; |
| 49 | + |
| 50 | +/** |
| 51 | + * Integration tests for static resource handling. |
| 52 | + * @author Rossen Stoyanchev |
| 53 | + */ |
| 54 | +public class ResourceHttpRequestHandlerIntegrationTests { |
| 55 | + |
| 56 | + private final MockServletContext servletContext = new MockServletContext(); |
| 57 | + |
| 58 | + private final MockServletConfig servletConfig = new MockServletConfig(this.servletContext); |
| 59 | + |
| 60 | + |
| 61 | + public static Stream<Arguments> argumentSource() { |
| 62 | + return Stream.of( |
| 63 | + arguments(true, "/cp"), |
| 64 | + arguments(true, "/fs"), |
| 65 | + arguments(true, "/url"), |
| 66 | + arguments(false, "/cp"), |
| 67 | + arguments(false, "/fs"), |
| 68 | + arguments(false, "/url") |
| 69 | + ); |
| 70 | + } |
| 71 | + |
| 72 | + |
| 73 | + @ParameterizedTest |
| 74 | + @MethodSource("argumentSource") |
| 75 | + void cssFile(boolean usePathPatterns, String pathPrefix) throws Exception { |
| 76 | + MockHttpServletRequest request = initRequest(pathPrefix + "/test/foo.css"); |
| 77 | + MockHttpServletResponse response = new MockHttpServletResponse(); |
| 78 | + |
| 79 | + DispatcherServlet servlet = initDispatcherServlet(usePathPatterns, WebConfig.class); |
| 80 | + servlet.service(request, response); |
| 81 | + |
| 82 | + String description = "usePathPattern=" + usePathPatterns + ", prefix=" + pathPrefix; |
| 83 | + assertThat(response.getStatus()).as(description).isEqualTo(200); |
| 84 | + assertThat(response.getContentType()).as(description).isEqualTo("text/css"); |
| 85 | + assertThat(response.getContentAsString()).as(description).isEqualTo("h1 { color:red; }"); |
| 86 | + } |
| 87 | + |
| 88 | + @ParameterizedTest |
| 89 | + @MethodSource("argumentSource") |
| 90 | + void classpathLocationWithEncodedPath(boolean usePathPatterns, String pathPrefix) throws Exception { |
| 91 | + MockHttpServletRequest request = initRequest(pathPrefix + "/test/фоо.css"); |
| 92 | + MockHttpServletResponse response = new MockHttpServletResponse(); |
| 93 | + |
| 94 | + DispatcherServlet servlet = initDispatcherServlet(usePathPatterns, WebConfig.class); |
| 95 | + servlet.service(request, response); |
| 96 | + |
| 97 | + String description = "usePathPattern=" + usePathPatterns + ", prefix=" + pathPrefix; |
| 98 | + assertThat(response.getStatus()).as(description).isEqualTo(200); |
| 99 | + assertThat(response.getContentType()).as(description).isEqualTo("text/css"); |
| 100 | + assertThat(response.getContentAsString()).as(description).isEqualTo("h1 { color:red; }"); |
| 101 | + } |
| 102 | + |
| 103 | + private DispatcherServlet initDispatcherServlet(boolean usePathPatterns, Class<?>... configClasses) |
| 104 | + throws ServletException { |
| 105 | + |
| 106 | + AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); |
| 107 | + context.register(configClasses); |
| 108 | + if (usePathPatterns) { |
| 109 | + context.register(PathPatternParserConfig.class); |
| 110 | + } |
| 111 | + context.setServletConfig(this.servletConfig); |
| 112 | + context.refresh(); |
| 113 | + |
| 114 | + DispatcherServlet servlet = new DispatcherServlet(); |
| 115 | + servlet.setApplicationContext(context); |
| 116 | + servlet.init(this.servletConfig); |
| 117 | + return servlet; |
| 118 | + } |
| 119 | + |
| 120 | + private MockHttpServletRequest initRequest(String path) { |
| 121 | + path = UriUtils.encodePath(path, StandardCharsets.UTF_8); |
| 122 | + MockHttpServletRequest request = new MockHttpServletRequest("GET", path); |
| 123 | + request.setCharacterEncoding(StandardCharsets.UTF_8.name()); |
| 124 | + return request; |
| 125 | + } |
| 126 | + |
| 127 | + |
| 128 | + @EnableWebMvc |
| 129 | + static class WebConfig implements WebMvcConfigurer { |
| 130 | + |
| 131 | + @Override |
| 132 | + public void addResourceHandlers(ResourceHandlerRegistry registry) { |
| 133 | + ClassPathResource classPathLocation = new ClassPathResource("", getClass()); |
| 134 | + String path = getPath(classPathLocation); |
| 135 | + |
| 136 | + registerClasspathLocation("/cp/**", classPathLocation, registry); |
| 137 | + registerFileSystemLocation("/fs/**", path, registry); |
| 138 | + registerUrlLocation("/url/**", "file://" + path, registry); |
| 139 | + } |
| 140 | + |
| 141 | + protected void registerClasspathLocation(String pattern, ClassPathResource resource, ResourceHandlerRegistry registry) { |
| 142 | + registry.addResourceHandler(pattern).addResourceLocations(resource); |
| 143 | + } |
| 144 | + |
| 145 | + protected void registerFileSystemLocation(String pattern, String path, ResourceHandlerRegistry registry) { |
| 146 | + FileSystemResource fileSystemLocation = new FileSystemResource(path); |
| 147 | + registry.addResourceHandler(pattern).addResourceLocations(fileSystemLocation); |
| 148 | + } |
| 149 | + |
| 150 | + protected void registerUrlLocation(String pattern, String path, ResourceHandlerRegistry registry) { |
| 151 | + UrlResource urlLocation = new UrlResource(toURL(path)); |
| 152 | + registry.addResourceHandler(pattern).addResourceLocations(urlLocation); |
| 153 | + } |
| 154 | + |
| 155 | + private String getPath(ClassPathResource resource) { |
| 156 | + try { |
| 157 | + return resource.getFile().getCanonicalPath().replace("classes/java", "resources") + "/"; |
| 158 | + } |
| 159 | + catch (IOException ex) { |
| 160 | + throw new IllegalStateException(ex); |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + private URL toURL(String path) { |
| 165 | + try { |
| 166 | + return URI.create(path).toURL(); |
| 167 | + } |
| 168 | + catch (MalformedURLException ex) { |
| 169 | + throw new IllegalStateException(ex); |
| 170 | + } |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + |
| 175 | + static class PathPatternParserConfig implements WebMvcConfigurer { |
| 176 | + |
| 177 | + @Override |
| 178 | + public void configurePathMatch(PathMatchConfigurer configurer) { |
| 179 | + configurer.setPatternParser(new PathPatternParser()); |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | +} |
0 commit comments