Search in sources :

Example 31 with SimpMessageHeaderAccessor

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

the class SimpleBrokerMessageHandlerTests method createConnectMessage.

private Message<String> createConnectMessage(String sessionId, Principal user, long[] heartbeat) {
    SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create(SimpMessageType.CONNECT);
    accessor.setSessionId(sessionId);
    accessor.setUser(user);
    accessor.setHeader(SimpMessageHeaderAccessor.HEART_BEAT_HEADER, heartbeat);
    return MessageBuilder.createMessage("", accessor.getMessageHeaders());
}
Also used : SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor)

Example 32 with SimpMessageHeaderAccessor

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

the class SendToMethodReturnValueHandlerTests method assertResponse.

private void assertResponse(MethodParameter methodParameter, String sessionId, int index, String destination) {
    SimpMessageHeaderAccessor accessor = getCapturedAccessor(index);
    assertEquals(sessionId, accessor.getSessionId());
    assertEquals(destination, accessor.getDestination());
    assertEquals(MIME_TYPE, accessor.getContentType());
    assertNull("Subscription id should not be copied", accessor.getSubscriptionId());
    assertEquals(methodParameter, accessor.getHeader(SimpMessagingTemplate.CONVERSION_HINT_HEADER));
}
Also used : SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor)

Example 33 with SimpMessageHeaderAccessor

use of org.springframework.messaging.simp.SimpMessageHeaderAccessor 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 34 with SimpMessageHeaderAccessor

use of org.springframework.messaging.simp.SimpMessageHeaderAccessor 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 35 with SimpMessageHeaderAccessor

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

Aggregations

SimpMessageHeaderAccessor (org.springframework.messaging.simp.SimpMessageHeaderAccessor)54 Test (org.junit.Test)34 Message (org.springframework.messaging.Message)14 MessageHeaders (org.springframework.messaging.MessageHeaders)5 StompHeaderAccessor (org.springframework.messaging.simp.stomp.StompHeaderAccessor)5 TextMessage (org.springframework.web.socket.TextMessage)5 MessageChannel (org.springframework.messaging.MessageChannel)3 MessageDeliveryException (org.springframework.messaging.MessageDeliveryException)3 TestPrincipal (org.springframework.messaging.simp.TestPrincipal)3 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)3 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)3 HttpRequestHandler (org.springframework.web.HttpRequestHandler)3 Principal (java.security.Principal)2 LinkedHashMap (java.util.LinkedHashMap)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 MissingCsrfTokenException (org.springframework.security.web.csrf.MissingCsrfTokenException)2 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 MessageSendingOperations (org.springframework.messaging.core.MessageSendingOperations)1