Search in sources :

Example 1 with TestPrincipal

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

the class StandardWebSocketSessionTests method getPrincipalWithConstructorArg.

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

Example 2 with TestPrincipal

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

the class StandardWebSocketSessionTests method getPrincipalWithNativeSession.

@Test
@SuppressWarnings("resource")
public void getPrincipalWithNativeSession() {
    TestPrincipal user = new TestPrincipal("joe");
    Session nativeSession = Mockito.mock(Session.class);
    given(nativeSession.getUserPrincipal()).willReturn(user);
    StandardWebSocketSession session = new StandardWebSocketSession(this.headers, this.attributes, null, null);
    session.initializeNativeSession(nativeSession);
    assertThat(session.getPrincipal()).isSameAs(user);
}
Also used : TestPrincipal(org.springframework.core.testfixture.security.TestPrincipal) Session(jakarta.websocket.Session) Test(org.junit.jupiter.api.Test)

Example 3 with TestPrincipal

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

the class DefaultSockJsServiceTests method handleTransportRequestXhrSendWithDifferentUser.

@Test
public void handleTransportRequestXhrSendWithDifferentUser() throws Exception {
    String sockJsPath = sessionUrlPrefix + "xhr";
    setRequest("POST", sockJsPrefix + sockJsPath);
    this.service.handleRequest(this.request, this.response, sockJsPath, this.wsHandler);
    // session created
    assertThat(this.servletResponse.getStatus()).isEqualTo(200);
    verify(this.xhrHandler).handleRequest(this.request, this.response, this.wsHandler, this.session);
    this.session.setPrincipal(new TestPrincipal("little red riding hood"));
    this.servletRequest.setUserPrincipal(new TestPrincipal("wolf"));
    resetResponse();
    reset(this.xhrSendHandler);
    sockJsPath = sessionUrlPrefix + "xhr_send";
    setRequest("POST", sockJsPrefix + sockJsPath);
    this.service.handleRequest(this.request, this.response, sockJsPath, this.wsHandler);
    assertThat(this.servletResponse.getStatus()).isEqualTo(404);
    verifyNoMoreInteractions(this.xhrSendHandler);
}
Also used : TestPrincipal(org.springframework.core.testfixture.security.TestPrincipal) Test(org.junit.jupiter.api.Test)

Example 4 with TestPrincipal

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

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 5 with TestPrincipal

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

the class StompSubProtocolHandlerTests method setup.

@BeforeEach
public void setup() {
    this.protocolHandler = new StompSubProtocolHandler();
    this.channel = Mockito.mock(MessageChannel.class);
    this.messageCaptor = ArgumentCaptor.forClass(Message.class);
    given(this.channel.send(any())).willReturn(true);
    this.session = new TestWebSocketSession();
    this.session.setId("s1");
    this.session.setPrincipal(new TestPrincipal("joe"));
}
Also used : MessageChannel(org.springframework.messaging.MessageChannel) TextMessage(org.springframework.web.socket.TextMessage) Message(org.springframework.messaging.Message) WebSocketMessage(org.springframework.web.socket.WebSocketMessage) BinaryMessage(org.springframework.web.socket.BinaryMessage) TestWebSocketSession(org.springframework.web.socket.handler.TestWebSocketSession) TestPrincipal(org.springframework.core.testfixture.security.TestPrincipal) BeforeEach(org.junit.jupiter.api.BeforeEach)

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