Search in sources :

Example 1 with StompEncoder

use of org.springframework.messaging.simp.stomp.StompEncoder in project spring-framework by spring-projects.

the class StompSubProtocolHandlerTests method eventPublication.

@Test
public void eventPublication() {
    TestPublisher publisher = new TestPublisher();
    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);
    headers = StompHeaderAccessor.create(StompCommand.CONNECTED);
    message = MessageBuilder.createMessage(EMPTY_PAYLOAD, headers.getMessageHeaders());
    this.protocolHandler.handleMessageToClient(this.session, message);
    headers = StompHeaderAccessor.create(StompCommand.SUBSCRIBE);
    message = MessageBuilder.createMessage(EMPTY_PAYLOAD, headers.getMessageHeaders());
    textMessage = new TextMessage(new StompEncoder().encode(message));
    this.protocolHandler.handleMessageFromClient(this.session, textMessage, this.channel);
    headers = StompHeaderAccessor.create(StompCommand.UNSUBSCRIBE);
    message = MessageBuilder.createMessage(EMPTY_PAYLOAD, headers.getMessageHeaders());
    textMessage = new TextMessage(new StompEncoder().encode(message));
    this.protocolHandler.handleMessageFromClient(this.session, textMessage, this.channel);
    this.protocolHandler.afterSessionEnded(this.session, CloseStatus.BAD_DATA, this.channel);
    assertThat(publisher.events.size()).as("Unexpected events " + publisher.events).isEqualTo(5);
    assertThat(publisher.events.get(0).getClass()).isEqualTo(SessionConnectEvent.class);
    assertThat(publisher.events.get(1).getClass()).isEqualTo(SessionConnectedEvent.class);
    assertThat(publisher.events.get(2).getClass()).isEqualTo(SessionSubscribeEvent.class);
    assertThat(publisher.events.get(3).getClass()).isEqualTo(SessionUnsubscribeEvent.class);
    assertThat(publisher.events.get(4).getClass()).isEqualTo(SessionDisconnectEvent.class);
}
Also used : 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)

Example 2 with StompEncoder

use of org.springframework.messaging.simp.stomp.StompEncoder in project spring-framework by spring-projects.

the class StompSubProtocolHandlerTests method webSocketScope.

@Test
public void webSocketScope() {
    Runnable runnable = Mockito.mock(Runnable.class);
    SimpAttributes simpAttributes = new SimpAttributes(this.session.getId(), this.session.getAttributes());
    simpAttributes.setAttribute("name", "value");
    simpAttributes.registerDestructionCallback("name", runnable);
    MessageChannel testChannel = new MessageChannel() {

        @Override
        public boolean send(Message<?> message) {
            SimpAttributes simpAttributes = SimpAttributesContextHolder.currentAttributes();
            assertThat(simpAttributes.getAttribute("name")).isEqualTo("value");
            return true;
        }

        @Override
        public boolean send(Message<?> message, long timeout) {
            return false;
        }
    };
    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, testChannel);
    assertThat(session.getSentMessages()).isEqualTo(Collections.<WebSocketMessage<?>>emptyList());
    this.protocolHandler.afterSessionEnded(this.session, CloseStatus.BAD_DATA, testChannel);
    assertThat(this.session.getSentMessages()).isEqualTo(Collections.<WebSocketMessage<?>>emptyList());
    verify(runnable, times(1)).run();
}
Also used : MessageChannel(org.springframework.messaging.MessageChannel) TextMessage(org.springframework.web.socket.TextMessage) Message(org.springframework.messaging.Message) WebSocketMessage(org.springframework.web.socket.WebSocketMessage) BinaryMessage(org.springframework.web.socket.BinaryMessage) SimpAttributes(org.springframework.messaging.simp.SimpAttributes) 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)

Example 3 with StompEncoder

use of org.springframework.messaging.simp.stomp.StompEncoder 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)

Example 4 with StompEncoder

use of org.springframework.messaging.simp.stomp.StompEncoder 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)

Aggregations

Test (org.junit.jupiter.api.Test)4 StompEncoder (org.springframework.messaging.simp.stomp.StompEncoder)4 StompHeaderAccessor (org.springframework.messaging.simp.stomp.StompHeaderAccessor)4 TextMessage (org.springframework.web.socket.TextMessage)4 ApplicationEventPublisher (org.springframework.context.ApplicationEventPublisher)1 ClientHttpResponse (org.springframework.http.client.ClientHttpResponse)1 Message (org.springframework.messaging.Message)1 MessageChannel (org.springframework.messaging.MessageChannel)1 MessageHeaders (org.springframework.messaging.MessageHeaders)1 SimpAttributes (org.springframework.messaging.simp.SimpAttributes)1 BinaryMessage (org.springframework.web.socket.BinaryMessage)1 CloseStatus (org.springframework.web.socket.CloseStatus)1 WebSocketMessage (org.springframework.web.socket.WebSocketMessage)1 Jackson2SockJsMessageCodec (org.springframework.web.socket.sockjs.frame.Jackson2SockJsMessageCodec)1 SockJsFrame (org.springframework.web.socket.sockjs.frame.SockJsFrame)1