use of com.github.bordertech.wcomponents.util.mock.MockResponse in project wcomponents by BorderTech.
the class WebUtilities method renderWithTransformToHTML.
/**
* Renders and transforms the given WComponent to a HTML String outside of the context of a Servlet.
*
* @param request the request being responded to
* @param component the root WComponent to render
* @param includePageShell true if include page shell
* @return the rendered output as a String.
*/
public static String renderWithTransformToHTML(final Request request, final WComponent component, final boolean includePageShell) {
// Setup a context (if needed)
boolean needsContext = UIContextHolder.getCurrent() == null;
if (needsContext) {
UIContextHolder.pushContext(new UIContextImpl());
}
try {
// Link Interceptors
InterceptorComponent templateRender = new TemplateRenderInterceptor();
InterceptorComponent transformXML = new TransformXMLInterceptor();
templateRender.setBackingComponent(transformXML);
if (includePageShell) {
transformXML.setBackingComponent(new PageShellInterceptor());
}
// Attach Component and Mock Response
InterceptorComponent chain = templateRender;
chain.attachUI(component);
chain.attachResponse(new MockResponse());
// Render chain
StringWriter buffer = new StringWriter();
chain.preparePaint(request);
try (PrintWriter writer = new PrintWriter(buffer)) {
chain.paint(new WebXmlRenderContext(writer));
}
return buffer.toString();
} finally {
if (needsContext) {
UIContextHolder.popContext();
}
}
}
use of com.github.bordertech.wcomponents.util.mock.MockResponse in project wcomponents by BorderTech.
the class ContentEscape_Test method testEscape.
@Test
public void testEscape() throws IOException {
MockContentAccess contentAccess = new MockContentAccess();
contentAccess.setBytes(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 9, 8, 7, 6, 5, 4, 3, 2, 1 });
contentAccess.setMimeType("ContentEscape_Test/mimeType");
MockResponse response = new MockResponse();
ContentEscape contentEscape = new ContentEscape(contentAccess);
contentEscape.setResponse(response);
contentEscape.escape();
Assert.assertTrue("Incorrect content written", Arrays.equals(contentAccess.getBytes(), response.getOutput()));
Assert.assertEquals("Incorrect mime type set", contentAccess.getMimeType(), response.getContentType());
Assert.assertFalse("Content-Disposition header should not be set", response.getHeaders().containsKey("Content-Disposition"));
Assert.assertEquals("Response should have header set for no caching", ConfigurationProperties.RESPONSE_DEFAULT_NO_CACHE_SETTINGS, response.getHeaders().get("Cache-Control"));
// ContentEscape with content and description
contentAccess.setDescription("ContentEscape_Test.contentAccessDescription");
response = new MockResponse();
contentEscape.setResponse(response);
contentEscape.escape();
Assert.assertTrue("Incorrect content written", Arrays.equals(contentAccess.getBytes(), response.getOutput()));
Assert.assertEquals("Incorrect mime type set", contentAccess.getMimeType(), response.getContentType());
Assert.assertEquals("Incorrect content-Disposition", "inline; filename=" + contentAccess.getDescription(), response.getHeaders().get("Content-Disposition"));
// ContentEscape with Cacheable True
contentEscape.setCacheable(true);
response = new MockResponse();
contentEscape.setResponse(response);
contentEscape.escape();
Assert.assertEquals("Response should have header set for caching", ConfigurationProperties.RESPONSE_DEFAULT_CACHE_SETTINGS, response.getHeaders().get("Cache-Control"));
}
use of com.github.bordertech.wcomponents.util.mock.MockResponse in project wcomponents by BorderTech.
the class ForwardException_Test method testEscape.
@Test
public void testEscape() throws IOException {
ForwardException forward = new ForwardException(URL);
MockResponse response = new MockResponse();
forward.setRequest(new MockRequest());
forward.setResponse(response);
forward.escape();
Assert.assertEquals("Should have forwarded to url", URL, response.getRedirect());
}
use of com.github.bordertech.wcomponents.util.mock.MockResponse in project wcomponents by BorderTech.
the class SerializationPerformance_Test method sendRequest.
/**
* Invokes WComponent request processing, so that this test case can more closely match a production scenario.
*
* @param comp the component to invoke request processing on.
* @param uic the user context to use.
*/
private void sendRequest(final WComponent comp, final UIContext uic) {
PrintWriter writer = new PrintWriter(new NullWriter());
uic.setEnvironment(new WServlet.WServletEnvironment("", "http://localhost", ""));
uic.setUI(comp);
InterceptorComponent root = ServletUtil.createInterceptorChain(new MockHttpServletRequest());
root.attachUI(comp);
Response response = new MockResponse();
root.attachResponse(response);
setActiveContext(uic);
MockRequest request = new MockRequest();
try {
root.serviceRequest(request);
root.preparePaint(request);
root.paint(new WebXmlRenderContext(writer));
} finally {
resetContext();
}
}
use of com.github.bordertech.wcomponents.util.mock.MockResponse in project wcomponents by BorderTech.
the class WContent_Test method testPaintContent.
@Test
public void testPaintContent() throws IOException {
byte[] data = "WContent_Test.testPaint".getBytes(CHAR_ENCODING);
MockContentAccess content = new MockContentAccess();
content.setBytes(data);
WContent wContent = new WContent();
setActiveContext(createUIContext());
wContent.setContentAccess(content);
MockRequest request = new MockRequest();
request.setParameter(Environment.TARGET_ID, wContent.getTargetId());
// Should produce the content
try {
wContent.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", ConfigurationProperties.RESPONSE_DEFAULT_NO_CACHE_SETTINGS, response.getHeaders().get("Cache-Control"));
}
// Test Cached Response
wContent.setCacheKey("key");
// Should produce the content with cache flag set
try {
wContent.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", ConfigurationProperties.RESPONSE_DEFAULT_CACHE_SETTINGS, response.getHeaders().get("Cache-Control"));
}
// Test with streamed content
MockContentStreamAccess contentStream = new MockContentStreamAccess();
contentStream.setBytes(data);
wContent.setContentAccess(contentStream);
try {
wContent.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);
}
}
Aggregations