Search in sources :

Example 61 with TextMessage

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

the class AbstractSockJsIntegrationTests method testEcho.

private void testEcho(int messageCount, Transport transport, WebSocketHttpHeaders headers) throws Exception {
    List<TextMessage> messages = new ArrayList<>();
    for (int i = 0; i < messageCount; i++) {
        messages.add(new TextMessage("m" + i));
    }
    TestClientHandler handler = new TestClientHandler();
    initSockJsClient(transport);
    URI url = new URI(this.baseUrl + "/echo");
    WebSocketSession session = this.sockJsClient.doHandshake(handler, headers, url).get();
    for (TextMessage message : messages) {
        session.sendMessage(message);
    }
    handler.awaitMessageCount(messageCount, 5000);
    for (TextMessage message : messages) {
        assertTrue("Message not received: " + message, handler.receivedMessages.remove(message));
    }
    assertEquals("Remaining messages: " + handler.receivedMessages, 0, handler.receivedMessages.size());
    session.close();
}
Also used : ArrayList(java.util.ArrayList) URI(java.net.URI) TextMessage(org.springframework.web.socket.TextMessage) WebSocketSession(org.springframework.web.socket.WebSocketSession)

Example 62 with TextMessage

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

the class ClientSockJsSessionTests method handleFrameMessageWithWebSocketHandlerException.

@Test
public void handleFrameMessageWithWebSocketHandlerException() throws Exception {
    this.session.handleFrame(SockJsFrame.openFrame().getContent());
    willThrow(new IllegalStateException("Fake error")).given(this.handler).handleMessage(this.session, new TextMessage("foo"));
    willThrow(new IllegalStateException("Fake error")).given(this.handler).handleMessage(this.session, new TextMessage("bar"));
    this.session.handleFrame(SockJsFrame.messageFrame(CODEC, "foo", "bar").getContent());
    assertThat(this.session.isOpen(), equalTo(true));
    verify(this.handler).afterConnectionEstablished(this.session);
    verify(this.handler).handleMessage(this.session, new TextMessage("foo"));
    verify(this.handler).handleMessage(this.session, new TextMessage("bar"));
    verifyNoMoreInteractions(this.handler);
}
Also used : TextMessage(org.springframework.web.socket.TextMessage) Test(org.junit.Test)

Example 63 with TextMessage

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

the class HttpReceivingTransportHandlerTests method handleRequest.

private void handleRequest(AbstractHttpReceivingTransportHandler transportHandler) throws Exception {
    WebSocketHandler wsHandler = mock(WebSocketHandler.class);
    AbstractSockJsSession session = new TestHttpSockJsSession("1", new StubSockJsServiceConfig(), wsHandler, null);
    transportHandler.initialize(new StubSockJsServiceConfig());
    transportHandler.handleRequest(this.request, this.response, wsHandler, session);
    assertEquals("text/plain;charset=UTF-8", this.response.getHeaders().getContentType().toString());
    verify(wsHandler).handleMessage(session, new TextMessage("x"));
}
Also used : TestHttpSockJsSession(org.springframework.web.socket.sockjs.transport.session.TestHttpSockJsSession) WebSocketHandler(org.springframework.web.socket.WebSocketHandler) AbstractSockJsSession(org.springframework.web.socket.sockjs.transport.session.AbstractSockJsSession) StubSockJsServiceConfig(org.springframework.web.socket.sockjs.transport.session.StubSockJsServiceConfig) TextMessage(org.springframework.web.socket.TextMessage)

Example 64 with TextMessage

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

the class HttpReceivingTransportHandlerTests method delegateMessageException.

@Test
public void delegateMessageException() throws Exception {
    StubSockJsServiceConfig sockJsConfig = new StubSockJsServiceConfig();
    this.servletRequest.setContent("[\"x\"]".getBytes("UTF-8"));
    WebSocketHandler wsHandler = mock(WebSocketHandler.class);
    TestHttpSockJsSession session = new TestHttpSockJsSession("1", sockJsConfig, wsHandler, null);
    session.delegateConnectionEstablished();
    willThrow(new Exception()).given(wsHandler).handleMessage(session, new TextMessage("x"));
    try {
        XhrReceivingTransportHandler transportHandler = new XhrReceivingTransportHandler();
        transportHandler.initialize(sockJsConfig);
        transportHandler.handleRequest(this.request, this.response, wsHandler, session);
        fail("Expected exception");
    } catch (SockJsMessageDeliveryException ex) {
        assertNull(session.getCloseStatus());
    }
}
Also used : TestHttpSockJsSession(org.springframework.web.socket.sockjs.transport.session.TestHttpSockJsSession) WebSocketHandler(org.springframework.web.socket.WebSocketHandler) SockJsMessageDeliveryException(org.springframework.web.socket.sockjs.SockJsMessageDeliveryException) StubSockJsServiceConfig(org.springframework.web.socket.sockjs.transport.session.StubSockJsServiceConfig) SockJsMessageDeliveryException(org.springframework.web.socket.sockjs.SockJsMessageDeliveryException) TextMessage(org.springframework.web.socket.TextMessage) Test(org.junit.Test)

Example 65 with TextMessage

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

the class WebSocketServerSockJsSessionTests method handleMessageEmptyPayload.

@Test
public void handleMessageEmptyPayload() throws Exception {
    this.session.handleMessage(new TextMessage(""), this.webSocketSession);
    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