@Test public void resolveFromClasspathRoot() { Resource location = new ClassPathResource("/"); String requestPath = "org/springframework/web/servlet/resource/test/bar.css"; Resource actual = this.resolver.resolveResource(null, requestPath, Collections.singletonList(location), null); assertNotNull(actual); }
private void testCheckResource(Resource location, String requestPath) throws IOException { List<Resource> locations = Collections.singletonList(location); Resource actual = this.resolver.resolveResource(null, requestPath, locations, null); if (!location.createRelative(requestPath).exists() && !requestPath.contains(":")) { fail(requestPath + " doesn't actually exist as a relative path"); } assertNull(actual); }
@Test public void resolveFromClasspath() throws IOException { Resource location = new ClassPathResource("test/", PathResourceResolver.class); String requestPath = "bar.css"; Resource actual = this.resolver.resolveResource(null, requestPath, Collections.singletonList(location), null); assertEquals(location.createRelative(requestPath), actual); }
@Test public void checkRelativeLocation() throws Exception { String locationUrl= new UrlResource(getClass().getResource("./test/")).getURL().toExternalForm(); Resource location = new UrlResource(locationUrl.replace("/springframework","/../org/springframework")); assertNotNull(this.resolver.resolveResource(null, "main.css", Collections.singletonList(location), null)); }
@Test public void relativePathEncodedForUrlResource() throws Exception { TestUrlResource location = new TestUrlResource("file:///tmp"); List<TestUrlResource> locations = Collections.singletonList(location); // ISO-8859-1 this.resolver.setUrlPathHelper(new UrlPathHelper()); this.resolver.setLocationCharsets(Collections.singletonMap(location, StandardCharsets.ISO_8859_1)); this.resolver.resolveResource(new MockHttpServletRequest(), "/Ä ;ä.txt", locations, null); assertEquals("%C4%20%3B%E4.txt", location.getSavedRelativePath()); // UTF-8 this.resolver.setLocationCharsets(Collections.singletonMap(location, StandardCharsets.UTF_8)); this.resolver.resolveResource(new MockHttpServletRequest(), "/Ä ;ä.txt", locations, null); assertEquals("%C3%84%20%3B%C3%A4.txt", location.getSavedRelativePath()); // UTF-8 by default this.resolver.setLocationCharsets(Collections.emptyMap()); this.resolver.resolveResource(new MockHttpServletRequest(), "/Ä ;ä.txt", locations, null); assertEquals("%C3%84%20%3B%C3%A4.txt", location.getSavedRelativePath()); }