Search in sources :

Example 26 with WebSocketHandler

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

the class WebSocketStompClientTests method handleWebSocketMessageSplitAcrossTwoMessage.

@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
public void handleWebSocketMessageSplitAcrossTwoMessage() throws Exception {
    WebSocketHandler webSocketHandler = connect();
    String part1 = "SEND\na:alpha\n\nMessage";
    webSocketHandler.handleMessage(this.webSocketSession, new TextMessage(part1));
    verifyNoMoreInteractions(this.stompSession);
    String part2 = " payload\0";
    webSocketHandler.handleMessage(this.webSocketSession, new TextMessage(part2));
    ArgumentCaptor<Message> captor = ArgumentCaptor.forClass(Message.class);
    verify(this.stompSession).handleMessage(captor.capture());
    Message<byte[]> message = captor.getValue();
    assertNotNull(message);
    StompHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, StompHeaderAccessor.class);
    StompHeaders headers = StompHeaders.readOnlyStompHeaders(accessor.toNativeHeaderMap());
    assertEquals(StompCommand.SEND, accessor.getCommand());
    assertEquals("alpha", headers.getFirst("a"));
    assertEquals("Message payload", new String(message.getPayload(), StandardCharsets.UTF_8));
}
Also used : TextMessage(org.springframework.web.socket.TextMessage) PongMessage(org.springframework.web.socket.PongMessage) Message(org.springframework.messaging.Message) BinaryMessage(org.springframework.web.socket.BinaryMessage) WebSocketHandler(org.springframework.web.socket.WebSocketHandler) StompHeaderAccessor(org.springframework.messaging.simp.stomp.StompHeaderAccessor) StompHeaders(org.springframework.messaging.simp.stomp.StompHeaders) TextMessage(org.springframework.web.socket.TextMessage) Test(org.junit.Test)

Example 27 with WebSocketHandler

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

the class DefaultHandshakeHandlerTests method supportedSubProtocols.

@Test
public void supportedSubProtocols() throws Exception {
    this.handshakeHandler.setSupportedProtocols("stomp", "mqtt");
    given(this.upgradeStrategy.getSupportedVersions()).willReturn(new String[] { "13" });
    this.servletRequest.setMethod("GET");
    WebSocketHttpHeaders headers = new WebSocketHttpHeaders(this.request.getHeaders());
    headers.setUpgrade("WebSocket");
    headers.setConnection("Upgrade");
    headers.setSecWebSocketVersion("13");
    headers.setSecWebSocketKey("82/ZS2YHjEnUN97HLL8tbw==");
    headers.setSecWebSocketProtocol("STOMP");
    WebSocketHandler handler = new TextWebSocketHandler();
    Map<String, Object> attributes = Collections.<String, Object>emptyMap();
    this.handshakeHandler.doHandshake(this.request, this.response, handler, attributes);
    verify(this.upgradeStrategy).upgrade(this.request, this.response, "STOMP", Collections.<WebSocketExtension>emptyList(), null, handler, attributes);
}
Also used : WebSocketHttpHeaders(org.springframework.web.socket.WebSocketHttpHeaders) WebSocketHandler(org.springframework.web.socket.WebSocketHandler) TextWebSocketHandler(org.springframework.web.socket.handler.TextWebSocketHandler) TextWebSocketHandler(org.springframework.web.socket.handler.TextWebSocketHandler) Test(org.junit.Test)

Example 28 with WebSocketHandler

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

the class SockJsSessionTests method delegateMessagesWithErrorAndConnectionClosing.

@Test
public void delegateMessagesWithErrorAndConnectionClosing() throws Exception {
    WebSocketHandler wsHandler = new ExceptionWebSocketHandlerDecorator(this.webSocketHandler);
    TestSockJsSession sockJsSession = new TestSockJsSession("1", this.sockJsConfig, wsHandler, Collections.<String, Object>emptyMap());
    String msg1 = "message 1";
    String msg2 = "message 2";
    String msg3 = "message 3";
    willThrow(new IOException()).given(this.webSocketHandler).handleMessage(sockJsSession, new TextMessage(msg2));
    sockJsSession.delegateConnectionEstablished();
    try {
        sockJsSession.delegateMessages(msg1, msg2, msg3);
        fail("expected exception");
    } catch (SockJsMessageDeliveryException ex) {
        assertEquals(Collections.singletonList(msg3), ex.getUndeliveredMessages());
        verify(this.webSocketHandler).afterConnectionEstablished(sockJsSession);
        verify(this.webSocketHandler).handleMessage(sockJsSession, new TextMessage(msg1));
        verify(this.webSocketHandler).handleMessage(sockJsSession, new TextMessage(msg2));
        verify(this.webSocketHandler).afterConnectionClosed(sockJsSession, CloseStatus.SERVER_ERROR);
        verifyNoMoreInteractions(this.webSocketHandler);
    }
}
Also used : ExceptionWebSocketHandlerDecorator(org.springframework.web.socket.handler.ExceptionWebSocketHandlerDecorator) WebSocketHandler(org.springframework.web.socket.WebSocketHandler) SockJsMessageDeliveryException(org.springframework.web.socket.sockjs.SockJsMessageDeliveryException) IOException(java.io.IOException) TextMessage(org.springframework.web.socket.TextMessage) Test(org.junit.Test)

Example 29 with WebSocketHandler

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

the class XhrTransportTests method connect.

@Test
public void connect() throws Exception {
    HttpHeaders handshakeHeaders = new HttpHeaders();
    handshakeHeaders.setOrigin("foo");
    TransportRequest request = mock(TransportRequest.class);
    given(request.getSockJsUrlInfo()).willReturn(new SockJsUrlInfo(new URI("http://example.com")));
    given(request.getHandshakeHeaders()).willReturn(handshakeHeaders);
    TestXhrTransport transport = new TestXhrTransport();
    WebSocketHandler handler = mock(WebSocketHandler.class);
    transport.connect(request, handler);
    ArgumentCaptor<Runnable> captor = ArgumentCaptor.forClass(Runnable.class);
    verify(request).getSockJsUrlInfo();
    verify(request).addTimeoutTask(captor.capture());
    verify(request).getTransportUrl();
    verify(request).getHandshakeHeaders();
    verify(request).getHttpRequestHeaders();
    verifyNoMoreInteractions(request);
    assertEquals(1, transport.actualHandshakeHeaders.size());
    assertEquals("foo", transport.actualHandshakeHeaders.getOrigin());
    assertFalse(transport.actualSession.isDisconnected());
    captor.getValue().run();
    assertTrue(transport.actualSession.isDisconnected());
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) WebSocketHandler(org.springframework.web.socket.WebSocketHandler) URI(java.net.URI) Test(org.junit.Test)

Example 30 with WebSocketHandler

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

the class PerConnectionWebSocketHandler method afterConnectionEstablished.

@Override
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
    WebSocketHandler handler = this.provider.getHandler();
    this.handlers.put(session, handler);
    handler.afterConnectionEstablished(session);
}
Also used : WebSocketHandler(org.springframework.web.socket.WebSocketHandler)

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