Search in sources :

Example 1 with CookieCollection

use of org.apache.wicket.protocol.http.mock.CookieCollection in project wicket by apache.

the class BaseWicketTester method setupNextRequestCycle.

private void setupNextRequestCycle() {
    request = new MockHttpServletRequest(application, httpSession, servletContext, servletRequestLocale());
    request.setURL(request.getContextPath() + request.getServletPath() + "/");
    // assign protocol://host:port to next request unless the last request was ajax
    final boolean assignBaseLocation = lastRequest != null && lastRequest.getHeader("Wicket-Ajax") == null;
    // resume request processing with scheme://host:port from last request
    if (assignBaseLocation) {
        request.setScheme(lastRequest.getScheme());
        request.setSecure(lastRequest.isSecure());
        request.setServerName(lastRequest.getServerName());
        request.setServerPort(lastRequest.getServerPort());
    }
    response = new MockHttpServletResponse(request);
    // They should assert that the cookie is in the next *request*
    if (lastResponse != null) {
        List<Cookie> lastResponseCookies = lastResponse.getCookies();
        if (lastResponse.isRedirect()) {
            CookieCollection responseCookies = new CookieCollection();
            // if the last request is a redirect, all cookies from last response should appear
            // in current response
            // this call will filter duplicates
            responseCookies.addAll(lastResponseCookies);
            for (Cookie cookie : responseCookies.allAsList()) {
                response.addCookie(cookie);
            }
            // handling this way, the cookie will be send to the next requested page
            if (lastRequest != null) {
                CookieCollection requestCookies = new CookieCollection();
                // this call will filter duplicates
                requestCookies.addAll(lastRequest.getCookies());
                request.addCookies(requestCookies.asList());
            }
        } else {
            // if the last response is not a redirect
            // - copy last request cookies to collection
            // - copy last response cookies to collection
            // - set only the not expired cookies to the next request
            CookieCollection cookies = new CookieCollection();
            if (lastRequest != null) {
                // this call will filter duplicates
                cookies.addAll(lastRequest.getCookies());
            }
            // this call will filter duplicates
            cookies.addAll(lastResponseCookies);
            request.addCookies(cookies.asList());
        }
    }
    ServletWebRequest servletWebRequest = newServletWebRequest();
    requestCycle = application.createRequestCycle(servletWebRequest, newServletWebResponse(servletWebRequest));
    ThreadContext.setRequestCycle(requestCycle);
    if (session == null) {
        newSession();
    }
}
Also used : Cookie(javax.servlet.http.Cookie) MockHttpServletRequest(org.apache.wicket.protocol.http.mock.MockHttpServletRequest) ServletWebRequest(org.apache.wicket.protocol.http.servlet.ServletWebRequest) MockHttpServletResponse(org.apache.wicket.protocol.http.mock.MockHttpServletResponse) CookieCollection(org.apache.wicket.protocol.http.mock.CookieCollection)

Aggregations

Cookie (javax.servlet.http.Cookie)1 CookieCollection (org.apache.wicket.protocol.http.mock.CookieCollection)1 MockHttpServletRequest (org.apache.wicket.protocol.http.mock.MockHttpServletRequest)1 MockHttpServletResponse (org.apache.wicket.protocol.http.mock.MockHttpServletResponse)1 ServletWebRequest (org.apache.wicket.protocol.http.servlet.ServletWebRequest)1