Search in sources :

Example 86 with HttpSession

use of javax.servlet.http.HttpSession in project spring-framework by spring-projects.

the class HtmlUnitRequestBuilderTests method buildRequestSessionWithExistingSession.

@Test
public void buildRequestSessionWithExistingSession() throws Exception {
    String sessionId = "session-id";
    webRequest.setAdditionalHeader("Cookie", "JSESSIONID=" + sessionId);
    MockHttpServletRequest actualRequest = requestBuilder.buildRequest(servletContext);
    HttpSession session = actualRequest.getSession();
    assertThat(session.getId(), equalTo(sessionId));
    assertSingleSessionCookie("JSESSIONID=" + session.getId() + "; Path=/test; Domain=example.com");
    requestBuilder = new HtmlUnitRequestBuilder(sessions, webClient, webRequest);
    actualRequest = requestBuilder.buildRequest(servletContext);
    assertThat(actualRequest.getSession(), equalTo(session));
    webRequest.setAdditionalHeader("Cookie", "JSESSIONID=" + sessionId + "NEW");
    actualRequest = requestBuilder.buildRequest(servletContext);
    assertThat(actualRequest.getSession(), not(equalTo(session)));
    assertSingleSessionCookie("JSESSIONID=" + actualRequest.getSession().getId() + "; Path=/test; Domain=example.com");
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) HttpSession(javax.servlet.http.HttpSession) MockHttpSession(org.springframework.mock.web.MockHttpSession) Test(org.junit.Test)

Example 87 with HttpSession

use of javax.servlet.http.HttpSession in project spring-framework by spring-projects.

the class HtmlUnitRequestBuilderTests method buildRequestSessionTrue.

@Test
public void buildRequestSessionTrue() throws Exception {
    MockHttpServletRequest actualRequest = requestBuilder.buildRequest(servletContext);
    HttpSession session = actualRequest.getSession(true);
    assertThat(session, notNullValue());
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) HttpSession(javax.servlet.http.HttpSession) MockHttpSession(org.springframework.mock.web.MockHttpSession) Test(org.junit.Test)

Example 88 with HttpSession

use of javax.servlet.http.HttpSession in project spring-framework by spring-projects.

the class HtmlUnitRequestBuilderTests method buildRequestSessionInvalidate.

@Test
public void buildRequestSessionInvalidate() throws Exception {
    String sessionId = "session-id";
    webRequest.setAdditionalHeader("Cookie", "JSESSIONID=" + sessionId);
    MockHttpServletRequest actualRequest = requestBuilder.buildRequest(servletContext);
    HttpSession sessionToRemove = actualRequest.getSession();
    sessionToRemove.invalidate();
    assertThat(sessions.containsKey(sessionToRemove.getId()), equalTo(false));
    assertSingleSessionCookie("JSESSIONID=" + sessionToRemove.getId() + "; Expires=Thu, 01-Jan-1970 00:00:01 GMT; Path=/test; Domain=example.com");
    webRequest.removeAdditionalHeader("Cookie");
    requestBuilder = new HtmlUnitRequestBuilder(sessions, webClient, webRequest);
    actualRequest = requestBuilder.buildRequest(servletContext);
    assertThat(actualRequest.getSession().isNew(), equalTo(true));
    assertThat(sessions.containsKey(sessionToRemove.getId()), equalTo(false));
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) HttpSession(javax.servlet.http.HttpSession) MockHttpSession(org.springframework.mock.web.MockHttpSession) Test(org.junit.Test)

Example 89 with HttpSession

use of javax.servlet.http.HttpSession in project spring-framework by spring-projects.

the class HtmlUnitRequestBuilderTests method buildRequestSession.

