Search in sources :

Example 26 with MockResponse

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

the class ResponseCacheInterceptor_Test method testCache.

@Test
public void testCache() {
    // Create interceptor
    ResponseCacheInterceptor interceptor = new ResponseCacheInterceptor(CacheType.CONTENT_CACHE);
    interceptor.setBackingComponent(new WText());
    // Mock Response
    MockResponse response = new MockResponse();
    interceptor.attachResponse(response);
    // Render phase
    interceptor.paint(new WebXmlRenderContext(response.getWriter()));
    Assert.assertEquals("Cache-Control header not set correctly for CACHE", ConfigurationProperties.getResponseCacheSettings(), response.getHeaders().get("Cache-Control"));
    Assert.assertNull("Pragma header should be null for CACHE", response.getHeaders().get("Pragma"));
    Assert.assertNull("Expires header should be null for CACHE", response.getHeaders().get("Expires"));
}
Also used : WebXmlRenderContext(com.github.bordertech.wcomponents.servlet.WebXmlRenderContext) MockResponse(com.github.bordertech.wcomponents.util.mock.MockResponse) WText(com.github.bordertech.wcomponents.WText) Test(org.junit.Test)

Example 27 with MockResponse

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

the class ResponseCacheInterceptor_Test method testNoCache.

@Test
public void testNoCache() {
    // 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()));
    Assert.assertEquals("Cache-Control header not set correctly for NO_CACHE", ConfigurationProperties.getResponseNoCacheSettings(), response.getHeaders().get("Cache-Control"));
    Assert.assertEquals("Pragma header not set correctly for NO_CACHE", "no-cache", response.getHeaders().get("Pragma"));
    Assert.assertEquals("Expires header not set correctly for NO_CACHE", "-1", response.getHeaders().get("Expires"));
}
Also used : WebXmlRenderContext(com.github.bordertech.wcomponents.servlet.WebXmlRenderContext) MockResponse(com.github.bordertech.wcomponents.util.mock.MockResponse) WText(com.github.bordertech.wcomponents.WText) Test(org.junit.Test)

Example 28 with MockResponse

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

the class ResponseCacheInterceptor_Test method testOverrideDefaultCache.

@Test
public void testOverrideDefaultCache() {
    /**
     * Original config.
     */
    Configuration originalConfig;
    originalConfig = Config.getInstance();
    String override = "OVERRIDE CACHE";
    try {
        // Test override cache settings
        Configuration config = Config.copyConfiguration(originalConfig);
        config.setProperty(ConfigurationProperties.RESPONSE_CACHE_SETTINGS, override);
        Config.setConfiguration(config);
        // Create interceptor
        ResponseCacheInterceptor interceptor = new ResponseCacheInterceptor(CacheType.CONTENT_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 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)

Example 29 with MockResponse

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

the class WrongStepAjaxInterceptor_Test method testInterceptorOkStep.

@Test
public void testInterceptorOkStep() throws XpathException, SAXException, IOException {
    MyApp app = new MyApp();
    app.setLocked(true);
    MockResponse response = doAjaxRequest(app, 1, 1);
    assertXpathEvaluatesTo(app.target.getText(), "//ui:textfield", response.getWriterOutput());
}
Also used : MockResponse(com.github.bordertech.wcomponents.util.mock.MockResponse) Test(org.junit.Test)

Example 30 with MockResponse

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

the class WrongStepAjaxInterceptor_Test method doAjaxRequest.

/**
 * Does an AJAX request for the app.
 *
 * @param app the MyApp instance to do an AJAX request for.
 * @param clientStep the client-side step counter
 * @param serverStep the server-side step counter
 * @return the response object.
 */
private MockResponse doAjaxRequest(final MyApp app, final int clientStep, final int serverStep) {
    UIContext uic = createUIContext();
    WServlet.WServletEnvironment env = new WServlet.WServletEnvironment(APP_POSTPATH, "http://localhost", "");
    env.setStep(serverStep);
    env.setSessionToken("T");
    uic.setEnvironment(env);
    uic.setUI(app);
    setActiveContext(uic);
    MockRequest request = new MockRequest();
    MockResponse response = new MockResponse();
    // Create interceptors
    AjaxSetupInterceptor ajaxSetupInterceptor = new AjaxSetupInterceptor();
    WrongStepAjaxInterceptor wrongStepInterceptor = new WrongStepAjaxInterceptor();
    AjaxPageShellInterceptor ajaxPageInterceptor = new AjaxPageShellInterceptor();
    AjaxInterceptor ajaxInterceptor = new AjaxInterceptor();
    ajaxPageInterceptor.setBackingComponent(ajaxInterceptor);
    wrongStepInterceptor.setBackingComponent(ajaxPageInterceptor);
    ajaxSetupInterceptor.setBackingComponent(wrongStepInterceptor);
    ajaxSetupInterceptor.attachUI(app);
    ajaxSetupInterceptor.attachResponse(response);
    // Action phase
    try {
        AjaxHelper.registerComponent(app.target.getId(), app.trigger.getId());
        request.setParameter(WServlet.AJAX_TRIGGER_PARAM_NAME, app.trigger.getId());
        request.setParameter(Environment.STEP_VARIABLE, String.valueOf(clientStep));
        ajaxSetupInterceptor.serviceRequest(request);
        ajaxSetupInterceptor.preparePaint(request);
        // Render phase
        ajaxSetupInterceptor.paint(new WebXmlRenderContext(response.getWriter()));
    } catch (ActionEscape ignored) {
    // is thrown to skip render phase
    }
    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) MockRequest(com.github.bordertech.wcomponents.util.mock.MockRequest) WServlet(com.github.bordertech.wcomponents.servlet.WServlet)

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