Search in sources :

Example 6 with StompHeaderAccessor

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

the class StompSubProtocolErrorHandlerTests method handleClientMessageProcessingErrorWithReceipt.

@Test
public void handleClientMessageProcessingErrorWithReceipt() throws Exception {
    String receiptId = "123";
    StompHeaderAccessor clientHeaderAccessor = StompHeaderAccessor.create(StompCommand.SEND);
    clientHeaderAccessor.setReceipt(receiptId);
    MessageHeaders clientHeaders = clientHeaderAccessor.getMessageHeaders();
    Message<byte[]> clientMessage = MessageBuilder.createMessage(new byte[0], clientHeaders);
    Message<byte[]> actual = this.handler.handleClientMessageProcessingError(clientMessage, new Exception());
    StompHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(actual, StompHeaderAccessor.class);
    assertNotNull(accessor);
    assertEquals(receiptId, accessor.getReceiptId());
}
Also used : StompHeaderAccessor(org.springframework.messaging.simp.stomp.StompHeaderAccessor) MessageHeaders(org.springframework.messaging.MessageHeaders) Test(org.junit.Test)

Example 7 with StompHeaderAccessor

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

the class StompSubProtocolHandlerTests method handleMessageToClientWithBinaryWebSocketMessage.

// SPR-12475
@Test
public void handleMessageToClientWithBinaryWebSocketMessage() {
    StompHeaderAccessor headers = StompHeaderAccessor.create(StompCommand.MESSAGE);
    headers.setMessageId("mess0");
    headers.setSubscriptionId("sub0");
    headers.setContentType(MimeTypeUtils.APPLICATION_OCTET_STREAM);
    headers.setDestination("/queue/foo");
    // Non-empty payload
    byte[] payload = new byte[1];
    Message<byte[]> message = MessageBuilder.createMessage(payload, headers.getMessageHeaders());
    this.protocolHandler.handleMessageToClient(this.session, message);
    assertEquals(1, this.session.getSentMessages().size());
    WebSocketMessage<?> webSocketMessage = this.session.getSentMessages().get(0);
    assertTrue(webSocketMessage instanceof BinaryMessage);
    // Empty payload
    payload = EMPTY_PAYLOAD;
    message = MessageBuilder.createMessage(payload, headers.getMessageHeaders());
    this.protocolHandler.handleMessageToClient(this.session, message);
    assertEquals(2, this.session.getSentMessages().size());
    webSocketMessage = this.session.getSentMessages().get(1);
    assertTrue(webSocketMessage instanceof TextMessage);
}
Also used : StompHeaderAccessor(org.springframework.messaging.simp.stomp.StompHeaderAccessor) BinaryMessage(org.springframework.web.socket.BinaryMessage) TextMessage(org.springframework.web.socket.TextMessage) Test(org.junit.Test)

Example 8 with StompHeaderAccessor

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

the class StompSubProtocolHandlerTests method handleMessageToClientWithSimpConnectAck.

@Test
public void handleMessageToClientWithSimpConnectAck() {
    StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.CONNECT);
    accessor.setHeartbeat(10000, 10000);
    accessor.setAcceptVersion("1.0,1.1");
    Message<?> connectMessage = MessageBuilder.createMessage(EMPTY_PAYLOAD, accessor.getMessageHeaders());
    SimpMessageHeaderAccessor ackAccessor = SimpMessageHeaderAccessor.create(SimpMessageType.CONNECT_ACK);
    ackAccessor.setHeader(SimpMessageHeaderAccessor.CONNECT_MESSAGE_HEADER, connectMessage);
    ackAccessor.setHeader(SimpMessageHeaderAccessor.HEART_BEAT_HEADER, new long[] { 15000, 15000 });
    Message<byte[]> ackMessage = MessageBuilder.createMessage(EMPTY_PAYLOAD, ackAccessor.getMessageHeaders());
    this.protocolHandler.handleMessageToClient(this.session, ackMessage);
    assertEquals(1, this.session.getSentMessages().size());
    TextMessage actual = (TextMessage) this.session.getSentMessages().get(0);
    assertEquals("CONNECTED\n" + "version:1.1\n" + "heart-beat:15000,15000\n" + "user-name:joe\n" + "\n" + "", actual.getPayload());
}
Also used : StompHeaderAccessor(org.springframework.messaging.simp.stomp.StompHeaderAccessor) TextMessage(org.springframework.web.socket.TextMessage) SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor) Test(org.junit.Test)

Example 9 with StompHeaderAccessor

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

the class StompSubProtocolHandlerTests method handleMessageToClientWithUserDestination.

@Test
public void handleMessageToClientWithUserDestination() {
    StompHeaderAccessor headers = StompHeaderAccessor.create(StompCommand.MESSAGE);
    headers.setMessageId("mess0");
    headers.setSubscriptionId("sub0");
    headers.setDestination("/queue/foo-user123");
    headers.setNativeHeader(StompHeaderAccessor.ORIGINAL_DESTINATION, "/user/queue/foo");
    Message<byte[]> message = MessageBuilder.createMessage(EMPTY_PAYLOAD, headers.getMessageHeaders());
    this.protocolHandler.handleMessageToClient(this.session, message);
    assertEquals(1, this.session.getSentMessages().size());
    WebSocketMessage<?> textMessage = this.session.getSentMessages().get(0);
    assertTrue(((String) textMessage.getPayload()).contains("destination:/user/queue/foo\n"));
    assertFalse(((String) textMessage.getPayload()).contains(SimpMessageHeaderAccessor.ORIGINAL_DESTINATION));
}
Also used : StompHeaderAccessor(org.springframework.messaging.simp.stomp.StompHeaderAccessor) Test(org.junit.Test)

Example 10 with StompHeaderAccessor

use of org.springframework.messaging.simp.stomp.StompHeaderAccessor 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"), is("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);
    assertEquals(Collections.<WebSocketMessage<?>>emptyList(), session.getSentMessages());
    this.protocolHandler.afterSessionEnded(this.session, CloseStatus.BAD_DATA, testChannel);
    assertEquals(Collections.<WebSocketMessage<?>>emptyList(), this.session.getSentMessages());
    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.Test)

Aggregations

StompHeaderAccessor (org.springframework.messaging.simp.stomp.StompHeaderAccessor)38 Test (org.junit.Test)28 TextMessage (org.springframework.web.socket.TextMessage)19 Message (org.springframework.messaging.Message)10 BinaryMessage (org.springframework.web.socket.BinaryMessage)9 SimpMessageHeaderAccessor (org.springframework.messaging.simp.SimpMessageHeaderAccessor)6 WebSocketMessage (org.springframework.web.socket.WebSocketMessage)5 StompEncoder (org.springframework.messaging.simp.stomp.StompEncoder)4 StompHeaders (org.springframework.messaging.simp.stomp.StompHeaders)3 PongMessage (org.springframework.web.socket.PongMessage)3 Principal (java.security.Principal)2 MessageHeaders (org.springframework.messaging.MessageHeaders)2 SimpAttributes (org.springframework.messaging.simp.SimpAttributes)2 SimpAnnotationMethodMessageHandler (org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler)2 MessageHeaderAccessor (org.springframework.messaging.support.MessageHeaderAccessor)2 ByteBuffer (java.nio.ByteBuffer)1 ApplicationContext (org.springframework.context.ApplicationContext)1 ApplicationEventPublisher (org.springframework.context.ApplicationEventPublisher)1 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)1 ClientHttpResponse (org.springframework.http.client.ClientHttpResponse)1