Search in sources :

Example 6 with ServletRequestEvent

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

the class RequestContextListenerTests method requestContextListenerWithSameThreadAndAttributesGone.

@Test
public void requestContextListenerWithSameThreadAndAttributesGone() {
    RequestContextListener listener = new RequestContextListener();
    MockServletContext context = new MockServletContext();
    MockHttpServletRequest request = new MockHttpServletRequest(context);
    request.setAttribute("test", "value");
    assertThat(RequestContextHolder.getRequestAttributes()).isNull();
    listener.requestInitialized(new ServletRequestEvent(context, request));
    assertThat(RequestContextHolder.getRequestAttributes()).isNotNull();
    assertThat(RequestContextHolder.getRequestAttributes().getAttribute("test", RequestAttributes.SCOPE_REQUEST)).isEqualTo("value");
    MockRunnable runnable = new MockRunnable();
    RequestContextHolder.getRequestAttributes().registerDestructionCallback("test", runnable, RequestAttributes.SCOPE_REQUEST);
    request.clearAttributes();
    listener.requestDestroyed(new ServletRequestEvent(context, request));
    assertThat(RequestContextHolder.getRequestAttributes()).isNull();
    assertThat(runnable.wasExecuted()).isTrue();
}
Also used : MockRunnable(org.springframework.core.task.MockRunnable) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) ServletRequestEvent(jakarta.servlet.ServletRequestEvent) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) Test(org.junit.jupiter.api.Test)

Aggregations

ServletRequestEvent (jakarta.servlet.ServletRequestEvent)6 ServletRequestListener (jakarta.servlet.ServletRequestListener)3 Test (org.junit.jupiter.api.Test)3 MockRunnable (org.springframework.core.task.MockRunnable)3 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)3 MockServletContext (org.springframework.web.testfixture.servlet.MockServletContext)3 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 Context (org.apache.catalina.Context)1 Tomcat (org.apache.catalina.startup.Tomcat)1 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)1 ByteChunk (org.apache.tomcat.util.buf.ByteChunk)1 SecurityConstraint (org.apache.tomcat.util.descriptor.web.SecurityConstraint)1 JSR356AsyncSupport (org.atmosphere.container.JSR356AsyncSupport)1 Test (org.junit.Test)1