Search in sources :

Example 61 with HttpSession

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

the class SharedHttpSessionTests method noHttpSession.

@Test
public void noHttpSession() throws Exception {
    MockMvc mockMvc = MockMvcBuilders.standaloneSetup(new TestController()).apply(sharedHttpSession()).build();
    String url = "/no-session";
    MvcResult result = mockMvc.perform(get(url)).andExpect(status().isOk()).andReturn();
    HttpSession session = result.getRequest().getSession(false);
    assertThat(session).isNull();
    result = mockMvc.perform(get(url)).andExpect(status().isOk()).andReturn();
    session = result.getRequest().getSession(false);
    assertThat(session).isNull();
    url = "/session";
    result = mockMvc.perform(get(url)).andExpect(status().isOk()).andReturn();
    session = result.getRequest().getSession(false);
    assertThat(session).isNotNull();
    assertThat(session.getAttribute("counter")).isEqualTo(1);
}
Also used : HttpSession(jakarta.servlet.http.HttpSession) SharedHttpSessionConfigurer.sharedHttpSession(org.springframework.test.web.servlet.setup.SharedHttpSessionConfigurer.sharedHttpSession) MvcResult(org.springframework.test.web.servlet.MvcResult) MockMvc(org.springframework.test.web.servlet.MockMvc) Test(org.junit.jupiter.api.Test)

Example 62 with HttpSession

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

the class SharedHttpSessionTests method httpSession.

@Test
public void httpSession() throws Exception {
    MockMvc mockMvc = MockMvcBuilders.standaloneSetup(new TestController()).apply(sharedHttpSession()).build();
    String url = "/session";
    MvcResult result = mockMvc.perform(get(url)).andExpect(status().isOk()).andReturn();
    HttpSession session = result.getRequest().getSession(false);
    assertThat(session).isNotNull();
    assertThat(session.getAttribute("counter")).isEqualTo(1);
    result = mockMvc.perform(get(url)).andExpect(status().isOk()).andReturn();
    session = result.getRequest().getSession(false);
    assertThat(session).isNotNull();
    assertThat(session.getAttribute("counter")).isEqualTo(2);
    result = mockMvc.perform(get(url)).andExpect(status().isOk()).andReturn();
    session = result.getRequest().getSession(false);
    assertThat(session).isNotNull();
    assertThat(session.getAttribute("counter")).isEqualTo(3);
}
Also used : HttpSession(jakarta.servlet.http.HttpSession) SharedHttpSessionConfigurer.sharedHttpSession(org.springframework.test.web.servlet.setup.SharedHttpSessionConfigurer.sharedHttpSession) MvcResult(org.springframework.test.web.servlet.MvcResult) MockMvc(org.springframework.test.web.servlet.MockMvc) Test(org.junit.jupiter.api.Test)

Example 63 with HttpSession

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

the class ServletAnnotationControllerHandlerMethodTests method sessionAttributeExposure.

@SuppressWarnings("rawtypes")
@PathPatternsParameterizedTest
void sessionAttributeExposure(boolean usePathPatterns) throws Exception {
    initDispatcherServlet(MySessionAttributesController.class, usePathPatterns, wac -> wac.registerBeanDefinition("viewResolver", new RootBeanDefinition(ModelExposingViewResolver.class)));
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myPage");
    MockHttpServletResponse response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertThat(request.getAttribute("viewName")).isEqualTo("page1");
    HttpSession session = request.getSession();
    assertThat(session).isNotNull();
    assertThat(session.getAttribute("object1") != null).isTrue();
    assertThat(session.getAttribute("object2") != null).isTrue();
    assertThat(((Map) session.getAttribute("model")).containsKey("object1")).isTrue();
    assertThat(((Map) session.getAttribute("model")).containsKey("object2")).isTrue();
    request = new MockHttpServletRequest("POST", "/myPage");
    request.setSession(session);
    response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertThat(request.getAttribute("viewName")).isEqualTo("page2");
    assertThat(session.getAttribute("object1") != null).isTrue();
    assertThat(session.getAttribute("object2") != null).isTrue();
    assertThat(((Map) session.getAttribute("model")).containsKey("object1")).isTrue();
    assertThat(((Map) session.getAttribute("model")).containsKey("object2")).isTrue();
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) HttpSession(jakarta.servlet.http.HttpSession) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Map(java.util.Map) MultiValueMap(org.springframework.util.MultiValueMap) ModelMap(org.springframework.ui.ModelMap) ExtendedModelMap(org.springframework.ui.ExtendedModelMap) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) PathPatternsParameterizedTest(org.springframework.web.servlet.handler.PathPatternsParameterizedTest)

