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);
}
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);
}
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();
}
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();
}
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();
}
Aggregations