Search in sources :

Example 6 with MockHttpSession

use of org.springframework.web.testfixture.servlet.MockHttpSession in project spring-framework by spring-projects.

the class DefaultServerRequestTests method session.

@Test
void session() {
    MockHttpServletRequest servletRequest = PathPatternsTestUtils.initRequest("GET", "/", true);
    MockHttpSession session = new MockHttpSession();
    servletRequest.setSession(session);
    DefaultServerRequest request = new DefaultServerRequest(servletRequest, this.messageConverters);
    assertThat(request.session()).isEqualTo(session);
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockHttpSession(org.springframework.web.testfixture.servlet.MockHttpSession) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 7 with MockHttpSession

use of org.springframework.web.testfixture.servlet.MockHttpSession in project spring-framework by spring-projects.

the class RequestLoggingFilterTests method session.

@Test
void session() throws Exception {
    request.setSession(new MockHttpSession(null, "42"));
    filter.setIncludeClientInfo(true);
    applyFilter();
    assertThat(filter.beforeRequestMessage).contains("session=42");
    assertThat(filter.afterRequestMessage).contains("session=42");
}
Also used : MockHttpSession(org.springframework.web.testfixture.servlet.MockHttpSession) Test(org.junit.jupiter.api.Test)

Example 8 with MockHttpSession

use of org.springframework.web.testfixture.servlet.MockHttpSession in project spring-framework by spring-projects.

the class ServletRequestAttributesTests method setSessionScopedAttribute.

@Test
public void setSessionScopedAttribute() {
    MockHttpSession session = new MockHttpSession();
    session.setAttribute(KEY, VALUE);
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setSession(session);
    ServletRequestAttributes attrs = new ServletRequestAttributes(request);
    attrs.setAttribute(KEY, VALUE, RequestAttributes.SCOPE_SESSION);
    assertThat(session.getAttribute(KEY)).isSameAs(VALUE);
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockHttpSession(org.springframework.web.testfixture.servlet.MockHttpSession) Test(org.junit.jupiter.api.Test)

Example 9 with MockHttpSession

use of org.springframework.web.testfixture.servlet.MockHttpSession in project spring-framework by spring-projects.

the class ServletRequestAttributesTests method removeSessionScopedAttribute.

@Test
public void removeSessionScopedAttribute() {
    MockHttpSession session = new MockHttpSession();
    session.setAttribute(KEY, VALUE);
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setSession(session);
    ServletRequestAttributes attrs = new ServletRequestAttributes(request);
    attrs.removeAttribute(KEY, RequestAttributes.SCOPE_SESSION);
    Object value = session.getAttribute(KEY);
    assertThat(value).isNull();
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockHttpSession(org.springframework.web.testfixture.servlet.MockHttpSession) Test(org.junit.jupiter.api.Test)

Example 10 with MockHttpSession

use of org.springframework.web.testfixture.servlet.MockHttpSession in project spring-framework by spring-projects.

the class ServletRequestAttributesTests method setSessionScopedAttributeAfterCompletion.

@Test
public void setSessionScopedAttributeAfterCompletion() {
    MockHttpSession session = new MockHttpSession();
    session.setAttribute(KEY, VALUE);
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setSession(session);
    ServletRequestAttributes attrs = new ServletRequestAttributes(request);
    assertThat(attrs.getAttribute(KEY, RequestAttributes.SCOPE_SESSION)).isSameAs(VALUE);
    attrs.requestCompleted();
    request.close();
    attrs.setAttribute(KEY, VALUE, RequestAttributes.SCOPE_SESSION);
    assertThat(session.getAttribute(KEY)).isSameAs(VALUE);
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockHttpSession(org.springframework.web.testfixture.servlet.MockHttpSession) Test(org.junit.jupiter.api.Test)

Aggregations

MockHttpSession (org.springframework.web.testfixture.servlet.MockHttpSession)15 Test (org.junit.jupiter.api.Test)14 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)9 DerivedTestBean (org.springframework.beans.testfixture.beans.DerivedTestBean)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 TestBean (org.springframework.beans.testfixture.beans.TestBean)2 FilterChain (jakarta.servlet.FilterChain)1 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)1 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)1 Serializable (java.io.Serializable)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)1 MethodParameter (org.springframework.core.MethodParameter)1 FileCopyUtils (org.springframework.util.FileCopyUtils)1 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)1 ContentCachingRequestWrapper (org.springframework.web.util.ContentCachingRequestWrapper)1 WebUtils (org.springframework.web.util.WebUtils)1