Example 64 with HttpSession

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

the class ServletAnnotationControllerHandlerMethodTests method parameterizedAnnotatedInterface.

@SuppressWarnings("rawtypes")
@PathPatternsParameterizedTest
void parameterizedAnnotatedInterface(boolean usePathPatterns) throws Exception {
    initDispatcherServlet(MyParameterizedControllerImpl.class, usePathPatterns, wac -> wac.registerBeanDefinition("viewResolver", new RootBeanDefinition(ModelExposingViewResolver.class)));
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myPage");
    MockHttpServletResponse response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertThat(request.getAttribute("viewName")).isEqualTo("page1");
    HttpSession session = request.getSession();
    assertThat(session).isNotNull();
    assertThat(session.getAttribute("object1") != null).isTrue();
    assertThat(session.getAttribute("object2") != null).isTrue();
    assertThat(((Map) session.getAttribute("model")).containsKey("object1")).isTrue();
    assertThat(((Map) session.getAttribute("model")).containsKey("object2")).isTrue();
    assertThat(((Map) session.getAttribute("model")).containsKey("testBeanList")).isTrue();
    request = new MockHttpServletRequest("POST", "/myPage");
    request.setSession(session);
    response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertThat(request.getAttribute("viewName")).isEqualTo("page2");
    assertThat(session.getAttribute("object1") != null).isTrue();
    assertThat(session.getAttribute("object2") != null).isTrue();
    assertThat(((Map) session.getAttribute("model")).containsKey("object1")).isTrue();
    assertThat(((Map) session.getAttribute("model")).containsKey("object2")).isTrue();
    assertThat(((Map) session.getAttribute("model")).containsKey("testBeanList")).isTrue();
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) HttpSession(jakarta.servlet.http.HttpSession) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Map(java.util.Map) MultiValueMap(org.springframework.util.MultiValueMap) ModelMap(org.springframework.ui.ModelMap) ExtendedModelMap(org.springframework.ui.ExtendedModelMap) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) PathPatternsParameterizedTest(org.springframework.web.servlet.handler.PathPatternsParameterizedTest)

Example 65 with HttpSession

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

the class ServletAnnotationControllerHandlerMethodTests method parameterizedAnnotatedInterfaceWithOverriddenMappingsInImpl.

@SuppressWarnings("rawtypes")
@PathPatternsParameterizedTest
void parameterizedAnnotatedInterfaceWithOverriddenMappingsInImpl(boolean usePathPatterns) throws Exception {
    initDispatcherServlet(MyParameterizedControllerImplWithOverriddenMappings.class, usePathPatterns, wac -> wac.registerBeanDefinition("viewResolver", new RootBeanDefinition(ModelExposingViewResolver.class)));
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myPage");
    MockHttpServletResponse response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertThat(request.getAttribute("viewName")).isEqualTo("page1");
    HttpSession session = request.getSession();
    assertThat(session).isNotNull();
    assertThat(session.getAttribute("object1") != null).isTrue();
    assertThat(session.getAttribute("object2") != null).isTrue();
    assertThat(((Map) session.getAttribute("model")).containsKey("object1")).isTrue();
    assertThat(((Map) session.getAttribute("model")).containsKey("object2")).isTrue();
    assertThat(((Map) session.getAttribute("model")).containsKey("testBeanList")).isTrue();
    request = new MockHttpServletRequest("POST", "/myPage");
    request.setSession(session);
    response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertThat(request.getAttribute("viewName")).isEqualTo("page2");
    assertThat(session.getAttribute("object1") != null).isTrue();
    assertThat(session.getAttribute("object2") != null).isTrue();
    assertThat(((Map) session.getAttribute("model")).containsKey("object1")).isTrue();
    assertThat(((Map) session.getAttribute("model")).containsKey("object2")).isTrue();
    assertThat(((Map) session.getAttribute("model")).containsKey("testBeanList")).isTrue();
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) HttpSession(jakarta.servlet.http.HttpSession) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Map(java.util.Map) MultiValueMap(org.springframework.util.MultiValueMap) ModelMap(org.springframework.ui.ModelMap) ExtendedModelMap(org.springframework.ui.ExtendedModelMap) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) PathPatternsParameterizedTest(org.springframework.web.servlet.handler.PathPatternsParameterizedTest)

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