Search in sources :

Example 11 with TestPrincipal

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

the class SimpMessageHeaderAccessorTests method getDetailedLogMessageWithValuesSet.

@Test
public void getDetailedLogMessageWithValuesSet() {
    SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create();
    accessor.setDestination("/destination");
    accessor.setSubscriptionId("subscription");
    accessor.setSessionId("session");
    accessor.setUser(new TestPrincipal("user"));
    accessor.setSessionAttributes(Collections.<String, Object>singletonMap("key", "value"));
    accessor.setNativeHeader("nativeKey", "nativeValue");
    assertThat(accessor.getDetailedLogMessage("p")).isEqualTo(("MESSAGE destination=/destination subscriptionId=subscription " + "session=session user=user attributes={key=value} nativeHeaders=" + "{nativeKey=[nativeValue]} payload=p"));
}
Also used : TestPrincipal(org.springframework.core.testfixture.security.TestPrincipal) Test(org.junit.jupiter.api.Test)

Example 12 with TestPrincipal

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

the class DefaultUserDestinationResolverTests method handleSubscribeInvalidUserName.

// gh-23836
@Test
public void handleSubscribeInvalidUserName() {
    TestPrincipal user = new TestPrincipal("joe%2F");
    String sourceDestination = "/user/queue/foo";
    Message<?> message = createMessage(SimpMessageType.SUBSCRIBE, user, "123", sourceDestination);
    assertThatIllegalArgumentException().isThrownBy(() -> this.resolver.resolveDestination(message));
}
Also used : TestPrincipal(org.springframework.core.testfixture.security.TestPrincipal) Test(org.junit.jupiter.api.Test)

Example 13 with TestPrincipal

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

the class DefaultUserDestinationResolverTests method handleMessageToOtherUser.

// SPR-12444
@Test
public void handleMessageToOtherUser() {
    TestSimpUser otherSimpUser = new TestSimpUser("anna");
    otherSimpUser.addSessions(new TestSimpSession("456"));
    given(this.registry.getUser("anna")).willReturn(otherSimpUser);
    TestPrincipal user = new TestPrincipal("joe");
    TestPrincipal otherUser = new TestPrincipal("anna");
    String sourceDestination = "/user/anna/queue/foo";
    Message<?> message = createMessage(SimpMessageType.MESSAGE, user, "456", sourceDestination);
    UserDestinationResult actual = this.resolver.resolveDestination(message);
    assertThat(actual.getSourceDestination()).isEqualTo(sourceDestination);
    assertThat(actual.getTargetDestinations().size()).isEqualTo(1);
    assertThat(actual.getTargetDestinations().iterator().next()).isEqualTo("/queue/foo-user456");
    assertThat(actual.getSubscribeDestination()).isEqualTo("/user/queue/foo");
    assertThat(actual.getUser()).isEqualTo(otherUser.getName());
}
Also used : TestPrincipal(org.springframework.core.testfixture.security.TestPrincipal) Test(org.junit.jupiter.api.Test)

Example 14 with TestPrincipal

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

the class DefaultUserDestinationResolverTests method handleSubscribeOneUserMultipleSessions.

// SPR-11325
@Test
public void handleSubscribeOneUserMultipleSessions() {
    TestSimpUser simpUser = new TestSimpUser("joe");
    simpUser.addSessions(new TestSimpSession("123"), new TestSimpSession("456"));
    given(this.registry.getUser("joe")).willReturn(simpUser);
    TestPrincipal user = new TestPrincipal("joe");
    Message<?> message = createMessage(SimpMessageType.SUBSCRIBE, user, "456", "/user/queue/foo");
    UserDestinationResult actual = this.resolver.resolveDestination(message);
    assertThat(actual.getTargetDestinations().size()).isEqualTo(1);
    assertThat(actual.getTargetDestinations().iterator().next()).isEqualTo("/queue/foo-user456");
}
Also used : TestPrincipal(org.springframework.core.testfixture.security.TestPrincipal) Test(org.junit.jupiter.api.Test)

Example 15 with TestPrincipal

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

the class DefaultUserDestinationResolverTests method handleMessage.

@Test
public void handleMessage() {
    TestPrincipal user = new TestPrincipal("joe");
    String sourceDestination = "/user/joe/queue/foo";
    Message<?> message = createMessage(SimpMessageType.MESSAGE, user, "123", sourceDestination);
    UserDestinationResult actual = this.resolver.resolveDestination(message);
    assertThat(actual.getSourceDestination()).isEqualTo(sourceDestination);
    assertThat(actual.getTargetDestinations().size()).isEqualTo(1);
    assertThat(actual.getTargetDestinations().iterator().next()).isEqualTo("/queue/foo-user123");
    assertThat(actual.getSubscribeDestination()).isEqualTo("/user/queue/foo");
    assertThat(actual.getUser()).isEqualTo(user.getName());
}
Also used : TestPrincipal(org.springframework.core.testfixture.security.TestPrincipal) 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