ImageUrlRewriter { public String rewrite(final String cssUri, final String imageUrl) { notNull(cssUri); notNull(imageUrl); if (StringUtils.isEmpty(imageUrl)) { return imageUrl; } if (ServletContextUriLocator.isValid(cssUri)) { if (ServletContextUriLocator.isValid(imageUrl)) { return prependContextPath(imageUrl); } if (ServletContextUriLocator.isProtectedResource(cssUri)) { return context.proxyPrefix + computeNewImageLocation(cssUri, imageUrl); } final String aggregatedPathPrefix = computeAggregationPathPrefix(context.aggregatedFolderPath); LOG.debug("computed aggregatedPathPrefix {}", aggregatedPathPrefix); String newImageLocation = computeNewImageLocation(aggregatedPathPrefix + cssUri, imageUrl); if (newImageLocation.startsWith(ServletContextUriLocator.PREFIX)) { newImageLocation = prependContextPath(newImageLocation); } LOG.debug("newImageLocation: {}", newImageLocation); return newImageLocation; } if (ClasspathUriLocator.isValid(cssUri)) { final String proxyUrl = context.proxyPrefix + computeNewImageLocation(cssUri, imageUrl); final String contextRelativeUrl = prependContextPath(imageUrl); return (ServletContextUriLocator.isValid(imageUrl) ? contextRelativeUrl : proxyUrl); } if (UrlUriLocator.isValid(cssUri)) { final String computedCssUri = ServletContextUriLocator.isValid(imageUrl) ? computeCssUriForExternalServer(cssUri) : cssUri; return computeNewImageLocation(computedCssUri, imageUrl); } throw new WroRuntimeException("Could not replace imageUrl: " + imageUrl + ", contained at location: " + cssUri); } ImageUrlRewriter(final RewriterContext context); String rewrite(final String cssUri, final String imageUrl);  }
@Test(expected = NullPointerException.class) public void cannotAcceptNullCssUri() { victim.rewrite(null, DEFAULT_IMAGE_URL); }
@Test(expected = NullPointerException.class) public void cannotAcceptNullImageUrl() { victim.rewrite(DEFAULT_CSS_URI, null); }
@Test public void checkContextRelativeUrlInClasspathCssResource() { final String actual = victim.rewrite(ClasspathUriLocator.createUri(DEFAULT_CSS_URI), DEFAULT_IMAGE_URL); assertEquals(DEFAULT_IMAGE_URL, actual); }
@Test public void checkRelativeImageUrlInClasspathCssResource() { final String actual = victim.rewrite(ClasspathUriLocator.createUri(DEFAULT_CSS_URI), RELATIVE_IMAGE_URL); final String expected = DEFAULT_PREFIX + "classpath:/path/to/" + RELATIVE_IMAGE_URL; assertEquals(expected, actual); }
@Test public void checkRelativeImageUrlInClasspathCssResourceWhenContextPathIsNotDefault() { context.setContextPath("/1/2/3"); final String actual = victim.rewrite(ClasspathUriLocator.createUri(DEFAULT_CSS_URI), RELATIVE_IMAGE_URL); final String expected = DEFAULT_PREFIX + "classpath:/path/to/" + RELATIVE_IMAGE_URL; assertEquals(expected, actual); }
@Test public void checkRelativeWithDotsImageUrlInClasspathCssResource() { final String actual = victim.rewrite(ClasspathUriLocator.createUri(DEFAULT_CSS_URI), "../" + RELATIVE_IMAGE_URL); final String expected = DEFAULT_PREFIX + "classpath:/path/" + RELATIVE_IMAGE_URL; assertEquals(expected, actual); }
@Test public void checkRelativeImageUrlInContextRelativeCssResource() { final String actual = victim.rewrite(DEFAULT_CSS_URI, RELATIVE_IMAGE_URL); final String expected = "/path/to/" + RELATIVE_IMAGE_URL; assertEquals(expected, actual); }
@Test public void checkEmptyImageUrlInContextRelativeCssResource() { final String actual = victim.rewrite(DEFAULT_CSS_URI, ""); final String expected = ""; assertEquals(expected, actual); }
@Test public void checkRelativeImageUrlInProtectedContextRelativeCssResource() { final String actual = victim.rewrite(PROTECTED_CSS_URI, RELATIVE_IMAGE_URL); final String expected = DEFAULT_PREFIX + "/WEB-INF/path/to/" + RELATIVE_IMAGE_URL; assertEquals(expected, actual); }
@Test public void checkRelativeWithDotsImageUrlInContextRelativeCssResource() { final String actual = victim.rewrite(DEFAULT_CSS_URI, "../" + RELATIVE_IMAGE_URL); final String expected = "/path/" + RELATIVE_IMAGE_URL; assertEquals(expected, actual); }