Fix ResourceUrlProvider path check in getForRequestUrl

Prior to this change, getForRequestUrl implementation would only work
for applications with a non-empty servlet path. So web applications
mapped to "/" would trigger a IllegalStateException while checking the
current request against the request path within the current mapping.

This change relaxes this and only check that the path within mapping is
within the request URL.

Issue: SPR-12158
This commit is contained in:
Brian Clozel
2014-09-05 22:46:13 +02:00
parent 1c2857d15e
commit 6aef1a1d17
2 changed files with 27 additions and 2 deletions

View File

@@ -73,7 +73,7 @@ public class ResourceTransformerSupportTests {
}
@Test
public void rewriteAbsolutePath() throws Exception {
public void rewriteAbsolutePathWithContext() throws Exception {
this.request.setRequestURI("/servlet/context/resources/main.css");
this.request.setMethod("GET");
this.request.setServletPath("/servlet");
@@ -84,6 +84,18 @@ public class ResourceTransformerSupportTests {
Resource mainCss = new ClassPathResource("test/main.css", getClass());
String actual = this.transformer.resolveUrlPath(resourcePath, this.request, mainCss, this.transformerChain);
assertEquals("/servlet/context/resources/bar-11e16cf79faee7ac698c805cf28248d2.css", actual);
}
@Test
public void rewriteAbsolutePath() throws Exception {
this.request.setRequestURI("/resources/main.css");
this.request.setMethod("GET");
this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "/resources/main.css");
String resourcePath = "/resources/bar.css";
Resource mainCss = new ClassPathResource("test/main.css", getClass());
String actual = this.transformer.resolveUrlPath(resourcePath, this.request, mainCss, this.transformerChain);
assertEquals("/resources/bar-11e16cf79faee7ac698c805cf28248d2.css", actual);
actual = this.transformer.resolveUrlPath("bar.css", this.request, mainCss, this.transformerChain);
assertEquals("bar-11e16cf79faee7ac698c805cf28248d2.css", actual);
@@ -102,6 +114,19 @@ public class ResourceTransformerSupportTests {
assertEquals("bar-11e16cf79faee7ac698c805cf28248d2.css", actual);
}
@Test(expected = IllegalStateException.class)
public void rewriteAbsolutePathWrongPath() throws Exception {
this.request.setRequestURI("/servlet/context/resources/main.css");
this.request.setMethod("GET");
this.request.setServletPath("/servlet");
this.request.setContextPath("/context");
this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "/wrong/main.css");
String resourcePath = "/servlet/context/resources/bar.css";
Resource mainCss = new ClassPathResource("test/main.css", getClass());
this.transformer.resolveUrlPath(resourcePath, this.request, mainCss, this.transformerChain);
}
@Test
public void rewriteRelativePathUpperLevel() throws Exception {
this.request.setRequestURI("/servlet/context/resources/images/image.png");