@Test
public void buildRequestSession() throws Exception {
    MockHttpServletRequest actualRequest = requestBuilder.buildRequest(servletContext);
    HttpSession newSession = actualRequest.getSession();
    assertThat(newSession, notNullValue());
    assertSingleSessionCookie("JSESSIONID=" + newSession.getId() + "; Path=/test; Domain=example.com");
    webRequest.setAdditionalHeader("Cookie", "JSESSIONID=" + newSession.getId());
    requestBuilder = new HtmlUnitRequestBuilder(sessions, webClient, webRequest);
    actualRequest = requestBuilder.buildRequest(servletContext);
    assertThat(actualRequest.getSession(), sameInstance(newSession));
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) HttpSession(javax.servlet.http.HttpSession) MockHttpSession(org.springframework.mock.web.MockHttpSession) Test(org.junit.Test)

Example 90 with HttpSession

use of javax.servlet.http.HttpSession in project spring-framework by spring-projects.

the class AbstractRequestLoggingFilter method createMessage.

/**
	 * Create a log message for the given request, prefix and suffix.
	 * <p>If {@code includeQueryString} is {@code true}, then the inner part
	 * of the log message will take the form {@code request_uri?query_string};
	 * otherwise the message will simply be of the form {@code request_uri}.
	 * <p>The final message is composed of the inner part as described and
	 * the supplied prefix and suffix.
	 */
protected String createMessage(HttpServletRequest request, String prefix, String suffix) {
    StringBuilder msg = new StringBuilder();
    msg.append(prefix);
    msg.append("uri=").append(request.getRequestURI());
    if (isIncludeQueryString()) {
        String queryString = request.getQueryString();
        if (queryString != null) {
            msg.append('?').append(queryString);
        }
    }
    if (isIncludeClientInfo()) {
        String client = request.getRemoteAddr();
        if (StringUtils.hasLength(client)) {
            msg.append(";client=").append(client);
        }
        HttpSession session = request.getSession(false);
        if (session != null) {
            msg.append(";session=").append(session.getId());
        }
        String user = request.getRemoteUser();
        if (user != null) {
            msg.append(";user=").append(user);
        }
    }
    if (isIncludeHeaders()) {
        msg.append(";headers=").append(new ServletServerHttpRequest(request).getHeaders());
    }
    if (isIncludePayload()) {
        ContentCachingRequestWrapper wrapper = WebUtils.getNativeRequest(request, ContentCachingRequestWrapper.class);
        if (wrapper != null) {
            byte[] buf = wrapper.getContentAsByteArray();
            if (buf.length > 0) {
                int length = Math.min(buf.length, getMaxPayloadLength());
                String payload;
                try {
                    payload = new String(buf, 0, length, wrapper.getCharacterEncoding());
                } catch (UnsupportedEncodingException ex) {
                    payload = "[unknown]";
                }
                msg.append(";payload=").append(payload);
            }
        }
    }
    msg.append(suffix);
    return msg.toString();
}
Also used : ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) HttpSession(javax.servlet.http.HttpSession) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ContentCachingRequestWrapper(org.springframework.web.util.ContentCachingRequestWrapper)

Aggregations

HttpSession (javax.servlet.http.HttpSession)730 HttpServletRequest (javax.servlet.http.HttpServletRequest)151 Test (org.junit.Test)110 IOException (java.io.IOException)80 HttpServletResponse (javax.servlet.http.HttpServletResponse)80 ServletException (javax.servlet.ServletException)75 ArrayList (java.util.ArrayList)65 RequestDispatcher (javax.servlet.RequestDispatcher)59 HashMap (java.util.HashMap)48 Map (java.util.Map)44 Locale (java.util.Locale)39 Properties (java.util.Properties)39 PrintWriter (java.io.PrintWriter)38 Cookie (javax.servlet.http.Cookie)27 List (java.util.List)24 SQLException (java.sql.SQLException)23 WebUser (org.compiere.util.WebUser)23 FlakyTest (org.apache.geode.test.junit.categories.FlakyTest)20 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)20 ModelAndView (org.springframework.web.servlet.ModelAndView)20