Search in sources :

Example 66 with SimpMessageHeaderAccessor

use of org.springframework.messaging.simp.SimpMessageHeaderAccessor in project spring-framework-debug by Joker-5.

the class StompSubProtocolHandlerTests method handleMessageToClientWithSimpHeartbeat.

@Test
public void handleMessageToClientWithSimpHeartbeat() {
    SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create(SimpMessageType.HEARTBEAT);
    accessor.setSessionId("s1");
    accessor.setUser(new TestPrincipal("joe"));
    Message<byte[]> ackMessage = MessageBuilder.createMessage(EMPTY_PAYLOAD, accessor.getMessageHeaders());
    this.protocolHandler.handleMessageToClient(this.session, ackMessage);
    assertThat(this.session.getSentMessages().size()).isEqualTo(1);
    TextMessage actual = (TextMessage) this.session.getSentMessages().get(0);
    assertThat(actual.getPayload()).isEqualTo("\n");
}
Also used : TestPrincipal(org.springframework.core.testfixture.security.TestPrincipal) TextMessage(org.springframework.web.socket.TextMessage) SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor) Test(org.junit.jupiter.api.Test)

Example 67 with SimpMessageHeaderAccessor

use of org.springframework.messaging.simp.SimpMessageHeaderAccessor in project spring-framework-debug by Joker-5.

the class WebSocketAnnotationMethodMessageHandlerTests method globalException.

@Test
public void globalException() throws Exception {
    SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.create();
    headers.setSessionId("session1");
    headers.setSessionAttributes(new ConcurrentHashMap<>());
    headers.setDestination("/exception");
    Message<?> message = MessageBuilder.withPayload(new byte[0]).setHeaders(headers).build();
    this.messageHandler.handleMessage(message);
    TestControllerAdvice controllerAdvice = this.applicationContext.getBean(TestControllerAdvice.class);
    assertThat(controllerAdvice.isExceptionHandled()).isTrue();
}
Also used : SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor) Test(org.junit.jupiter.api.Test)

Example 68 with SimpMessageHeaderAccessor

use of org.springframework.messaging.simp.SimpMessageHeaderAccessor in project spring-framework-debug by Joker-5.

the class DefaultSimpUserRegistryTests method createMessage.

private Message<byte[]> createMessage(SimpMessageType type, String sessionId, String subscriptionId, String destination) {
    SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create(type);
    accessor.setSessionId(sessionId);
    if (destination != null) {
        accessor.setDestination(destination);
    }
    if (subscriptionId != null) {
        accessor.setSubscriptionId(subscriptionId);
    }
    return MessageBuilder.createMessage(new byte[0], accessor.getMessageHeaders());
}
Also used : SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor)

Example 69 with SimpMessageHeaderAccessor

use of org.springframework.messaging.simp.SimpMessageHeaderAccessor in project spring-framework-debug by Joker-5.

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);
    assertThat(accessor.getSessionId()).isNull();
    assertThat(accessor.getSubscriptionId()).isNull();
    assertThat(accessor.getDestination()).isEqualTo(("/user/" + user.getName() + "/queue/dest"));
}
Also used : Message(org.springframework.messaging.Message) SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor) Test(org.junit.jupiter.api.Test)

Example 70 with SimpMessageHeaderAccessor

use of org.springframework.messaging.simp.SimpMessageHeaderAccessor in project spring-framework-debug by Joker-5.

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);
    assertThat(accessor.getDestination()).isEqualTo("/user/Me myself and I/dest1");
    accessor = getCapturedAccessor(1);
    assertThat(accessor.getDestination()).isEqualTo("/user/Me myself and I/dest2");
}
Also used : Message(org.springframework.messaging.Message) SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor) Test(org.junit.jupiter.api.Test)

Aggregations

SimpMessageHeaderAccessor (org.springframework.messaging.simp.SimpMessageHeaderAccessor)198 Test (org.junit.jupiter.api.Test)103 Message (org.springframework.messaging.Message)50 StompHeaderAccessor (org.springframework.messaging.simp.stomp.StompHeaderAccessor)19 TextMessage (org.springframework.web.socket.TextMessage)16 MessageHeaders (org.springframework.messaging.MessageHeaders)15 Principal (java.security.Principal)13 MessageChannel (org.springframework.messaging.MessageChannel)11 GraphQLMessage (org.activiti.cloud.services.notifications.graphql.ws.api.GraphQLMessage)9 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)8 LinkedHashMap (java.util.LinkedHashMap)7 HashMap (java.util.HashMap)6 TestPrincipal (org.springframework.core.testfixture.security.TestPrincipal)6 List (java.util.List)5 SimpMessageType (org.springframework.messaging.simp.SimpMessageType)5 Map (java.util.Map)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 SimpMessagingTemplate (org.springframework.messaging.simp.SimpMessagingTemplate)4 RoomMessage (com.github.chipolaris.bootforum.messaging.RoomMessage)3 CountDownLatch (java.util.concurrent.CountDownLatch)3