Search in sources :

Example 21 with WebSocketHandler

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

the class OriginHandshakeInterceptorTests method originMatchAll.

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

Example 22 with WebSocketHandler

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

the class OriginHandshakeInterceptorTests method originListMatch.

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

Example 23 with WebSocketHandler

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

the class OriginHandshakeInterceptorTests method originNoMatchWithNullHostileCollection.

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

Example 24 with WebSocketHandler

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

the class OriginHandshakeInterceptorTests method sameOriginNoMatch.

@Test
public void sameOriginNoMatch() throws Exception {
    Map<String, Object> attributes = new HashMap<>();
    WebSocketHandler wsHandler = Mockito.mock(WebSocketHandler.class);
    this.servletRequest.addHeader(HttpHeaders.ORIGIN, "http://mydomain3.com");
    this.servletRequest.setServerName("mydomain2.com");
    OriginHandshakeInterceptor interceptor = new OriginHandshakeInterceptor(Collections.emptyList());
    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)

Example 25 with WebSocketHandler

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

the class WebSocketStompClientTests method getTcpConnection.

@SuppressWarnings("unchecked")
private TcpConnection<byte[]> getTcpConnection() throws Exception {
    WebSocketHandler webSocketHandler = connect();
    webSocketHandler.afterConnectionEstablished(this.webSocketSession);
    return (TcpConnection<byte[]>) webSocketHandler;
}
Also used : WebSocketHandler(org.springframework.web.socket.WebSocketHandler) TcpConnection(org.springframework.messaging.tcp.TcpConnection)

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