Search in sources :

Example 6 with TestPrincipal

use of org.springframework.core.testfixture.security.TestPrincipal in project spring-framework by spring-projects.

the class DefaultSimpUserRegistryTests method nullSessionId.

@Test
public void nullSessionId() throws Exception {
    DefaultSimpUserRegistry registry = new DefaultSimpUserRegistry();
    TestPrincipal user = new TestPrincipal("joe");
    Message<byte[]> message = createMessage(SimpMessageType.CONNECT_ACK, "123");
    SessionConnectedEvent event = new SessionConnectedEvent(this, message, user);
    registry.onApplicationEvent(event);
    SimpUser simpUser = registry.getUser("joe");
    assertThat(simpUser.getSession(null)).isNull();
}
Also used : TestPrincipal(org.springframework.core.testfixture.security.TestPrincipal) SimpUser(org.springframework.messaging.simp.user.SimpUser) Test(org.junit.jupiter.api.Test)

Example 7 with TestPrincipal

use of org.springframework.core.testfixture.security.TestPrincipal in project spring-framework by spring-projects.

the class SimpleBrokerMessageHandlerTests method subscribeDisconnectPublish.

@Test
public void subscribeDisconnectPublish() {
    String sess1 = "sess1";
    String sess2 = "sess2";
    startSession(sess1);
    startSession(sess2);
    this.messageHandler.handleMessage(createSubscriptionMessage(sess1, "sub1", "/foo"));
    this.messageHandler.handleMessage(createSubscriptionMessage(sess1, "sub2", "/foo"));
    this.messageHandler.handleMessage(createSubscriptionMessage(sess1, "sub3", "/bar"));
    this.messageHandler.handleMessage(createSubscriptionMessage(sess2, "sub1", "/foo"));
    this.messageHandler.handleMessage(createSubscriptionMessage(sess2, "sub2", "/foo"));
    this.messageHandler.handleMessage(createSubscriptionMessage(sess2, "sub3", "/bar"));
    SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.create(SimpMessageType.DISCONNECT);
    headers.setSessionId(sess1);
    headers.setUser(new TestPrincipal("joe"));
    Message<byte[]> message = MessageBuilder.createMessage(new byte[0], headers.getMessageHeaders());
    this.messageHandler.handleMessage(message);
    this.messageHandler.handleMessage(createMessage("/foo", "message1"));
    this.messageHandler.handleMessage(createMessage("/bar", "message2"));
    verify(this.clientOutChannel, times(4)).send(this.messageCaptor.capture());
    Message<?> captured = this.messageCaptor.getAllValues().get(2);
    assertThat(SimpMessageHeaderAccessor.getMessageType(captured.getHeaders())).isEqualTo(SimpMessageType.DISCONNECT_ACK);
    assertThat(captured.getHeaders().get(SimpMessageHeaderAccessor.DISCONNECT_MESSAGE_HEADER)).isSameAs(message);
    assertThat(SimpMessageHeaderAccessor.getSessionId(captured.getHeaders())).isEqualTo(sess1);
    assertThat(SimpMessageHeaderAccessor.getUser(captured.getHeaders()).getName()).isEqualTo("joe");
    assertThat(messageCaptured(sess2, "sub1", "/foo")).isTrue();
    assertThat(messageCaptured(sess2, "sub2", "/foo")).isTrue();
    assertThat(messageCaptured(sess2, "sub3", "/bar")).isTrue();
}
Also used : TestPrincipal(org.springframework.core.testfixture.security.TestPrincipal) SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor) Test(org.junit.jupiter.api.Test)

Example 8 with TestPrincipal

use of org.springframework.core.testfixture.security.TestPrincipal in project spring-framework by spring-projects.

the class SimpleBrokerMessageHandlerTests method startSession.

private Message<String> startSession(String id) {
    this.messageHandler.start();
    Message<String> connectMessage = createConnectMessage(id, new TestPrincipal("joe"), null);
    this.messageHandler.setTaskScheduler(this.taskScheduler);
    this.messageHandler.handleMessage(connectMessage);
    verify(this.clientOutChannel, times(1)).send(this.messageCaptor.capture());
    reset(this.clientOutChannel);
    return connectMessage;
}
Also used : TestPrincipal(org.springframework.core.testfixture.security.TestPrincipal)

Example 9 with TestPrincipal

use of org.springframework.core.testfixture.security.TestPrincipal in project spring-framework by spring-projects.

the class SimpleBrokerMessageHandlerTests method readWriteIntervalCalculation.

