Search in sources :

Example 1 with MockResponse

use of com.github.bordertech.wcomponents.util.mock.MockResponse in project wcomponents by BorderTech.

the class WVideo_Test method testHandlePosterRequest.

@Test
public void testHandlePosterRequest() throws IOException {
    MockImage poster = new MockImage();
    poster.setBytes("WVideo_Test.testHandlePosterRequest.one".getBytes(CHAR_ENCODING));
    WVideo video = new WVideo(new MockVideo());
    video.setPoster(poster);
    setActiveContext(createUIContext());
    MockRequest request = new MockRequest();
    // Should not do anything when target is not present
    video.handleRequest(request);
    try {
        request.setParameter(Environment.TARGET_ID, video.getTargetId());
        request.setParameter("WVideo.poster", "x");
        video.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(poster.getBytes(), 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
    video.setCacheKey("key");
    // Should produce the content with cache flag set
    try {
        request.setParameter("WVideo.poster", "x");
        video.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(poster.getBytes(), 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"));
    }
}
Also used : MockResponse(com.github.bordertech.wcomponents.util.mock.MockResponse) MockRequest(com.github.bordertech.wcomponents.util.mock.MockRequest) Test(org.junit.Test)

Example 2 with MockResponse

use of com.github.bordertech.wcomponents.util.mock.MockResponse in project wcomponents by BorderTech.

the class WVideo_Test method testHandleVideoRequest.

@Test
public void testHandleVideoRequest() throws IOException {
    MockVideo clip1 = new MockVideo();
    clip1.setBytes("WVideo_Test.testHandleVideoRequest.one".getBytes(CHAR_ENCODING));
    MockVideo clip2 = new MockVideo();
    clip2.setBytes("WVideo_Test.testHandleVideoRequest.two".getBytes(CHAR_ENCODING));
    WVideo video = new WVideo(new Video[] { clip1, clip2 });
    video.setLocked(true);
    setActiveContext(createUIContext());
    MockRequest request = new MockRequest();
    // Should not do anything when target is not present
    video.handleRequest(request);
    try {
        request.setParameter(Environment.TARGET_ID, video.getTargetId());
        request.setParameter("WVideo.videoIndex", "0");
        video.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(clip1.getBytes(), 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
    video.setCacheKey("key");
    // Should produce the content with cache flag set
    try {
        request.setParameter("WVideo.videoIndex", "1");
        video.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(clip2.getBytes(), 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"));
    }
}
Also used : MockResponse(com.github.bordertech.wcomponents.util.mock.MockResponse) MockRequest(com.github.bordertech.wcomponents.util.mock.MockRequest) Test(org.junit.Test)

Example 3 with MockResponse

use of com.github.bordertech.wcomponents.util.mock.MockResponse in project wcomponents by BorderTech.

the class DataListInterceptor_Test method testInterecptor.

@Test
public void testInterecptor() throws XpathException, SAXException, IOException {
    String tableKey = TestLookupTable.CACHEABLE_DAY_OF_WEEK_TABLE;
    // Create interceptor
    DataListInterceptor interceptor = new DataListInterceptor();
    interceptor.attachUI(new DefaultWComponent());
    // Action phase
    MockRequest request = new MockRequest();
    request.setParameter(WServlet.DATA_LIST_PARAM_NAME, tableKey);
    interceptor.serviceRequest(request);
    // Render phase
    MockResponse response = new MockResponse();
    interceptor.attachResponse(response);
    interceptor.paint(new WebXmlRenderContext(new PrintWriter(response.getWriter())));
    String xml = response.getWriterOutput();
    // Ensure that the data matches the test table.
    List<TestLookupTable.TableEntry> table = (List<TestLookupTable.TableEntry>) Factory.newInstance(LookupTable.class).getTable(tableKey);
    assertXpathEvaluatesTo(String.valueOf(table.size()), "count(/ui:datalist/ui:option)", xml);
    for (int i = 0; i < table.size(); i++) {
        assertXpathEvaluatesTo(table.get(i).getCode(), "/ui:datalist/ui:option[" + (i + 1) + "]/@value", xml);
        assertXpathEvaluatesTo(table.get(i).getDesc(), "/ui:datalist/ui:option[" + (i + 1) + "]/text()", xml);
    }
}
Also used : WebXmlRenderContext(com.github.bordertech.wcomponents.servlet.WebXmlRenderContext) MockResponse(com.github.bordertech.wcomponents.util.mock.MockResponse) TestLookupTable(com.github.bordertech.wcomponents.TestLookupTable) List(java.util.List) DefaultWComponent(com.github.bordertech.wcomponents.DefaultWComponent) MockRequest(com.github.bordertech.wcomponents.util.mock.MockRequest) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 4 with MockResponse

use of com.github.bordertech.wcomponents.util.mock.MockResponse in project wcomponents by BorderTech.

the class DebugStructureInterceptor_Test method doRequest.

/**
 * Does a request/response cycle for a WApplication.
 *
 * @return the xml output.
 */
private String doRequest() {
    WApplication app = new WApplication();
    app.setLocked(true);
    UIContext uic = createUIContext();
    uic.setUI(app);
    setActiveContext(uic);
    // Create interceptor
    PageShellInterceptor pageInterceptor = new PageShellInterceptor();
    DebugStructureInterceptor debugInterceptor = new DebugStructureInterceptor();
    pageInterceptor.setBackingComponent(debugInterceptor);
    pageInterceptor.attachUI(app);
    // Action phase
    MockRequest request = new MockRequest();
    pageInterceptor.serviceRequest(request);
    pageInterceptor.preparePaint(request);
    // Render phase
    MockResponse response = new MockResponse();
    pageInterceptor.attachResponse(response);
    pageInterceptor.paint(new WebXmlRenderContext(response.getWriter()));
    return response.getWriterOutput();
}
Also used : WebXmlRenderContext(com.github.bordertech.wcomponents.servlet.WebXmlRenderContext) MockResponse(com.github.bordertech.wcomponents.util.mock.MockResponse) WApplication(com.github.bordertech.wcomponents.WApplication) UIContext(com.github.bordertech.wcomponents.UIContext) MockRequest(com.github.bordertech.wcomponents.util.mock.MockRequest)

Example 5 with MockResponse

use of com.github.bordertech.wcomponents.util.mock.MockResponse in project wcomponents by BorderTech.

the class ResponseCacheInterceptor_Test method testOverrideDefaultNoCache.

@Test
public void testOverrideDefaultNoCache() {
    /**
     * Original config.
     */
    Configuration originalConfig;
    originalConfig = Config.getInstance();
    String override = "OVERRIDE NO CACHE";
    try {
        // Test override cache settings
        Configuration config = Config.copyConfiguration(originalConfig);
        config.setProperty(ConfigurationProperties.RESPONSE_NO_CACHE_SETTINGS, override);
        Config.setConfiguration(config);
        // Create interceptor
        ResponseCacheInterceptor interceptor = new ResponseCacheInterceptor(CacheType.CONTENT_NO_CACHE);
        interceptor.setBackingComponent(new WText());
        // Mock Response
        MockResponse response = new MockResponse();
        interceptor.attachResponse(response);
        // Render phase
        interceptor.paint(new WebXmlRenderContext(response.getWriter()));
        // Check Override
        Assert.assertEquals("Cache-Control header not overriden correctly for NO CACHE", override, response.getHeaders().get("Cache-Control"));
    } finally {
        // Remove overrides
        Config.setConfiguration(originalConfig);
    }
}
Also used : WebXmlRenderContext(com.github.bordertech.wcomponents.servlet.WebXmlRenderContext) MockResponse(com.github.bordertech.wcomponents.util.mock.MockResponse) Configuration(org.apache.commons.configuration.Configuration) WText(com.github.bordertech.wcomponents.WText) 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