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);
}
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);
}
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")));
}
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);
}
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);
}
Aggregations