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