Search in sources :

Example 66 with HttpSession

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

the class ServletAnnotationControllerHandlerMethodTests method redirectAttribute.

@PathPatternsParameterizedTest
void redirectAttribute(boolean usePathPatterns) throws Exception {
    initDispatcherServlet(RedirectAttributesController.class, usePathPatterns);
    MockHttpServletRequest request = new MockHttpServletRequest("POST", "/messages");
    HttpSession session = request.getSession();
    MockHttpServletResponse response = new MockHttpServletResponse();
    getServlet().service(request, response);
    // POST -> bind error
    getServlet().service(request, response);
    assertThat(response.getStatus()).isEqualTo(200);
    assertThat(response.getForwardedUrl()).isEqualTo("messages/new");
    assertThat(RequestContextUtils.getOutputFlashMap(request).isEmpty()).isTrue();
    // POST -> success
    request = new MockHttpServletRequest("POST", "/messages");
    request.setSession(session);
    request.addParameter("name", "Jeff");
    response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertThat(response.getStatus()).isEqualTo(302);
    assertThat(response.getRedirectedUrl()).isEqualTo("/messages/1?name=value");
    assertThat(RequestContextUtils.getOutputFlashMap(request).get("successMessage")).isEqualTo("yay!");
    // GET after POST
    request = new MockHttpServletRequest("GET", "/messages/1");
    request.setQueryString("name=value");
    request.setSession(session);
    response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertThat(response.getStatus()).isEqualTo(200);
    assertThat(response.getContentAsString()).isEqualTo("Got: yay!");
    assertThat(RequestContextUtils.getOutputFlashMap(request).isEmpty()).isTrue();
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) HttpSession(jakarta.servlet.http.HttpSession) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) PathPatternsParameterizedTest(org.springframework.web.servlet.handler.PathPatternsParameterizedTest)

Example 67 with HttpSession

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

the class ServletAnnotationControllerHandlerMethodTests method parameterDispatchingController.

@PathPatternsParameterizedTest
void parameterDispatchingController(boolean usePathPatterns) throws Exception {
    final MockServletContext servletContext = new MockServletContext();
    final MockServletConfig servletConfig = new MockServletConfig(servletContext);
    WebApplicationContext webAppContext = initDispatcherServlet(MyParameterDispatchingController.class, usePathPatterns, wac -> {
        wac.setServletContext(servletContext);
        AnnotationConfigUtils.registerAnnotationConfigProcessors(wac);
        wac.getBeanFactory().registerResolvableDependency(ServletConfig.class, servletConfig);
    });
    MockHttpServletRequest request = new MockHttpServletRequest(servletContext, "GET", "/myPath.do");
    MockHttpServletResponse response = new MockHttpServletResponse();
    HttpSession session = request.getSession();
    assertThat(session).isNotNull();
    getServlet().service(request, response);
    assertThat(response.getContentAsString()).isEqualTo("myView");
    assertThat(request.getAttribute("servletContext")).isSameAs(servletContext);
    assertThat(request.getAttribute("servletConfig")).isSameAs(servletConfig);
    assertThat(request.getAttribute("sessionId")).isSameAs(session.getId());
    assertThat(request.getAttribute("requestUri")).isSameAs(request.getRequestURI());
    assertThat(request.getAttribute("locale")).isSameAs(request.getLocale());
    request = new MockHttpServletRequest(servletContext, "GET", "/myPath.do");
    response = new MockHttpServletResponse();
    session = request.getSession();
    assertThat(session).isNotNull();
    getServlet().service(request, response);
    assertThat(response.getContentAsString()).isEqualTo("myView");
    assertThat(request.getAttribute("servletContext")).isSameAs(servletContext);
    assertThat(request.getAttribute("servletConfig")).isSameAs(servletConfig);
    assertThat(request.getAttribute("sessionId")).isSameAs(session.getId());
    assertThat(request.getAttribute("requestUri")).isSameAs(request.getRequestURI());
    request = new MockHttpServletRequest(servletContext, "GET", "/myPath.do");
    request.addParameter("view", "other");
    response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertThat(response.getContentAsString()).isEqualTo("myOtherView");
    request = new MockHttpServletRequest(servletContext, "GET", "/myPath.do");
    request.addParameter("view", "my");
    request.addParameter("lang", "de");
    response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertThat(response.getContentAsString()).isEqualTo("myLangView");
    request = new MockHttpServletRequest(servletContext, "GET", "/myPath.do");
    request.addParameter("surprise", "!");
    response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertThat(response.getContentAsString()).isEqualTo("mySurpriseView");
    MyParameterDispatchingController deserialized = (MyParameterDispatchingController) SerializationTestUtils.serializeAndDeserialize(webAppContext.getBean(MyParameterDispatchingController.class.getSimpleName()));
    assertThat(deserialized.request).isNotNull();
    assertThat(deserialized.session).isNotNull();
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) HttpSession(jakarta.servlet.http.HttpSession) MockServletConfig(org.springframework.web.testfixture.servlet.MockServletConfig) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) WebApplicationContext(org.springframework.web.context.WebApplicationContext) PathPatternsParameterizedTest(org.springframework.web.servlet.handler.PathPatternsParameterizedTest)

