Search in sources :

Example 31 with HttpSession

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

the class SessionLocaleResolverTests method testSetAndResolveLocale.

@Test
public void testSetAndResolveLocale() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    SessionLocaleResolver resolver = new SessionLocaleResolver();
    resolver.setLocale(request, response, Locale.GERMAN);
    assertThat(resolver.resolveLocale(request)).isEqualTo(Locale.GERMAN);
    HttpSession session = request.getSession();
    request = new MockHttpServletRequest();
    request.setSession(session);
    resolver = new SessionLocaleResolver();
    assertThat(resolver.resolveLocale(request)).isEqualTo(Locale.GERMAN);
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) HttpSession(jakarta.servlet.http.HttpSession) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 32 with HttpSession

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

the class SessionLocaleResolverTests method testSetLocaleToNullLocale.

@Test
public void testSetLocaleToNullLocale() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addPreferredLocale(Locale.TAIWAN);
    request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME, Locale.GERMAN);
    MockHttpServletResponse response = new MockHttpServletResponse();
    SessionLocaleResolver resolver = new SessionLocaleResolver();
    resolver.setLocale(request, response, null);
    Locale locale = (Locale) request.getSession().getAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME);
    assertThat(locale).isNull();
    HttpSession session = request.getSession();
    request = new MockHttpServletRequest();
    request.addPreferredLocale(Locale.TAIWAN);
    request.setSession(session);
    resolver = new SessionLocaleResolver();
    assertThat(resolver.resolveLocale(request)).isEqualTo(Locale.TAIWAN);
}
Also used : Locale(java.util.Locale) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) HttpSession(jakarta.servlet.http.HttpSession) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 33 with HttpSession

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

the class ServletRequestAttributesTests method updateAccessedAttributes.

@Test
public void updateAccessedAttributes() {
    HttpServletRequest request = mock(HttpServletRequest.class);
    HttpSession session = mock(HttpSession.class);
    given(request.getSession(anyBoolean())).willReturn(session);
    given(session.getAttribute(KEY)).willReturn(VALUE);
    ServletRequestAttributes attrs = new ServletRequestAttributes(request);
    assertThat(attrs.getAttribute(KEY, RequestAttributes.SCOPE_SESSION)).isSameAs(VALUE);
    attrs.requestCompleted();
    verify(session, times(2)).getAttribute(KEY);
    verify(session).setAttribute(KEY, VALUE);
    verifyNoMoreInteractions(session);
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockHttpSession(org.springframework.web.testfixture.servlet.MockHttpSession) HttpSession(jakarta.servlet.http.HttpSession) Test(org.junit.jupiter.api.Test)

Example 34 with HttpSession

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

the class RequestMappingHandlerAdapter method handleInternal.

@Override
protected ModelAndView handleInternal(HttpServletRequest request, HttpServletResponse response, HandlerMethod handlerMethod) throws Exception {
    ModelAndView mav;
    checkRequest(request);
    // Execute invokeHandlerMethod in synchronized block if required.
    if (this.synchronizeOnSession) {
        HttpSession session = request.getSession(false);
        if (session != null) {
            Object mutex = WebUtils.getSessionMutex(session);
            synchronized (mutex) {
                mav = invokeHandlerMethod(request, response, handlerMethod);
            }
        } else {
            // No HttpSession available -> no mutex necessary
            mav = invokeHandlerMethod(request, response, handlerMethod);
        }
    } else {
        // No synchronization on session demanded at all...
        mav = invokeHandlerMethod(request, response, handlerMethod);
    }
    if (!response.containsHeader(HEADER_CACHE_CONTROL)) {
        if (getSessionAttributesHandler(handlerMethod).hasSessionAttributes()) {
            applyCacheSeconds(response, this.cacheSecondsForSessionAttributeHandlers);
        } else {
            prepareResponse(response);
        }
    }
    return mav;
}
Also used : HttpSession(jakarta.servlet.http.HttpSession) ModelAndView(org.springframework.web.servlet.ModelAndView)

Example 35 with HttpSession

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

the class ServletRequestAttributes method obtainSession.

private HttpSession obtainSession() {
    HttpSession session = getSession(true);
    Assert.state(session != null, "No HttpSession");
    return session;
}
Also used : HttpSession(jakarta.servlet.http.HttpSession)

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