@Test
public void readWriteIntervalCalculation() throws Exception {
    this.messageHandler.setHeartbeatValue(new long[] { 1, 1 });
    this.messageHandler.setTaskScheduler(this.taskScheduler);
    this.messageHandler.start();
    ArgumentCaptor<Runnable> taskCaptor = ArgumentCaptor.forClass(Runnable.class);
    verify(this.taskScheduler).scheduleWithFixedDelay(taskCaptor.capture(), eq(1L));
    Runnable heartbeatTask = taskCaptor.getValue();
    assertThat(heartbeatTask).isNotNull();
    String id = "sess1";
    TestPrincipal user = new TestPrincipal("joe");
    Message<String> connectMessage = createConnectMessage(id, user, new long[] { 10000, 10000 });
    this.messageHandler.handleMessage(connectMessage);
    Thread.sleep(10);
    heartbeatTask.run();
    verify(this.clientOutChannel, times(1)).send(this.messageCaptor.capture());
    List<Message<?>> messages = this.messageCaptor.getAllValues();
    assertThat(messages.size()).isEqualTo(1);
    assertThat(messages.get(0).getHeaders().get(SimpMessageHeaderAccessor.MESSAGE_TYPE_HEADER)).isEqualTo(SimpMessageType.CONNECT_ACK);
}
Also used : TestPrincipal(org.springframework.core.testfixture.security.TestPrincipal) Message(org.springframework.messaging.Message) Test(org.junit.jupiter.api.Test)

Example 10 with TestPrincipal

use of org.springframework.core.testfixture.security.TestPrincipal in project spring-framework by spring-projects.

the class SimpleBrokerMessageHandlerTests method writeInactivity.

@Test
public void writeInactivity() throws Exception {
    this.messageHandler.setHeartbeatValue(new long[] { 1, 0 });
    this.messageHandler.setTaskScheduler(this.taskScheduler);
    this.messageHandler.start();
    ArgumentCaptor<Runnable> taskCaptor = ArgumentCaptor.forClass(Runnable.class);
    verify(this.taskScheduler).scheduleWithFixedDelay(taskCaptor.capture(), eq(1L));
    Runnable heartbeatTask = taskCaptor.getValue();
    assertThat(heartbeatTask).isNotNull();
    String id = "sess1";
    TestPrincipal user = new TestPrincipal("joe");
    Message<String> connectMessage = createConnectMessage(id, user, new long[] { 0, 1 });
    this.messageHandler.handleMessage(connectMessage);
    Thread.sleep(10);
    heartbeatTask.run();
    verify(this.clientOutChannel, times(2)).send(this.messageCaptor.capture());
    List<Message<?>> messages = this.messageCaptor.getAllValues();
    assertThat(messages.size()).isEqualTo(2);
    MessageHeaders headers = messages.get(0).getHeaders();
    assertThat(headers.get(SimpMessageHeaderAccessor.MESSAGE_TYPE_HEADER)).isEqualTo(SimpMessageType.CONNECT_ACK);
    headers = messages.get(1).getHeaders();
    assertThat(headers.get(SimpMessageHeaderAccessor.MESSAGE_TYPE_HEADER)).isEqualTo(SimpMessageType.HEARTBEAT);
    assertThat(headers.get(SimpMessageHeaderAccessor.SESSION_ID_HEADER)).isEqualTo(id);
    assertThat(headers.get(SimpMessageHeaderAccessor.USER_HEADER)).isEqualTo(user);
}
Also used : TestPrincipal(org.springframework.core.testfixture.security.TestPrincipal) Message(org.springframework.messaging.Message) MessageHeaders(org.springframework.messaging.MessageHeaders) Test(org.junit.jupiter.api.Test)

Aggregations

TestPrincipal (org.springframework.core.testfixture.security.TestPrincipal)31 Test (org.junit.jupiter.api.Test)27 Message (org.springframework.messaging.Message)4 SimpUser (org.springframework.messaging.simp.user.SimpUser)4 MessageHeaders (org.springframework.messaging.MessageHeaders)2 SimpMessageHeaderAccessor (org.springframework.messaging.simp.SimpMessageHeaderAccessor)2 TextMessage (org.springframework.web.socket.TextMessage)2 Session (jakarta.websocket.Session)1 HashSet (java.util.HashSet)1 Session (org.eclipse.jetty.websocket.api.Session)1 UpgradeRequest (org.eclipse.jetty.websocket.api.UpgradeRequest)1 UpgradeResponse (org.eclipse.jetty.websocket.api.UpgradeResponse)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1 MessageChannel (org.springframework.messaging.MessageChannel)1 SimpSubscription (org.springframework.messaging.simp.user.SimpSubscription)1 BinaryMessage (org.springframework.web.socket.BinaryMessage)1 CloseStatus (org.springframework.web.socket.CloseStatus)1 WebSocketMessage (org.springframework.web.socket.WebSocketMessage)1 TestWebSocketSession (org.springframework.web.socket.handler.TestWebSocketSession)1