Search in sources :

Example 1 with MockHttpSession

use of org.springframework.mock.web.test.MockHttpSession in project spring-framework by spring-projects.

the class HttpSessionHandshakeInterceptorTests method doNotCopyAttributes.

@Test
public void doNotCopyAttributes() throws Exception {
    Map<String, Object> attributes = new HashMap<>();
    WebSocketHandler wsHandler = Mockito.mock(WebSocketHandler.class);
    this.servletRequest.setSession(new MockHttpSession(null, "123"));
    this.servletRequest.getSession().setAttribute("foo", "bar");
    HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();
    interceptor.setCopyAllAttributes(false);
    interceptor.beforeHandshake(this.request, this.response, wsHandler, attributes);
    assertEquals(1, attributes.size());
    assertEquals("123", attributes.get(HttpSessionHandshakeInterceptor.HTTP_SESSION_ID_ATTR_NAME));
}
Also used : HashMap(java.util.HashMap) WebSocketHandler(org.springframework.web.socket.WebSocketHandler) MockHttpSession(org.springframework.mock.web.test.MockHttpSession) Test(org.junit.Test)

Example 2 with MockHttpSession

use of org.springframework.mock.web.test.MockHttpSession in project spring-framework by spring-projects.

the class HttpSessionHandshakeInterceptorTests method defaultConstructor.

@Test
public void defaultConstructor() throws Exception {
    Map<String, Object> attributes = new HashMap<>();
    WebSocketHandler wsHandler = Mockito.mock(WebSocketHandler.class);
    this.servletRequest.setSession(new MockHttpSession(null, "123"));
    this.servletRequest.getSession().setAttribute("foo", "bar");
    this.servletRequest.getSession().setAttribute("bar", "baz");
    HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();
    interceptor.beforeHandshake(this.request, this.response, wsHandler, attributes);
    assertEquals(3, attributes.size());
    assertEquals("bar", attributes.get("foo"));
    assertEquals("baz", attributes.get("bar"));
    assertEquals("123", attributes.get(HttpSessionHandshakeInterceptor.HTTP_SESSION_ID_ATTR_NAME));
}
Also used : HashMap(java.util.HashMap) WebSocketHandler(org.springframework.web.socket.WebSocketHandler) MockHttpSession(org.springframework.mock.web.test.MockHttpSession) Test(org.junit.Test)

Example 3 with MockHttpSession

use of org.springframework.mock.web.test.MockHttpSession in project spring-framework by spring-projects.

the class HttpSessionHandshakeInterceptorTests method constructorWithAttributeNames.

@Test
public void constructorWithAttributeNames() throws Exception {
    Map<String, Object> attributes = new HashMap<>();
    WebSocketHandler wsHandler = Mockito.mock(WebSocketHandler.class);
    this.servletRequest.setSession(new MockHttpSession(null, "123"));
    this.servletRequest.getSession().setAttribute("foo", "bar");
    this.servletRequest.getSession().setAttribute("bar", "baz");
    Set<String> names = Collections.singleton("foo");
    HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor(names);
    interceptor.beforeHandshake(this.request, this.response, wsHandler, attributes);
    assertEquals(2, attributes.size());
    assertEquals("bar", attributes.get("foo"));
    assertEquals("123", attributes.get(HttpSessionHandshakeInterceptor.HTTP_SESSION_ID_ATTR_NAME));
}
Also used : HashMap(java.util.HashMap) WebSocketHandler(org.springframework.web.socket.WebSocketHandler) MockHttpSession(org.springframework.mock.web.test.MockHttpSession) Test(org.junit.Test)

Example 4 with MockHttpSession

use of org.springframework.mock.web.test.MockHttpSession in project spring-framework by spring-projects.

the class HttpSessionHandshakeInterceptorTests method doNotCopyHttpSessionId.

@Test
public void doNotCopyHttpSessionId() throws Exception {
    Map<String, Object> attributes = new HashMap<>();
    WebSocketHandler wsHandler = Mockito.mock(WebSocketHandler.class);
    this.servletRequest.setSession(new MockHttpSession(null, "123"));
    this.servletRequest.getSession().setAttribute("foo", "bar");
    HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();
    interceptor.setCopyHttpSessionId(false);
    interceptor.beforeHandshake(this.request, this.response, wsHandler, attributes);
    assertEquals(1, attributes.size());
    assertEquals("bar", attributes.get("foo"));
}
Also used : HashMap(java.util.HashMap) WebSocketHandler(org.springframework.web.socket.WebSocketHandler) MockHttpSession(org.springframework.mock.web.test.MockHttpSession) Test(org.junit.Test)

Example 5 with MockHttpSession

use of org.springframework.mock.web.test.MockHttpSession in project spring-framework by spring-projects.

the class ServletRequestAttributesTests method removeSessionScopedAttribute.

@Test
public void removeSessionScopedAttribute() throws Exception {
    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);
    assertNull(value);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockHttpSession(org.springframework.mock.web.test.MockHttpSession) Test(org.junit.Test)

Aggregations

MockHttpSession (org.springframework.mock.web.test.MockHttpSession)12 Test (org.junit.Test)11 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)7 HashMap (java.util.HashMap)4 DerivedTestBean (org.springframework.tests.sample.beans.DerivedTestBean)4 WebSocketHandler (org.springframework.web.socket.WebSocketHandler)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 TestBean (org.springframework.tests.sample.beans.TestBean)2 Serializable (java.io.Serializable)1 MethodParameter (org.springframework.core.MethodParameter)1