Search in sources :

Example 16 with WebSocketHandler

use of org.springframework.web.socket.WebSocketHandler 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 17 with WebSocketHandler

use of org.springframework.web.socket.WebSocketHandler 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 18 with WebSocketHandler

use of org.springframework.web.socket.WebSocketHandler 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 19 with WebSocketHandler

use of org.springframework.web.socket.WebSocketHandler 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 20 with WebSocketHandler

use of org.springframework.web.socket.WebSocketHandler in project spring-framework by spring-projects.

the class OriginHandshakeInterceptorTests method originValueNoMatch.

@Test
public void originValueNoMatch() throws Exception {
    Map<String, Object> attributes = new HashMap<>();
    WebSocketHandler wsHandler = Mockito.mock(WebSocketHandler.class);
    this.servletRequest.addHeader(HttpHeaders.ORIGIN, "http://mydomain1.com");
    List<String> allowed = Collections.singletonList("http://mydomain2.com");
    OriginHandshakeInterceptor interceptor = new OriginHandshakeInterceptor(allowed);
    assertFalse(interceptor.beforeHandshake(request, response, wsHandler, attributes));
    assertEquals(servletResponse.getStatus(), HttpStatus.FORBIDDEN.value());
}
Also used : HashMap(java.util.HashMap) WebSocketHandler(org.springframework.web.socket.WebSocketHandler) Test(org.junit.Test)

Aggregations

WebSocketHandler (org.springframework.web.socket.WebSocketHandler)44 Test (org.junit.Test)36 HashMap (java.util.HashMap)14 TextWebSocketHandler (org.springframework.web.socket.handler.TextWebSocketHandler)14 WebSocketHttpHeaders (org.springframework.web.socket.WebSocketHttpHeaders)5 SubProtocolWebSocketHandler (org.springframework.web.socket.messaging.SubProtocolWebSocketHandler)5 MockHttpSession (org.springframework.mock.web.test.MockHttpSession)4 TextMessage (org.springframework.web.socket.TextMessage)4 HandshakeHandler (org.springframework.web.socket.server.HandshakeHandler)4 HttpSessionHandshakeInterceptor (org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor)4 SimpleUrlHandlerMapping (org.springframework.web.servlet.handler.SimpleUrlHandlerMapping)3 HandshakeInterceptor (org.springframework.web.socket.server.HandshakeInterceptor)3 DefaultHandshakeHandler (org.springframework.web.socket.server.support.DefaultHandshakeHandler)3 URI (java.net.URI)2 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)2 MessageHandler (org.springframework.messaging.MessageHandler)2 SimpAnnotationMethodMessageHandler (org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler)2 SimpleBrokerMessageHandler (org.springframework.messaging.simp.broker.SimpleBrokerMessageHandler)2 StompBrokerRelayMessageHandler (org.springframework.messaging.simp.stomp.StompBrokerRelayMessageHandler)2 DefaultUserDestinationResolver (org.springframework.messaging.simp.user.DefaultUserDestinationResolver)2