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));
}
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);
}
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);
}
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());
}
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());
}
Aggregations