Search in sources :

Example 16 with MockResponse

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();
        }
    }
}
Also used : WebXmlRenderContext(com.github.bordertech.wcomponents.servlet.WebXmlRenderContext) MockResponse(com.github.bordertech.wcomponents.util.mock.MockResponse) InterceptorComponent(com.github.bordertech.wcomponents.container.InterceptorComponent) StringWriter(java.io.StringWriter) TransformXMLInterceptor(com.github.bordertech.wcomponents.container.TransformXMLInterceptor) PageShellInterceptor(com.github.bordertech.wcomponents.container.PageShellInterceptor) TemplateRenderInterceptor(com.github.bordertech.wcomponents.container.TemplateRenderInterceptor) PrintWriter(java.io.PrintWriter)

Example 17 with MockResponse

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"));
}
Also used : MockResponse(com.github.bordertech.wcomponents.util.mock.MockResponse) Test(org.junit.Test)

Example 18 with MockResponse

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());
}
Also used : MockResponse(com.github.bordertech.wcomponents.util.mock.MockResponse) MockRequest(com.github.bordertech.wcomponents.util.mock.MockRequest) Test(org.junit.Test)

Example 19 with MockResponse

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();
    }
}
Also used : MockResponse(com.github.bordertech.wcomponents.util.mock.MockResponse) WebXmlRenderContext(com.github.bordertech.wcomponents.servlet.WebXmlRenderContext) MockResponse(com.github.bordertech.wcomponents.util.mock.MockResponse) InterceptorComponent(com.github.bordertech.wcomponents.container.InterceptorComponent) MockHttpServletRequest(com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletRequest) MockRequest(com.github.bordertech.wcomponents.util.mock.MockRequest) WServlet(com.github.bordertech.wcomponents.servlet.WServlet) NullWriter(com.github.bordertech.wcomponents.util.NullWriter) PrintWriter(java.io.PrintWriter)

Example 20 with MockResponse

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);
    }
}
Also used : MockResponse(com.github.bordertech.wcomponents.util.mock.MockResponse) MockRequest(com.github.bordertech.wcomponents.util.mock.MockRequest) Test(org.junit.Test)

Aggregations

MockResponse (com.github.bordertech.wcomponents.util.mock.MockResponse)32 Test (org.junit.Test)23 WebXmlRenderContext (com.github.bordertech.wcomponents.servlet.WebXmlRenderContext)17 MockRequest (com.github.bordertech.wcomponents.util.mock.MockRequest)15 PrintWriter (java.io.PrintWriter)7 UIContext (com.github.bordertech.wcomponents.UIContext)6 WText (com.github.bordertech.wcomponents.WText)6 Configuration (org.apache.commons.configuration.Configuration)5 WServlet (com.github.bordertech.wcomponents.servlet.WServlet)4 MockHttpServletRequest (com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletRequest)4 ActionEscape (com.github.bordertech.wcomponents.ActionEscape)3 InterceptorComponent (com.github.bordertech.wcomponents.container.InterceptorComponent)3 StringWriter (java.io.StringWriter)3 DefaultWComponent (com.github.bordertech.wcomponents.DefaultWComponent)2 TestLookupTable (com.github.bordertech.wcomponents.TestLookupTable)2 ServletRequest (com.github.bordertech.wcomponents.servlet.ServletRequest)2 NullWriter (com.github.bordertech.wcomponents.util.NullWriter)2 List (java.util.List)2 Locale (java.util.Locale)2 ContentEscape (com.github.bordertech.wcomponents.ContentEscape)1