Search in sources :

Example 86 with TextMessage

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

the class RestTemplateXhrTransportTests method connectReceiveAndCloseWithStompFrame.

@Test
public void connectReceiveAndCloseWithStompFrame() throws Exception {
    StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.SEND);
    accessor.setDestination("/destination");
    MessageHeaders headers = accessor.getMessageHeaders();
    Message<byte[]> message = MessageBuilder.createMessage("body".getBytes(StandardCharsets.UTF_8), headers);
    byte[] bytes = new StompEncoder().encode(message);
    TextMessage textMessage = new TextMessage(bytes);
    SockJsFrame frame = SockJsFrame.messageFrame(new Jackson2SockJsMessageCodec(), textMessage.getPayload());
    String body = "o\n" + frame.getContent() + "\n" + "c[3000,\"Go away!\"]";
    ClientHttpResponse response = response(HttpStatus.OK, body);
    connect(response);
    verify(this.webSocketHandler).afterConnectionEstablished(any());
    verify(this.webSocketHandler).handleMessage(any(), eq(textMessage));
    verify(this.webSocketHandler).afterConnectionClosed(any(), eq(new CloseStatus(3000, "Go away!")));
    verifyNoMoreInteractions(this.webSocketHandler);
}
Also used : Jackson2SockJsMessageCodec(org.springframework.web.socket.sockjs.frame.Jackson2SockJsMessageCodec) StompEncoder(org.springframework.messaging.simp.stomp.StompEncoder) StompHeaderAccessor(org.springframework.messaging.simp.stomp.StompHeaderAccessor) MessageHeaders(org.springframework.messaging.MessageHeaders) CloseStatus(org.springframework.web.socket.CloseStatus) SockJsFrame(org.springframework.web.socket.sockjs.frame.SockJsFrame) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse) TextMessage(org.springframework.web.socket.TextMessage) Test(org.junit.jupiter.api.Test)

Example 87 with TextMessage

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

the class SubProtocolWebSocketHandlerTests method checkSession.

@Test
@SuppressWarnings("unchecked")
public void checkSession() throws Exception {
    TestWebSocketSession session1 = new TestWebSocketSession("id1");
    TestWebSocketSession session2 = new TestWebSocketSession("id2");
    session1.setOpen(true);
    session2.setOpen(true);
    session1.setAcceptedProtocol("v12.stomp");
    session2.setAcceptedProtocol("v12.stomp");
    this.webSocketHandler.setProtocolHandlers(Arrays.asList(this.stompHandler));
    this.webSocketHandler.afterConnectionEstablished(session1);
    this.webSocketHandler.afterConnectionEstablished(session2);
    DirectFieldAccessor handlerAccessor = new DirectFieldAccessor(this.webSocketHandler);
    Map<String, ?> map = (Map<String, ?>) handlerAccessor.getPropertyValue("sessions");
    DirectFieldAccessor session1Accessor = new DirectFieldAccessor(map.get("id1"));
    DirectFieldAccessor session2Accessor = new DirectFieldAccessor(map.get("id2"));
    long sixtyOneSecondsAgo = System.currentTimeMillis() - 61 * 1000;
    handlerAccessor.setPropertyValue("lastSessionCheckTime", sixtyOneSecondsAgo);
    session1Accessor.setPropertyValue("createTime", sixtyOneSecondsAgo);
    session2Accessor.setPropertyValue("createTime", sixtyOneSecondsAgo);
    this.webSocketHandler.start();
    this.webSocketHandler.handleMessage(session1, new TextMessage("foo"));
    assertThat(session1.isOpen()).isTrue();
    assertThat(session1.getCloseStatus()).isNull();
    assertThat(session2.isOpen()).isFalse();
    assertThat(session2.getCloseStatus()).isEqualTo(CloseStatus.SESSION_NOT_RELIABLE);
    assertThat(handlerAccessor.getPropertyValue("lastSessionCheckTime")).as("lastSessionCheckTime not updated").isNotEqualTo(sixtyOneSecondsAgo);
}
Also used : TestWebSocketSession(org.springframework.web.socket.handler.TestWebSocketSession) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) Map(java.util.Map) TextMessage(org.springframework.web.socket.TextMessage) Test(org.junit.jupiter.api.Test)

