/** * Render the component and execute the interceptor. * * @param testUI the test component * @return the response */ private TestResult generateOutput(final MyComponent testUI) { InterceptorComponent interceptor = new TransformXMLInterceptor(); interceptor.setBackingComponent(testUI); MockResponse response = new MockResponse(); interceptor.attachResponse(response); StringWriter writer = new StringWriter(); UIContext uic = createUIContext(); uic.setLocale(new Locale("xx")); setActiveContext(uic); try { interceptor.paint(new WebXmlRenderContext(new PrintWriter(writer))); } finally { resetContext(); } return new TestResult(writer.toString(), response.getContentType()); }
@Test public void testHandleRequest() throws IOException { byte[] data = "WImage_Test.testHandleRequest".getBytes(CHAR_ENCODING); MockRequest request = new MockRequest(); MockImage content = new MockImage(); content.setBytes(data); WImage image = new WImage(); image.setLocked(true); setActiveContext(createUIContext()); image.setImage(content); // Should not do anything when target is not present image.handleRequest(request); try { request.setParameter(Environment.TARGET_ID, image.getTargetId()); image.handleRequest(request); Assert.fail("Should have thrown a content escape"); } catch (ContentEscape escape) { MockResponse response = new MockResponse(); escape.setResponse(response); escape.escape(); String output = new String(response.getOutput(), CHAR_ENCODING); Assert.assertEquals("Incorrect content returned", new String(data, CHAR_ENCODING), output); Assert.assertFalse("Cache flag should not be set", escape.isCacheable()); Assert.assertEquals( "Response should have header set for no caching", ResponseCacheInterceptor.DEFAULT_NO_CACHE_SETTINGS, response.getHeaders().get("Cache-Control")); } // Test Cached Response image.setCacheKey("key"); // Should produce the content with cache flag set try { image.handleRequest(request); Assert.fail("Should have thrown a content escape"); } catch (ContentEscape escape) { MockResponse response = new MockResponse(); escape.setResponse(response); escape.escape(); String output = new String(response.getOutput(), CHAR_ENCODING); Assert.assertEquals("Incorrect content returned", new String(data, CHAR_ENCODING), output); Assert.assertTrue("Cache flag should be set", escape.isCacheable()); Assert.assertEquals( "Response should have header set for caching", ResponseCacheInterceptor.DEFAULT_CACHE_SETTINGS, response.getHeaders().get("Cache-Control")); } }