Search in sources :

Example 1 with InvocationContext

use of com.meterware.servletunit.InvocationContext in project teammates by TEAMMATES.

the class GaeSimulation method createWebRequest.

private HttpServletRequest createWebRequest(String uri, String... parameters) {
    WebRequest request = new PostMethodWebRequest("http://localhost" + uri);
    if (Const.SystemParams.PAGES_REQUIRING_ORIGIN_VALIDATION.contains(uri)) {
        request.setHeaderField("referer", "http://localhost");
        String sessionId = sc.getSession(true).getId();
        String token = CryptoHelper.computeSessionToken(sessionId);
        request.setParameter(Const.ParamsNames.SESSION_TOKEN, token);
    }
    Map<String, List<String>> paramMultiMap = new HashMap<>();
    for (int i = 0; i < parameters.length; i = i + 2) {
        String key = parameters[i];
        if (paramMultiMap.get(key) == null) {
            paramMultiMap.put(key, new ArrayList<String>());
        }
        paramMultiMap.get(key).add(parameters[i + 1]);
    }
    paramMultiMap.forEach((key, values) -> request.setParameter(key, values.toArray(new String[0])));
    try {
        InvocationContext ic = sc.newInvocation(request);
        return ic.getRequest();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : WebRequest(com.meterware.httpunit.WebRequest) PostMethodWebRequest(com.meterware.httpunit.PostMethodWebRequest) HashMap(java.util.HashMap) PostMethodWebRequest(com.meterware.httpunit.PostMethodWebRequest) ArrayList(java.util.ArrayList) List(java.util.List) InvocationContext(com.meterware.servletunit.InvocationContext)

Aggregations

PostMethodWebRequest (com.meterware.httpunit.PostMethodWebRequest)1 WebRequest (com.meterware.httpunit.WebRequest)1 InvocationContext (com.meterware.servletunit.InvocationContext)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1