Example 68 with HttpSession

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

the class HtmlUnitRequestBuilderTests method buildRequestSessionFalseWithExistingSession.

@Test
public void buildRequestSessionFalseWithExistingSession() throws Exception {
    String sessionId = "session-id";
    webRequest.setAdditionalHeader("Cookie", "JSESSIONID=" + sessionId);
    MockHttpServletRequest actualRequest = requestBuilder.buildRequest(servletContext);
    HttpSession session = actualRequest.getSession(false);
    assertThat(session).isNotNull();
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) HttpSession(jakarta.servlet.http.HttpSession) MockHttpSession(org.springframework.mock.web.MockHttpSession) Test(org.junit.jupiter.api.Test)

Example 69 with HttpSession

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

the class HtmlUnitRequestBuilderTests method buildRequestSessionFalseIsNull.

@Test
public void buildRequestSessionFalseIsNull() throws Exception {
    MockHttpServletRequest actualRequest = requestBuilder.buildRequest(servletContext);
    HttpSession session = actualRequest.getSession(false);
    assertThat(session).isNull();
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) HttpSession(jakarta.servlet.http.HttpSession) MockHttpSession(org.springframework.mock.web.MockHttpSession) Test(org.junit.jupiter.api.Test)

Example 70 with HttpSession

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

the class AbstractController method handleRequest.

@Override
@Nullable
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
    if (HttpMethod.OPTIONS.matches(request.getMethod())) {
        response.setHeader(HttpHeaders.ALLOW, getAllowHeader());
        return null;
    }
    // Delegate to WebContentGenerator for checking and preparing.
    checkRequest(request);
    prepareResponse(response);
    // Execute handleRequestInternal in synchronized block if required.
    if (this.synchronizeOnSession) {
        HttpSession session = request.getSession(false);
        if (session != null) {
            Object mutex = WebUtils.getSessionMutex(session);
            synchronized (mutex) {
                return handleRequestInternal(request, response);
            }
        }
    }
    return handleRequestInternal(request, response);
}
Also used : HttpSession(jakarta.servlet.http.HttpSession) Nullable(org.springframework.lang.Nullable)

Aggregations

HttpSession (jakarta.servlet.http.HttpSession)101 Test (org.junit.jupiter.api.Test)39 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)17 MvcResult (org.springframework.test.web.servlet.MvcResult)16 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)13 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)12 MockHttpSession (org.springframework.mock.web.MockHttpSession)12 Map (java.util.Map)11 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)11 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)9 SecurityContext (org.springframework.security.core.context.SecurityContext)7 PathPatternsParameterizedTest (org.springframework.web.servlet.handler.PathPatternsParameterizedTest)7 Authentication (org.springframework.security.core.Authentication)6 Cookie (jakarta.servlet.http.Cookie)5 Request (org.apache.catalina.connector.Request)5 OAuth2AuthorizedClient (org.springframework.security.oauth2.client.OAuth2AuthorizedClient)5 SessionFixationProtectionStrategy (org.springframework.security.web.authentication.session.SessionFixationProtectionStrategy)5 IOException (java.io.IOException)4 PrintWriter (java.io.PrintWriter)4 Response (org.apache.catalina.connector.Response)4