Search in sources :

Example 16 with MockHttpServletRequest

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

the class ServletUtilDeviceType_Test method testDeviceType.

@Test
public void testDeviceType() {
    // HttpServletRequest
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setHeader("User-Agent", userAgent);
    Assert.assertEquals("Incorrect device type for " + desc, expectedDeviceType, ServletUtil.getDevice(request));
    // ServletRequest
    ServletRequest req = new ServletRequest(request);
    Assert.assertEquals("Incorrect device type for " + desc + " using ServletRequest", expectedDeviceType, ServletUtil.getDevice(req));
}
Also used : MockHttpServletRequest(com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletRequest) MockHttpServletRequest(com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletRequest) Test(org.junit.Test)

Example 17 with MockHttpServletRequest

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

the class WServletPerformance_Test method timeWServlet.

/**
 * Times the WServlet execution looping the given number of times and returns the elapsed time.
 *
 * @param count the number of times to loop.
 * @return the elapsed time, in nanoseconds.
 * @throws Exception an exception
 */
private long timeWServlet(final int count) throws Exception {
    final SimpleWServlet servlet = new SimpleWServlet();
    servlet.init(new MockServletConfig());
    final MockHttpSession simpleWServletSession = new MockHttpSession();
    // Do first request to get session token
    sendWServletRequest(servlet, simpleWServletSession, 0, null);
    // Get token
    WServletHelper helper = new WServletHelper(servlet, new MockHttpServletRequest(simpleWServletSession), new MockHttpServletResponse());
    String token = helper.getUIContext().getEnvironment().getSessionToken();
    // JIT warm-up
    for (int i = 1; i < count; i++) {
        sendWServletRequest(servlet, simpleWServletSession, i, token);
    }
    simpleWServletSession.getAttributes().clear();
    // Do first request to get session token
    sendWServletRequest(servlet, simpleWServletSession, 0, null);
    final String token2 = helper.getUIContext().getEnvironment().getSessionToken();
    Runnable runnable = new Runnable() {

        @Override
        public void run() {
            try {
                for (int i = 1; i < count; i++) {
                    sendWServletRequest(servlet, simpleWServletSession, i, token2);
                }
            } catch (Exception e) {
                LOG.error("Failed to execute test", e);
            }
        }
    };
    return time(runnable);
}
Also used : WServletHelper(com.github.bordertech.wcomponents.servlet.WServlet.WServletHelper) MockHttpServletRequest(com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletRequest) MockServletConfig(com.github.bordertech.wcomponents.util.mock.servlet.MockServletConfig) MockHttpSession(com.github.bordertech.wcomponents.util.mock.servlet.MockHttpSession) MockHttpServletResponse(com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletResponse) ServletException(javax.servlet.ServletException)

Example 18 with MockHttpServletRequest

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

the class WServletPerformance_Test method sendWServletRequest.

/**
 * Invokes WComponent servlet processing.
 *
 * @param servlet the servlet to invoke request processing on.
 * @param session the current session.
 * @param step the step count
 * @param token the session token
 * @throws Exception an exception
 */
private void sendWServletRequest(final WServlet servlet, final HttpSession session, final int step, final String token) throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest(session);
    MockHttpServletResponse response = new MockHttpServletResponse();
    if (step > 0) {
        request.setMethod("POST");
        request.setRequestURI("http://localhost/app");
        // These are hard-coded to avoid overhead during performance testing
        request.setParameter(Environment.STEP_VARIABLE, String.valueOf(step));
        request.setParameter(Environment.SESSION_TOKEN_VARIABLE, token);
        request.setParameter("_0b", "p1_" + step);
        request.setParameter("_0d", "p2_" + step);
        request.setParameter("_0e", "x");
    } else {
        request.setMethod("GET");
        request.setRequestURI("http://localhost/app");
    }
    servlet.service(request, response);
}
Also used : MockHttpServletRequest(com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletRequest) MockHttpServletResponse(com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletResponse)

Example 19 with MockHttpServletRequest

use of com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletRequest 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 20 with MockHttpServletRequest

use of com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletRequest 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)

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