Search in sources :

Example 21 with MockHttpServletRequest

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

the class DevToolkit_Test method testResetSession.

@Test
public void testResetSession() {
    MockHttpSession session = new MockHttpSession();
    Assert.assertFalse("Session should not be invalidated", session.isInvalidated());
    DevToolkit toolkit = new DevToolkit();
    MockHttpServletRequest request = new MockHttpServletRequest(session);
    request.setParameter("wc_devToolkit", "x");
    toolkit.serviceRequest(request);
    Assert.assertFalse("Session should not be invalidated", session.isInvalidated());
    request = new MockHttpServletRequest(session);
    request.setParameter("wc_devToolkit", "x");
    request.setParameter("devToolkit_resetSession", "x");
    toolkit.serviceRequest(request);
    Assert.assertTrue("Session should be invalidated", session.isInvalidated());
}
Also used : MockHttpServletRequest(com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletRequest) MockHttpSession(com.github.bordertech.wcomponents.util.mock.servlet.MockHttpSession) Test(org.junit.Test)

Example 22 with MockHttpServletRequest

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

the class DevToolkit_Test method testPaintHeaderShowRequest.

@Test
public void testPaintHeaderShowRequest() {
    DevToolkit toolkit = new DevToolkit();
    String header = renderHeader(toolkit);
    Assert.assertTrue("Should not contain request info", header.indexOf("request headers") == -1);
    sendToolkitRequest(toolkit, "devToolkit_showRequest", "true");
    final String paramKey1 = "DevToolkit_Test.testPaintHeaderShowRequest.pkey1";
    final String paramKey2 = "DevToolkit_Test.testPaintHeaderShowRequest.pkey2";
    final String paramValue1 = "DevToolkit_Test.testPaintHeaderShowRequest.pvalue1";
    final String paramValue2 = "DevToolkit_Test.testPaintHeaderShowRequest.pvalue2";
    final String headerKey1 = "DevToolkit_Test.testPaintHeaderShowRequest.hkey1";
    final String headerKey2 = "DevToolkit_Test.testPaintHeaderShowRequest.hkey2";
    final String headerValue1 = "DevToolkit_Test.testPaintHeaderShowRequest.hvalue1";
    final String headerValue2 = "DevToolkit_Test.testPaintHeaderShowRequest.hvalue2";
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setParameter(paramKey1, paramValue1);
    request.setParameter(paramKey2, paramValue2);
    request.setHeader(headerKey1, headerValue1);
    request.setHeader(headerKey2, headerValue2);
    toolkit.serviceRequest(request);
    header = renderHeader(toolkit);
    Assert.assertTrue("Should contain request info", header.indexOf("request headers") != -1);
    Assert.assertTrue("Output should contain parameter key1", header.contains(paramKey1));
    Assert.assertTrue("Output should contain parameter key2", header.contains(paramKey2));
    Assert.assertTrue("Output should contain parameter value1", header.contains(paramValue1));
    Assert.assertTrue("Output should contain parameter value2", header.contains(paramValue2));
    Assert.assertTrue("Output should contain header key1", header.contains(headerKey1));
    Assert.assertTrue("Output should contain header key2", header.contains(headerKey2));
    Assert.assertTrue("Output should contain header value1", header.contains(headerValue1));
    Assert.assertTrue("Output should contain header value2", header.contains(headerValue2));
}
Also used : MockHttpServletRequest(com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletRequest) Test(org.junit.Test)

Example 23 with MockHttpServletRequest

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

the class WebXmlRenderingPerformance_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 : Response(com.github.bordertech.wcomponents.Response) 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 24 with MockHttpServletRequest

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

the class ServletUtilTest method testExtractCookieAmongstMany.

@Test
public void testExtractCookieAmongstMany() {
    String cookieName = "mycookiename";
    String cookieValue = "mycookievalue";
    MockHttpServletRequest httpServletRequest = new MockHttpServletRequest();
    for (byte i = 0; i < 10; i++) {
        httpServletRequest.setCookie(cookieName + i, cookieValue + 1);
    }
    httpServletRequest.setCookie(cookieName, cookieValue);
    Assert.assertEquals("Got cookie value by name", cookieValue, ServletUtil.extractCookie(httpServletRequest, cookieName));
}
Also used : MockHttpServletRequest(com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletRequest) Test(org.junit.Test)

Example 25 with MockHttpServletRequest

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

the class ThemeServlet_Test method requestFile.

/**
 * Requests a file from the servlet.
 *
 * @param path the path to the file
 * @param asParam true to use the ThemeServlet's 'f' parameter or false to place the path in the URL.
 * @return the servlet response
 * @throws ServletException a servlet exception
 * @throws IOException an exception
 */
private MockHttpServletResponse requestFile(final String path, final boolean asParam) throws ServletException, IOException {
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    if (asParam) {
        request.setRequestURI("/theme");
        request.setParameter("f", path);
    } else {
        request.setRequestURI("/theme/" + path);
        request.setPathInfo('/' + path);
    }
    themeServlet.doGet(request, response);
    return response;
}
Also used : MockHttpServletRequest(com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletRequest) MockHttpServletResponse(com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletResponse)

Aggregations

MockHttpServletRequest (com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletRequest)36 Test (org.junit.Test)25 MockHttpServletResponse (com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletResponse)13 MockHttpSession (com.github.bordertech.wcomponents.util.mock.servlet.MockHttpSession)13 MockServletConfig (com.github.bordertech.wcomponents.util.mock.servlet.MockServletConfig)9 WText (com.github.bordertech.wcomponents.WText)5 PrintWriter (java.io.PrintWriter)5 UIContext (com.github.bordertech.wcomponents.UIContext)4 WebXmlRenderContext (com.github.bordertech.wcomponents.servlet.WebXmlRenderContext)4 MockResponse (com.github.bordertech.wcomponents.util.mock.MockResponse)4 ServletRequest (com.github.bordertech.wcomponents.servlet.ServletRequest)3 NullWriter (com.github.bordertech.wcomponents.util.NullWriter)3 AbstractWComponent (com.github.bordertech.wcomponents.AbstractWComponent)2 WComponent (com.github.bordertech.wcomponents.WComponent)2 InterceptorComponent (com.github.bordertech.wcomponents.container.InterceptorComponent)2 WServlet (com.github.bordertech.wcomponents.servlet.WServlet)2 WServletHelper (com.github.bordertech.wcomponents.servlet.WServlet.WServletHelper)2 MockRequest (com.github.bordertech.wcomponents.util.mock.MockRequest)2 StringWriter (java.io.StringWriter)2 Locale (java.util.Locale)2