Search in sources :

Example 31 with TextMessage

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

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

the class WebSocketServerSockJsSessionTests method handleMessageBadData.

@Test
public void handleMessageBadData() throws Exception {
    TextMessage message = new TextMessage("[\"x]");
    this.session.handleMessage(message, this.webSocketSession);
    this.session.isClosed();
    verify(this.webSocketHandler).handleTransportError(same(this.session), any(IOException.class));
    verifyNoMoreInteractions(this.webSocketHandler);
}
Also used : IOException(java.io.IOException) TextMessage(org.springframework.web.socket.TextMessage) Test(org.junit.Test)

Example 33 with TextMessage

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

the class WebSocketServerSockJsSessionTests method handleMessage.

@Test
public void handleMessage() throws Exception {
    TextMessage message = new TextMessage("[\"x\"]");
    this.session.handleMessage(message, this.webSocketSession);
    verify(this.webSocketHandler).handleMessage(this.session, new TextMessage("x"));
    verifyNoMoreInteractions(this.webSocketHandler);
}
Also used : TextMessage(org.springframework.web.socket.TextMessage) Test(org.junit.Test)

Example 34 with TextMessage

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

the class WebSocketServerSockJsSessionTests method afterSessionInitializedOpenFrameFirst.

@Test
@SuppressWarnings("resource")
public void afterSessionInitializedOpenFrameFirst() throws Exception {
    TextWebSocketHandler handler = new TextWebSocketHandler() {

        @Override
        public void afterConnectionEstablished(WebSocketSession session) throws Exception {
            session.sendMessage(new TextMessage("go go"));
        }
    };
    TestWebSocketServerSockJsSession session = new TestWebSocketServerSockJsSession(this.sockJsConfig, handler, null);
    session.initializeDelegateSession(this.webSocketSession);
    List<TextMessage> expected = Arrays.asList(new TextMessage("o"), new TextMessage("a[\"go go\"]"));
    assertEquals(expected, this.webSocketSession.getSentMessages());
}
Also used : TextWebSocketHandler(org.springframework.web.socket.handler.TextWebSocketHandler) TextMessage(org.springframework.web.socket.TextMessage) WebSocketSession(org.springframework.web.socket.WebSocketSession) TestWebSocketSession(org.springframework.web.socket.handler.TestWebSocketSession) TestWebSocketServerSockJsSession(org.springframework.web.socket.sockjs.transport.session.WebSocketServerSockJsSessionTests.TestWebSocketServerSockJsSession) Test(org.junit.Test)

Example 35 with TextMessage

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

the class SockJsSessionTests method delegateMessages.

@Test
public void delegateMessages() throws Exception {
    String msg1 = "message 1";
    String msg2 = "message 2";
    this.session.delegateMessages(msg1, msg2);
    verify(this.webSocketHandler).handleMessage(this.session, new TextMessage(msg1));
    verify(this.webSocketHandler).handleMessage(this.session, new TextMessage(msg2));
    verifyNoMoreInteractions(this.webSocketHandler);
}
Also used : TextMessage(org.springframework.web.socket.TextMessage) Test(org.junit.Test)

Aggregations

TextMessage (org.springframework.web.socket.TextMessage)67 Test (org.junit.Test)48 StompHeaderAccessor (org.springframework.messaging.simp.stomp.StompHeaderAccessor)16 WebSocketSession (org.springframework.web.socket.WebSocketSession)11 IOException (java.io.IOException)8 BinaryMessage (org.springframework.web.socket.BinaryMessage)8 SimpMessageHeaderAccessor (org.springframework.messaging.simp.SimpMessageHeaderAccessor)7 Message (org.springframework.messaging.Message)6 URI (java.net.URI)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 StompEncoder (org.springframework.messaging.simp.stomp.StompEncoder)4 WebSocketHandler (org.springframework.web.socket.WebSocketHandler)4 WebSocketMessage (org.springframework.web.socket.WebSocketMessage)4 ClientHttpResponse (org.springframework.http.client.ClientHttpResponse)3 MessageChannel (org.springframework.messaging.MessageChannel)3 ExecutorSubscribableChannel (org.springframework.messaging.support.ExecutorSubscribableChannel)3 ImmutableMessageChannelInterceptor (org.springframework.messaging.support.ImmutableMessageChannelInterceptor)3 CloseStatus (org.springframework.web.socket.CloseStatus)3 TestWebSocketSession (org.springframework.web.socket.handler.TestWebSocketSession)3 SockJsMessageDeliveryException (org.springframework.web.socket.sockjs.SockJsMessageDeliveryException)3