Search in sources :

Example 26 with TestPrincipal

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

the class JettyWebSocketSessionTests method getPrincipalWithConstructorArg.

@Test
@SuppressWarnings("resource")
public void getPrincipalWithConstructorArg() {
    TestPrincipal user = new TestPrincipal("joe");
    JettyWebSocketSession session = new JettyWebSocketSession(attributes, user);
    assertThat(session.getPrincipal()).isSameAs(user);
}
Also used : TestPrincipal(org.springframework.core.testfixture.security.TestPrincipal) Test(org.junit.jupiter.api.Test)

Example 27 with TestPrincipal

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

the class DefaultSimpUserRegistryTests method addOneSessionId.

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

Example 28 with TestPrincipal

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

the class DefaultSimpUserRegistryTests method addMultipleSessionIds.

@Test
public void addMultipleSessionIds() {
    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);
    message = createMessage(SimpMessageType.CONNECT_ACK, "456");
    event = new SessionConnectedEvent(this, message, user);
    registry.onApplicationEvent(event);
    message = createMessage(SimpMessageType.CONNECT_ACK, "789");
    event = new SessionConnectedEvent(this, message, user);
    registry.onApplicationEvent(event);
    SimpUser simpUser = registry.getUser("joe");
    assertThat(simpUser).isNotNull();
    assertThat(registry.getUserCount()).isEqualTo(1);
    assertThat(simpUser.getSessions().size()).isEqualTo(3);
    assertThat(simpUser.getSession("123")).isNotNull();
    assertThat(simpUser.getSession("456")).isNotNull();
    assertThat(simpUser.getSession("789")).isNotNull();
}
Also used : TestPrincipal(org.springframework.core.testfixture.security.TestPrincipal) SimpUser(org.springframework.messaging.simp.user.SimpUser) Test(org.junit.jupiter.api.Test)

Example 29 with TestPrincipal

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

the class DefaultSimpUserRegistryTests method removeSessionIds.

@Test
public void removeSessionIds() {
    DefaultSimpUserRegistry registry = new DefaultSimpUserRegistry();
    TestPrincipal user = new TestPrincipal("joe");
    Message<byte[]> message = createMessage(SimpMessageType.CONNECT_ACK, "123");
    SessionConnectedEvent connectedEvent = new SessionConnectedEvent(this, message, user);
    registry.onApplicationEvent(connectedEvent);
    message = createMessage(SimpMessageType.CONNECT_ACK, "456");
    connectedEvent = new SessionConnectedEvent(this, message, user);
    registry.onApplicationEvent(connectedEvent);
    message = createMessage(SimpMessageType.CONNECT_ACK, "789");
    connectedEvent = new SessionConnectedEvent(this, message, user);
    registry.onApplicationEvent(connectedEvent);
    SimpUser simpUser = registry.getUser("joe");
    assertThat(simpUser).isNotNull();
    assertThat(simpUser.getSessions().size()).isEqualTo(3);
    CloseStatus status = CloseStatus.GOING_AWAY;
    message = createMessage(SimpMessageType.DISCONNECT, "456");
    SessionDisconnectEvent disconnectEvent = new SessionDisconnectEvent(this, message, "456", status, user);
    registry.onApplicationEvent(disconnectEvent);
    message = createMessage(SimpMessageType.DISCONNECT, "789");
    disconnectEvent = new SessionDisconnectEvent(this, message, "789", status, user);
    registry.onApplicationEvent(disconnectEvent);
    assertThat(simpUser.getSessions().size()).isEqualTo(1);
    assertThat(simpUser.getSession("123")).isNotNull();
}
Also used : TestPrincipal(org.springframework.core.testfixture.security.TestPrincipal) SimpUser(org.springframework.messaging.simp.user.SimpUser) CloseStatus(org.springframework.web.socket.CloseStatus) Test(org.junit.jupiter.api.Test)

Example 30 with TestPrincipal

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

the class DefaultSimpUserRegistryTests method findSubscriptions.

@Test
public void findSubscriptions() 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);
    message = createMessage(SimpMessageType.SUBSCRIBE, "123", "sub1", "/match");
    SessionSubscribeEvent subscribeEvent = new SessionSubscribeEvent(this, message, user);
    registry.onApplicationEvent(subscribeEvent);
    message = createMessage(SimpMessageType.SUBSCRIBE, "123", "sub2", "/match");
    subscribeEvent = new SessionSubscribeEvent(this, message, user);
    registry.onApplicationEvent(subscribeEvent);
    message = createMessage(SimpMessageType.SUBSCRIBE, "123", "sub3", "/not-a-match");
    subscribeEvent = new SessionSubscribeEvent(this, message, user);
    registry.onApplicationEvent(subscribeEvent);
    Set<SimpSubscription> matches = registry.findSubscriptions(subscription -> subscription.getDestination().equals("/match"));
    assertThat(matches.size()).isEqualTo(2);
    Iterator<SimpSubscription> iterator = matches.iterator();
    Set<String> sessionIds = new HashSet<>(2);
    sessionIds.add(iterator.next().getId());
    sessionIds.add(iterator.next().getId());
    assertThat(sessionIds).isEqualTo(new HashSet<>(Arrays.asList("sub1", "sub2")));
}
Also used : TestPrincipal(org.springframework.core.testfixture.security.TestPrincipal) SimpSubscription(org.springframework.messaging.simp.user.SimpSubscription) HashSet(java.util.HashSet) 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