Search in sources :

Example 31 with Message

use of org.springframework.messaging.Message in project spring-framework by spring-projects.

the class UserDestinationMessageHandlerTests method handleUnsubscribe.

@Test
public void handleUnsubscribe() {
    given(this.brokerChannel.send(Mockito.any(Message.class))).willReturn(true);
    this.handler.handleMessage(createWith(SimpMessageType.UNSUBSCRIBE, "joe", "123", "/user/queue/foo"));
    ArgumentCaptor<Message> captor = ArgumentCaptor.forClass(Message.class);
    Mockito.verify(this.brokerChannel).send(captor.capture());
    Message message = captor.getValue();
    assertEquals("/queue/foo-user123", SimpMessageHeaderAccessor.getDestination(message.getHeaders()));
}
Also used : Message(org.springframework.messaging.Message) Test(org.junit.Test)

Example 32 with Message

use of org.springframework.messaging.Message in project spring-framework by spring-projects.

the class UserDestinationMessageHandlerTests method handleMessageWithoutActiveSession.

@Test
public void handleMessageWithoutActiveSession() {
    this.handler.setBroadcastDestination("/topic/unresolved");
    given(this.brokerChannel.send(Mockito.any(Message.class))).willReturn(true);
    this.handler.handleMessage(createWith(SimpMessageType.MESSAGE, "joe", "123", "/user/joe/queue/foo"));
    ArgumentCaptor<Message> captor = ArgumentCaptor.forClass(Message.class);
    Mockito.verify(this.brokerChannel).send(captor.capture());
    Message message = captor.getValue();
    SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.wrap(message);
    assertEquals("/topic/unresolved", accessor.getDestination());
    assertEquals("/user/joe/queue/foo", accessor.getFirstNativeHeader(ORIGINAL_DESTINATION));
    // Should ignore our own broadcast to brokerChannel
    this.handler.handleMessage(message);
    Mockito.verifyNoMoreInteractions(this.brokerChannel);
}
Also used : Message(org.springframework.messaging.Message) SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor) Test(org.junit.Test)

Example 33 with Message

use of org.springframework.messaging.Message in project spring-framework by spring-projects.

the class UserDestinationMessageHandlerTests method handleMessageFromBrokerWithActiveSession.

@Test
public void handleMessageFromBrokerWithActiveSession() {
    TestSimpUser simpUser = new TestSimpUser("joe");
    simpUser.addSessions(new TestSimpSession("123"));
    when(this.registry.getUser("joe")).thenReturn(simpUser);
    this.handler.setBroadcastDestination("/topic/unresolved");
    given(this.brokerChannel.send(Mockito.any(Message.class))).willReturn(true);
    StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.MESSAGE);
    accessor.setSessionId("system123");
    accessor.setDestination("/topic/unresolved");
    accessor.setNativeHeader(ORIGINAL_DESTINATION, "/user/joe/queue/foo");
    accessor.setNativeHeader("customHeader", "customHeaderValue");
    accessor.setLeaveMutable(true);
    byte[] payload = "payload".getBytes(StandardCharsets.UTF_8);
    this.handler.handleMessage(MessageBuilder.createMessage(payload, accessor.getMessageHeaders()));
    ArgumentCaptor<Message> captor = ArgumentCaptor.forClass(Message.class);
    Mockito.verify(this.brokerChannel).send(captor.capture());
    assertNotNull(captor.getValue());
    SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.wrap(captor.getValue());
    assertEquals("/queue/foo-user123", headers.getDestination());
    assertEquals("/user/queue/foo", headers.getFirstNativeHeader(ORIGINAL_DESTINATION));
    assertEquals("customHeaderValue", headers.getFirstNativeHeader("customHeader"));
    assertArrayEquals(payload, (byte[]) captor.getValue().getPayload());
}
Also used : Message(org.springframework.messaging.Message) StompHeaderAccessor(org.springframework.messaging.simp.stomp.StompHeaderAccessor) SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor) Test(org.junit.Test)

Example 34 with Message

use of org.springframework.messaging.Message in project spring-framework by spring-projects.

the class UserDestinationMessageHandlerTests method handleMessage.

@Test
public void handleMessage() {
    TestSimpUser simpUser = new TestSimpUser("joe");
    simpUser.addSessions(new TestSimpSession("123"));
    when(this.registry.getUser("joe")).thenReturn(simpUser);
    given(this.brokerChannel.send(Mockito.any(Message.class))).willReturn(true);
    this.handler.handleMessage(createWith(SimpMessageType.MESSAGE, "joe", "123", "/user/joe/queue/foo"));
    ArgumentCaptor<Message> captor = ArgumentCaptor.forClass(Message.class);
    Mockito.verify(this.brokerChannel).send(captor.capture());
    SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.wrap(captor.getValue());
    assertEquals("/queue/foo-user123", accessor.getDestination());
    assertEquals("/user/queue/foo", accessor.getFirstNativeHeader(ORIGINAL_DESTINATION));
}
Also used : Message(org.springframework.messaging.Message) SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor) Test(org.junit.Test)

Example 35 with Message

use of org.springframework.messaging.Message in project spring-framework by spring-projects.

the class StompDecoderTests method decodeMultipleFramesFromSameBuffer.

@Test
public void decodeMultipleFramesFromSameBuffer() {
    String frame1 = "SEND\ndestination:test\n\nThe body of the message\0";
    String frame2 = "DISCONNECT\n\n\0";
    ByteBuffer buffer = ByteBuffer.wrap((frame1 + frame2).getBytes());
    final List<Message<byte[]>> messages = decoder.decode(buffer);
    assertEquals(2, messages.size());
    assertEquals(StompCommand.SEND, StompHeaderAccessor.wrap(messages.get(0)).getCommand());
    assertEquals(StompCommand.DISCONNECT, StompHeaderAccessor.wrap(messages.get(1)).getCommand());
}
Also used : Message(org.springframework.messaging.Message) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Aggregations

Message (org.springframework.messaging.Message)51 Test (org.junit.Test)41 SimpMessageHeaderAccessor (org.springframework.messaging.simp.SimpMessageHeaderAccessor)16 MessageHeaders (org.springframework.messaging.MessageHeaders)14 StompHeaderAccessor (org.springframework.messaging.simp.stomp.StompHeaderAccessor)9 BinaryMessage (org.springframework.web.socket.BinaryMessage)9 TextMessage (org.springframework.web.socket.TextMessage)9 WebSocketMessage (org.springframework.web.socket.WebSocketMessage)6 ByteBuffer (java.nio.ByteBuffer)5 MessageChannel (org.springframework.messaging.MessageChannel)4 MessageHeaderAccessor (org.springframework.messaging.support.MessageHeaderAccessor)4 Method (java.lang.reflect.Method)3 MethodParameter (org.springframework.core.MethodParameter)3 TestPrincipal (org.springframework.messaging.simp.TestPrincipal)3 Principal (java.security.Principal)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 TextMessage (javax.jms.TextMessage)2 SynthesizingMethodParameter (org.springframework.core.annotation.SynthesizingMethodParameter)2 StubTextMessage (org.springframework.jms.StubTextMessage)2 MessagingException (org.springframework.messaging.MessagingException)2