Example 88 with TextMessage

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

the class XhrTransportTests method sendMessageError.

@Test
public void sendMessageError() throws Exception {
    TestXhrTransport transport = new TestXhrTransport();
    transport.sendMessageResponseToReturn = new ResponseEntity<>(HttpStatus.BAD_REQUEST);
    URI url = new URI("https://example.com");
    assertThatExceptionOfType(HttpServerErrorException.class).isThrownBy(() -> transport.executeSendRequest(url, new HttpHeaders(), new TextMessage("payload")));
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) URI(java.net.URI) HttpServerErrorException(org.springframework.web.client.HttpServerErrorException) TextMessage(org.springframework.web.socket.TextMessage) Test(org.junit.jupiter.api.Test)

Example 89 with TextMessage

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

the class SockJsSessionTests method delegateMessagesWithError.

@Test
public void delegateMessagesWithError() throws Exception {
    TestSockJsSession session = new TestSockJsSession("1", this.sockJsConfig, new ExceptionWebSocketHandlerDecorator(this.webSocketHandler), Collections.emptyMap());
    String msg1 = "message 1";
    String msg2 = "message 2";
    String msg3 = "message 3";
    willThrow(new IOException()).given(this.webSocketHandler).handleMessage(session, new TextMessage(msg2));
    session.delegateConnectionEstablished();
    session.delegateMessages(msg1, msg2, msg3);
    verify(this.webSocketHandler).afterConnectionEstablished(session);
    verify(this.webSocketHandler).handleMessage(session, new TextMessage(msg1));
    verify(this.webSocketHandler).handleMessage(session, new TextMessage(msg2));
    verify(this.webSocketHandler).afterConnectionClosed(session, CloseStatus.SERVER_ERROR);
    verifyNoMoreInteractions(this.webSocketHandler);
}
Also used : ExceptionWebSocketHandlerDecorator(org.springframework.web.socket.handler.ExceptionWebSocketHandlerDecorator) IOException(java.io.IOException) TextMessage(org.springframework.web.socket.TextMessage) Test(org.junit.jupiter.api.Test)

Example 90 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.jupiter.api.Test)

Aggregations

TextMessage (org.springframework.web.socket.TextMessage)92 Test (org.junit.jupiter.api.Test)42 WebSocketSession (org.springframework.web.socket.WebSocketSession)20 StompHeaderAccessor (org.springframework.messaging.simp.stomp.StompHeaderAccessor)16 IOException (java.io.IOException)14 Test (org.junit.Test)8 BinaryMessage (org.springframework.web.socket.BinaryMessage)8 SimpMessageHeaderAccessor (org.springframework.messaging.simp.SimpMessageHeaderAccessor)7 URI (java.net.URI)6 Message (org.springframework.messaging.Message)6 WebSocketMessage (org.springframework.web.socket.WebSocketMessage)6 StompEncoder (org.springframework.messaging.simp.stomp.StompEncoder)4 CloseStatus (org.springframework.web.socket.CloseStatus)4 WebSocketHandler (org.springframework.web.socket.WebSocketHandler)4 ArrayList (java.util.ArrayList)3 MessageChannel (org.springframework.messaging.MessageChannel)3 ExecutorSubscribableChannel (org.springframework.messaging.support.ExecutorSubscribableChannel)3 ImmutableMessageChannelInterceptor (org.springframework.messaging.support.ImmutableMessageChannelInterceptor)3 ShellLogResponseMessage (com.ibeiliao.deployment.admin.websocket.request.ShellLogResponseMessage)2 UncheckedIOException (java.io.UncheckedIOException)2