Search in sources :

Example 36 with Message

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

the class StompDecoderTests method decodeHeartbeat.

@Test
public void decodeHeartbeat() {
    String frame = "\n";
    ByteBuffer buffer = ByteBuffer.wrap(frame.getBytes());
    final List<Message<byte[]>> messages = decoder.decode(buffer);
    assertEquals(1, messages.size());
    assertEquals(SimpMessageType.HEARTBEAT, StompHeaderAccessor.wrap(messages.get(0)).getMessageType());
}
Also used : Message(org.springframework.messaging.Message) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 37 with Message

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

the class SendToMethodReturnValueHandlerTests method sendToWithDestinationPlaceholders.

// SPR-12170
@Test
public void sendToWithDestinationPlaceholders() throws Exception {
    given(this.messageChannel.send(any(Message.class))).willReturn(true);
    Map<String, String> vars = new LinkedHashMap<>(1);
    vars.put("roomName", "roomA");
    String sessionId = "sess1";
    SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create();
    accessor.setSessionId(sessionId);
    accessor.setSubscriptionId("sub1");
    accessor.setHeader(DestinationVariableMethodArgumentResolver.DESTINATION_TEMPLATE_VARIABLES_HEADER, vars);
    Message<?> message = MessageBuilder.createMessage(PAYLOAD, accessor.getMessageHeaders());
    this.handler.handleReturnValue(PAYLOAD, this.sendToWithPlaceholdersReturnType, message);
    verify(this.messageChannel, times(1)).send(this.messageCaptor.capture());
    SimpMessageHeaderAccessor actual = getCapturedAccessor(0);
    assertEquals(sessionId, actual.getSessionId());
    assertEquals("/topic/chat.message.filtered.roomA", actual.getDestination());
}
Also used : Message(org.springframework.messaging.Message) LinkedHashMap(java.util.LinkedHashMap) SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor) Test(org.junit.Test)

Example 38 with Message

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

the class SendToMethodReturnValueHandlerTests method sendToUserWithUserNameProvider.

@Test
public void sendToUserWithUserNameProvider() throws Exception {
    given(this.messageChannel.send(any(Message.class))).willReturn(true);
    String sessionId = "sess1";
    TestUser user = new UniqueUser();
    Message<?> inputMessage = createMessage(sessionId, "sub1", null, null, user);
    this.handler.handleReturnValue(PAYLOAD, this.sendToUserReturnType, inputMessage);
    verify(this.messageChannel, times(2)).send(this.messageCaptor.capture());
    SimpMessageHeaderAccessor accessor = getCapturedAccessor(0);
    assertEquals("/user/Me myself and I/dest1", accessor.getDestination());
    accessor = getCapturedAccessor(1);
    assertEquals("/user/Me myself and I/dest2", accessor.getDestination());
}
Also used : Message(org.springframework.messaging.Message) SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor) Test(org.junit.Test)

Example 39 with Message

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

the class SendToMethodReturnValueHandlerTests method sendToUserDefaultDestination.

@Test
public void sendToUserDefaultDestination() throws Exception {
    given(this.messageChannel.send(any(Message.class))).willReturn(true);
    String sessionId = "sess1";
    TestUser user = new TestUser();
    Message<?> inputMessage = createMessage(sessionId, "sub1", "/app", "/dest", user);
    this.handler.handleReturnValue(PAYLOAD, this.sendToUserDefaultDestReturnType, inputMessage);
    verify(this.messageChannel, times(1)).send(this.messageCaptor.capture());
    SimpMessageHeaderAccessor accessor = getCapturedAccessor(0);
    assertNull(accessor.getSessionId());
    assertNull(accessor.getSubscriptionId());
    assertEquals("/user/" + user.getName() + "/queue/dest", accessor.getDestination());
}
Also used : Message(org.springframework.messaging.Message) SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor) Test(org.junit.Test)

Example 40 with Message

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

the class SimpAnnotationMethodMessageHandlerTests method completableFutureFailure.

@Test
@SuppressWarnings("unchecked")
public void completableFutureFailure() {
    Message emptyMessage = (Message) MessageBuilder.withPayload(new byte[0]).build();
    given(this.channel.send(any(Message.class))).willReturn(true);
    given(this.converter.toMessage(any(), any(MessageHeaders.class))).willReturn(emptyMessage);
    CompletableFutureController controller = new CompletableFutureController();
    this.messageHandler.registerHandler(controller);
    this.messageHandler.setDestinationPrefixes(Arrays.asList("/app1", "/app2/"));
    Message<?> message = createMessage("/app1/completable-future");
    this.messageHandler.handleMessage(message);
    controller.future.completeExceptionally(new IllegalStateException());
    assertTrue(controller.exceptionCaught);
}
Also used : Message(org.springframework.messaging.Message) MessageHeaders(org.springframework.messaging.MessageHeaders) 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