Search in sources :

Example 76 with TextMessage

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

the class StompWebSocketIntegrationTests method sendMessageToBrokerAndReceiveReplyViaTopic.

// SPR-10930
@ParameterizedWebSocketTest
void sendMessageToBrokerAndReceiveReplyViaTopic(WebSocketTestServer server, WebSocketClient webSocketClient, TestInfo testInfo) throws Exception {
    super.setup(server, webSocketClient, testInfo);
    TextMessage m0 = create(StompCommand.CONNECT).headers("accept-version:1.1").build();
    TextMessage m1 = create(StompCommand.SUBSCRIBE).headers("id:subs1", "destination:/topic/foo").build();
    TextMessage m2 = create(StompCommand.SEND).headers("destination:/topic/foo").body("5").build();
    TestClientWebSocketHandler clientHandler = new TestClientWebSocketHandler(2, m0, m1, m2);
    try (WebSocketSession session = doHandshake(clientHandler, "/ws").get()) {
        assertThat(session).isNotNull();
        assertThat(clientHandler.latch.await(TIMEOUT, TimeUnit.SECONDS)).isTrue();
        String payload = clientHandler.actual.get(1).getPayload();
        assertThat(payload.startsWith("MESSAGE\n")).as("Expected STOMP MESSAGE, got " + payload).isTrue();
    }
}
Also used : TextMessage(org.springframework.web.socket.TextMessage) WebSocketSession(org.springframework.web.socket.WebSocketSession)

Example 77 with TextMessage

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

the class WebSocketStompClientTests method sendWebSocketMessage.

@Test
public void sendWebSocketMessage() throws Exception {
    StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.SEND);
    accessor.setDestination("/topic/foo");
    byte[] payload = "payload".getBytes(StandardCharsets.UTF_8);
    getTcpConnection().send(MessageBuilder.createMessage(payload, accessor.getMessageHeaders()));
    ArgumentCaptor<TextMessage> textMessageCaptor = ArgumentCaptor.forClass(TextMessage.class);
    verify(this.webSocketSession).sendMessage(textMessageCaptor.capture());
    TextMessage textMessage = textMessageCaptor.getValue();
    assertThat(textMessage).isNotNull();
    assertThat(textMessage.getPayload()).isEqualTo("SEND\ndestination:/topic/foo\ncontent-length:7\n\npayload\0");
}
Also used : StompHeaderAccessor(org.springframework.messaging.simp.stomp.StompHeaderAccessor) TextMessage(org.springframework.web.socket.TextMessage) Test(org.junit.jupiter.api.Test)

Example 78 with TextMessage

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

the class WebSocketStompClientTests method handleWebSocketMessage.

@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
public void handleWebSocketMessage() throws Exception {
    String text = "SEND\na:alpha\n\nMessage payload\0";
    connect().handleMessage(this.webSocketSession, new TextMessage(text));
    ArgumentCaptor<Message> captor = ArgumentCaptor.forClass(Message.class);
    verify(this.stompSession).handleMessage(captor.capture());
    Message<byte[]> message = captor.getValue();
    assertThat(message).isNotNull();
    StompHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, StompHeaderAccessor.class);
    StompHeaders headers = StompHeaders.readOnlyStompHeaders(accessor.toNativeHeaderMap());
    assertThat(accessor.getCommand()).isEqualTo(StompCommand.SEND);
    assertThat(headers.getFirst("a")).isEqualTo("alpha");
    assertThat(new String(message.getPayload(), StandardCharsets.UTF_8)).isEqualTo("Message payload");
}
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) StompHeaderAccessor(org.springframework.messaging.simp.stomp.StompHeaderAccessor) StompHeaders(org.springframework.messaging.simp.stomp.StompHeaders) TextMessage(org.springframework.web.socket.TextMessage) Test(org.junit.jupiter.api.Test)

Example 79 with TextMessage

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

the class ExceptionWebSocketHandlerDecoratorTests method handleMessage.

@Test
public void handleMessage() throws Exception {
    TextMessage message = new TextMessage("payload");
    willThrow(new IllegalStateException("error")).given(this.delegate).handleMessage(this.session, message);
    this.decorator.handleMessage(this.session, message);
    assertThat(this.session.getCloseStatus()).isEqualTo(CloseStatus.SERVER_ERROR);
}
Also used : TextMessage(org.springframework.web.socket.TextMessage) Test(org.junit.jupiter.api.Test)

Example 80 with TextMessage

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

the class StompSubProtocolHandlerTests method eventPublicationWithExceptions.

@Test
public void eventPublicationWithExceptions() {
    ApplicationEventPublisher publisher = mock(ApplicationEventPublisher.class);
    this.protocolHandler.setApplicationEventPublisher(publisher);
    this.protocolHandler.afterSessionStarted(this.session, this.channel);
    StompHeaderAccessor headers = StompHeaderAccessor.create(StompCommand.CONNECT);
    Message<byte[]> message = MessageBuilder.createMessage(EMPTY_PAYLOAD, headers.getMessageHeaders());
    TextMessage textMessage = new TextMessage(new StompEncoder().encode(message));
    this.protocolHandler.handleMessageFromClient(this.session, textMessage, this.channel);
    verify(this.channel).send(this.messageCaptor.capture());
    Message<?> actual = this.messageCaptor.getValue();
    assertThat(actual).isNotNull();
    assertThat(StompHeaderAccessor.wrap(actual).getCommand()).isEqualTo(StompCommand.CONNECT);
    reset(this.channel);
    headers = StompHeaderAccessor.create(StompCommand.CONNECTED);
    message = MessageBuilder.createMessage(EMPTY_PAYLOAD, headers.getMessageHeaders());
    this.protocolHandler.handleMessageToClient(this.session, message);
    assertThat(this.session.getSentMessages().size()).isEqualTo(1);
    textMessage = (TextMessage) this.session.getSentMessages().get(0);
    assertThat(textMessage.getPayload()).isEqualTo(("CONNECTED\n" + "user-name:joe\n" + "\n" + "\u0000"));
    this.protocolHandler.afterSessionEnded(this.session, CloseStatus.BAD_DATA, this.channel);
    verify(this.channel).send(this.messageCaptor.capture());
    actual = this.messageCaptor.getValue();
    assertThat(actual).isNotNull();
    StompHeaderAccessor accessor = StompHeaderAccessor.wrap(actual);
    assertThat(accessor.getCommand()).isEqualTo(StompCommand.DISCONNECT);
    assertThat(accessor.getSessionId()).isEqualTo("s1");
    assertThat(accessor.getUser().getName()).isEqualTo("joe");
}
Also used : ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) StompEncoder(org.springframework.messaging.simp.stomp.StompEncoder) StompHeaderAccessor(org.springframework.messaging.simp.stomp.StompHeaderAccessor) 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