Search in sources :

Example 11 with MockResponse

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

the class WrongStepContentInterceptor_Test method testServiceRequestNoStepError.

@Test
public void testServiceRequestNoStepError() {
    MyApp app = new MyApp();
    app.setLocked(true);
    MockResponse response = sendContentRequest(app.appContent, 1, 1);
    Assert.assertEquals("Should have returned content", MyContent.CONTENT, new String(response.getOutput()));
}
Also used : MockResponse(com.github.bordertech.wcomponents.util.mock.MockResponse) Test(org.junit.Test)

Example 12 with MockResponse

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

the class WrongStepContentInterceptor_Test method sendContentRequest.

/**
 * Utility method to send a mock request to the application.
 *
 * @param target the target content.
 * @param clientStep the client-side step count
 * @param serverStep the server-side step count
 * @return the response.
 */
private MockResponse sendContentRequest(final WComponent target, final int clientStep, final int serverStep) {
    UIContext uic = createUIContext();
    uic.setUI(WebUtilities.getTop(target));
    WServlet.WServletEnvironment env = new WServlet.WServletEnvironment("/app", "http://localhost", "");
    env.setStep(serverStep);
    uic.setEnvironment(env);
    setActiveContext(uic);
    MockRequest request = new MockRequest();
    request.setParameter(Environment.TARGET_ID, target.getId());
    request.setParameter(Environment.STEP_VARIABLE, String.valueOf(clientStep));
    MockResponse response = new MockResponse();
    InterceptorComponent interceptor = new WrongStepContentInterceptor();
    interceptor.attachUI(uic.getUI());
    interceptor.attachResponse(response);
    // Handle the request. This will either return the content or a step error
    try {
        interceptor.serviceRequest(request);
        interceptor.preparePaint(request);
        interceptor.paint(new WebXmlRenderContext(response.getWriter()));
    } catch (ContentEscape escape) {
        try {
            // Content has been returned
            escape.setRequest(request);
            escape.setResponse(response);
            escape.escape();
        } catch (IOException e) {
            Assert.fail("Failed to write content");
        }
    } catch (ErrorCodeEscape escape) {
        try {
            escape.setRequest(request);
            escape.setResponse(response);
            escape.escape();
        } catch (IOException e) {
            Assert.fail("Failed to write error content");
        }
    } catch (ActionEscape ignored) {
    // don't care
    }
    // Step error
    return response;
}
Also used : WebXmlRenderContext(com.github.bordertech.wcomponents.servlet.WebXmlRenderContext) MockResponse(com.github.bordertech.wcomponents.util.mock.MockResponse) ActionEscape(com.github.bordertech.wcomponents.ActionEscape) UIContext(com.github.bordertech.wcomponents.UIContext) ContentEscape(com.github.bordertech.wcomponents.ContentEscape) IOException(java.io.IOException) WServlet(com.github.bordertech.wcomponents.servlet.WServlet) ErrorCodeEscape(com.github.bordertech.wcomponents.ErrorCodeEscape) MockRequest(com.github.bordertech.wcomponents.util.mock.MockRequest)

Example 13 with MockResponse

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

the class ContentEscape_Test method testEscapeNoContent.

@Test
public void testEscapeNoContent() throws IOException {
    // ContentEscape with no content
    MockResponse response = new MockResponse();
    ContentEscape contentEscape = new ContentEscape(null);
    contentEscape.setResponse(response);
    contentEscape.escape();
    Assert.assertEquals("Content length should be zero", 0, response.getOutput().length);
    Assert.assertNull("Mime type should be null", response.getContentType());
    Assert.assertTrue("Headers should be empty", response.getHeaders().isEmpty());
}
Also used : MockResponse(com.github.bordertech.wcomponents.util.mock.MockResponse) Test(org.junit.Test)

Example 14 with MockResponse

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

the class WAudio_Test method testHandleRequest.

@Test
public void testHandleRequest() throws IOException {
    MockAudio clip1 = new MockAudio();
    clip1.setBytes("WAudio_Test.testHandleRequest.one".getBytes(CHAR_ENCODING));
    MockAudio clip2 = new MockAudio();
    clip2.setBytes("WAudio_Test.testHandleRequest.two".getBytes(CHAR_ENCODING));
    WAudio audio = new WAudio(new Audio[] { clip1, clip2 });
    MockRequest request = new MockRequest();
    setActiveContext(createUIContext());
    // Should not do anything when target is not present
    audio.handleRequest(request);
    try {
        request.setParameter(Environment.TARGET_ID, audio.getTargetId());
        request.setParameter("WAudio.index", "0");
        audio.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
    audio.setCacheKey("key");
    // Should produce the content with cache flag set
    try {
        request.setParameter("WAudio.index", "1");
        audio.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 15 with MockResponse

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

the class WImage_Test method testHandleRequest.

@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", ConfigurationProperties.RESPONSE_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", 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)

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