@Test public void testBasicURLWithNonExistingImageRewriting() { // Set the properties Properties props = new Properties(); config = new JawrConfig("css", props); ServletContext servletContext = new MockServletContext(); config.setContext(servletContext); config.setServletMapping("/css"); config.setCharsetName("UTF-8"); addGeneratorRegistryToConfig(config, "css"); status = new BundleProcessingStatus( BundleProcessingStatus.BUNDLE_PROCESSING_TYPE, bundle, rsHandler, config); // Set up the Image servlet Jawr config props = new Properties(); JawrConfig imgServletJawrConfig = new JawrConfig(JawrConstant.BINARY_TYPE, props); addGeneratorRegistryToConfig(imgServletJawrConfig, JawrConstant.BINARY_TYPE); BinaryResourcesHandler imgRsHandler = new BinaryResourcesHandler(imgServletJawrConfig, rsHandler, null); servletContext.setAttribute(JawrConstant.BINARY_CONTEXT_ATTRIBUTE, imgRsHandler); // basic test StringBuffer data = new StringBuffer("background-image:url(../../../../images/someImage.gif);"); // the image is at /images String filePath = "/css/folder/subfolder/subfolder/someCSS.css"; // Expected: goes 1 back for servlet mapping, 1 back for prefix, 1 back for the id having a // subdir path. String expectedURL = "background-image:url(../../../images/someImage.gif);"; status.setLastPathAdded(filePath); String result = processor.postProcessBundle(status, data).toString(); assertEquals("URL was not rewritten properly", expectedURL, result); }
@Test public void testSameUrlWithQuotes() { StringBuffer data = new StringBuffer("background-image:url( 'images/some\\'Image\\\".gif' );"); // Remove the url mapping from config, one back reference less expected config.setServletMapping(""); String expectedURL = "background-image:url('../../css/images/some\\'Image\\\".gif');"; String result = processor.postProcessBundle(status, data).toString(); assertEquals("URL was not rewritten properly:" + result, expectedURL, result); }
@Test public void testURLImgClasspathCssRewriting() { // Set the properties Properties props = new Properties(); props.setProperty(JawrConfig.JAWR_CSS_CLASSPATH_HANDLE_IMAGE, "true"); config = new JawrConfig("css", props); ServletContext servletContext = new MockServletContext(); config.setContext(servletContext); config.setServletMapping("/css"); config.setCharsetName("UTF-8"); addGeneratorRegistryToConfig(config, "css"); // Set up the Image servlet Jawr config props = new Properties(); JawrConfig imgServletJawrConfig = new JawrConfig(JawrConstant.BINARY_TYPE, props); GeneratorRegistry generatorRegistry = addGeneratorRegistryToConfig(imgServletJawrConfig, JawrConstant.BINARY_TYPE); generatorRegistry.setResourceReaderHandler(rsHandler); imgServletJawrConfig.setServletMapping("/cssImg/"); BinaryResourcesHandler imgRsHandler = new BinaryResourcesHandler(imgServletJawrConfig, rsHandler, null); servletContext.setAttribute(JawrConstant.BINARY_CONTEXT_ATTRIBUTE, imgRsHandler); status = new BundleProcessingStatus( BundleProcessingStatus.BUNDLE_PROCESSING_TYPE, bundle, null, config); // Css data StringBuffer data = new StringBuffer("background-image:url(jar:style/images/logo.png);"); // Css path String filePath = "style/default/assets/someCSS.css"; // Expected: goes 3 back to the context path, then add the CSS image servlet mapping, // then go to the image path // the image is at classPath:/style/images/someImage.gif String expectedURL = "background-image:url(../../../cssImg/jar_cb3015770054/style/images/logo.png);"; status.setLastPathAdded(filePath); String result = processor.postProcessBundle(status, data).toString(); assertEquals("URL was not rewritten properly", expectedURL, result); }
@SuppressWarnings("unchecked") @Before public void setUp() throws Exception { // Bundle path (full url would be: /servletMapping/prefix/css/bundle.css final String bundlePath = "/css/bundle.css"; // Bundle url prefix final String urlPrefix = "/v00"; when(rsHandler.getResourceAsStream(Matchers.anyString())) .thenAnswer( new Answer<InputStream>() { @Override public InputStream answer(InvocationOnMock invocation) throws Throwable { String resourceName = (String) invocation.getArguments()[0]; if (resourceName.equals("jar:style/images/logo.png")) { return new ByteArrayInputStream("Fake value".getBytes()); } throw new ResourceNotFoundException(resourceName); } }); when(bundle.getId()).thenReturn(bundlePath); when(bundle.getURLPrefix(Matchers.anyMap())).thenReturn(urlPrefix); config = new JawrConfig("css", new Properties()); ServletContext servletContext = new MockServletContext(); config.setContext(servletContext); config.setServletMapping("/js"); config.setCharsetName("UTF-8"); status = new BundleProcessingStatus( BundleProcessingStatus.BUNDLE_PROCESSING_TYPE, bundle, null, config); addGeneratorRegistryToConfig(config, "js"); status.setLastPathAdded("/css/someCSS.css"); processor = new CSSURLPathRewriterPostProcessor(); }