Search in sources :

Example 6 with MockResponse

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

the class ResponseCacheInterceptor_Test method testOverrideContentCache.

@Test
public void testOverrideContentCache() {
    /**
     * Original config.
     */
    Configuration originalConfig;
    originalConfig = Config.getInstance();
    String override = "OVERRIDE CONTENT 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 CONTENT 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 7 with MockResponse

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

the class TemplateRenderInterceptorTest method generateOutput.

/**
 * Render the component and execute the interceptor.
 *
 * @param testUI the test component
 * @param headers Request headers to set (key/value pairs).
 * @return the response
 */
private TestResult generateOutput(final MyComponent testUI, final Map<String, String> headers) {
    InterceptorComponent interceptor = new TemplateRenderInterceptor();
    interceptor.attachUI(testUI);
    MockHttpServletRequest backing = new MockHttpServletRequest();
    if (headers != null) {
        for (String headerName : headers.keySet()) {
            backing.setHeader(headerName, headers.get(headerName));
        }
    }
    ServletRequest request = new ServletRequest(backing);
    MockResponse response = new MockResponse();
    interceptor.attachResponse(response);
    StringWriter writer = new StringWriter();
    UIContext uic = createUIContext();
    uic.setLocale(new Locale("en"));
    setActiveContext(uic);
    try {
        interceptor.preparePaint(request);
        interceptor.paint(new WebXmlRenderContext(new PrintWriter(writer)));
    } finally {
        resetContext();
    }
    return new TestResult(writer.toString(), response.getContentType());
}
Also used : Locale(java.util.Locale) WebXmlRenderContext(com.github.bordertech.wcomponents.servlet.WebXmlRenderContext) ServletRequest(com.github.bordertech.wcomponents.servlet.ServletRequest) MockHttpServletRequest(com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletRequest) MockResponse(com.github.bordertech.wcomponents.util.mock.MockResponse) StringWriter(java.io.StringWriter) UIContext(com.github.bordertech.wcomponents.UIContext) MockHttpServletRequest(com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletRequest) PrintWriter(java.io.PrintWriter)

Example 8 with MockResponse

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

the class TransformXMLInterceptor_Test method generateOutput.

/**
 * Render the component and execute the interceptor.
 *
 * @param testUI the test component
 * @param headers Request headers to set (key/value pairs).
 * @return the response
 */
private TestResult generateOutput(final MyComponent testUI, final Map<String, String> headers) {
    InterceptorComponent interceptor = new TransformXMLInterceptor();
    interceptor.attachUI(testUI);
    MockHttpServletRequest backing = new MockHttpServletRequest();
    if (headers != null) {
        for (String headerName : headers.keySet()) {
            backing.setHeader(headerName, headers.get(headerName));
        }
    }
    ServletRequest request = new ServletRequest(backing);
    MockResponse response = new MockResponse();
    interceptor.attachResponse(response);
    StringWriter writer = new StringWriter();
    UIContext uic = createUIContext();
    uic.setLocale(new Locale("en"));
    setActiveContext(uic);
    try {
        interceptor.preparePaint(request);
        interceptor.paint(new WebXmlRenderContext(new PrintWriter(writer)));
    } finally {
        resetContext();
    }
    return new TestResult(writer.toString(), response.getContentType());
}
Also used : Locale(java.util.Locale) WebXmlRenderContext(com.github.bordertech.wcomponents.servlet.WebXmlRenderContext) ServletRequest(com.github.bordertech.wcomponents.servlet.ServletRequest) MockHttpServletRequest(com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletRequest) MockResponse(com.github.bordertech.wcomponents.util.mock.MockResponse) StringWriter(java.io.StringWriter) UIContext(com.github.bordertech.wcomponents.UIContext) MockHttpServletRequest(com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletRequest) PrintWriter(java.io.PrintWriter)

Example 9 with MockResponse

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

the class WrongStepAjaxInterceptor_Test method testInterceptorRedirect.

@Test
public void testInterceptorRedirect() throws XpathException, SAXException, IOException {
    Config.getInstance().setProperty(ConfigurationProperties.STEP_ERROR_URL, "http://test.test");
    MyApp app = new MyApp();
    app.setLocked(true);
    MockResponse response = doAjaxRequest(app, 1, 99);
    assertXpathEvaluatesTo("http://test.test", "//ui:redirect/@url", response.getWriterOutput());
}
Also used : MockResponse(com.github.bordertech.wcomponents.util.mock.MockResponse) Test(org.junit.Test)

Example 10 with MockResponse

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

the class WrongStepAjaxInterceptor_Test method testInterceptorWarp.

@Test
public void testInterceptorWarp() throws XpathException, SAXException, IOException {
    Config.getInstance().setProperty(ConfigurationProperties.STEP_ERROR_URL, "");
    MyApp app = new MyApp();
    app.setLocked(true);
    // Should redirect to app postpath
    MockResponse response = doAjaxRequest(app, 1, 99);
    assertXpathEvaluatesTo(APP_POSTPATH, "//ui:redirect/@url", response.getWriterOutput());
}
Also used : MockResponse(com.github.bordertech.wcomponents.util.mock.MockResponse) 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