Search in sources :

Example 1 with StompSubProtocolHandler

use of org.springframework.web.socket.messaging.StompSubProtocolHandler in project spring-integration by spring-projects.

the class SubProtocolHandlerRegistryTests method testResolveSessionId.

@Test
public void testResolveSessionId() {
    SubProtocolHandlerRegistry subProtocolHandlerRegistry = new SubProtocolHandlerRegistry(new StompSubProtocolHandler());
    Message<String> message = MessageBuilder.withPayload("foo").setHeader(SimpMessageHeaderAccessor.SESSION_ID_HEADER, "TEST_SESSION").build();
    String sessionId = subProtocolHandlerRegistry.resolveSessionId(message);
    assertEquals(sessionId, "TEST_SESSION");
    message = MessageBuilder.withPayload("foo").setHeader("MY_SESSION_ID", "TEST_SESSION").build();
    sessionId = subProtocolHandlerRegistry.resolveSessionId(message);
    assertNull(sessionId);
}
Also used : StompSubProtocolHandler(org.springframework.web.socket.messaging.StompSubProtocolHandler) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 2 with StompSubProtocolHandler

use of org.springframework.web.socket.messaging.StompSubProtocolHandler in project spring-integration by spring-projects.

the class SubProtocolHandlerRegistryTests method testSingleHandler.

@Test
public void testSingleHandler() {
    SubProtocolHandler testProtocolHandler = spy(new StompSubProtocolHandler());
    when(testProtocolHandler.getSupportedProtocols()).thenReturn(Collections.singletonList("foo"));
    SubProtocolHandlerRegistry subProtocolHandlerRegistry = new SubProtocolHandlerRegistry(Collections.<SubProtocolHandler>singletonList(testProtocolHandler));
    WebSocketSession session = mock(WebSocketSession.class);
    when(session.getAcceptedProtocol()).thenReturn("foo", (String) null);
    SubProtocolHandler protocolHandler = subProtocolHandlerRegistry.findProtocolHandler(session);
    assertNotNull(protocolHandler);
    assertSame(protocolHandler, testProtocolHandler);
    protocolHandler = subProtocolHandlerRegistry.findProtocolHandler(session);
    assertNotNull(protocolHandler);
    assertSame(protocolHandler, testProtocolHandler);
}
Also used : StompSubProtocolHandler(org.springframework.web.socket.messaging.StompSubProtocolHandler) StompSubProtocolHandler(org.springframework.web.socket.messaging.StompSubProtocolHandler) SubProtocolHandler(org.springframework.web.socket.messaging.SubProtocolHandler) WebSocketSession(org.springframework.web.socket.WebSocketSession) Test(org.junit.Test)

Example 3 with StompSubProtocolHandler

use of org.springframework.web.socket.messaging.StompSubProtocolHandler in project spring-integration by spring-projects.

the class SubProtocolHandlerRegistryTests method testProtocolHandlers.

@Test
public void testProtocolHandlers() {
    SubProtocolHandler defaultProtocolHandler = mock(SubProtocolHandler.class);
    SubProtocolHandlerRegistry subProtocolHandlerRegistry = new SubProtocolHandlerRegistry(Collections.<SubProtocolHandler>singletonList(new StompSubProtocolHandler()), defaultProtocolHandler);
    WebSocketSession session = mock(WebSocketSession.class);
    when(session.getAcceptedProtocol()).thenReturn("v10.stomp", (String) null);
    SubProtocolHandler protocolHandler = subProtocolHandlerRegistry.findProtocolHandler(session);
    assertNotNull(protocolHandler);
    assertThat(protocolHandler, instanceOf(StompSubProtocolHandler.class));
    protocolHandler = subProtocolHandlerRegistry.findProtocolHandler(session);
    assertNotNull(protocolHandler);
    assertSame(protocolHandler, defaultProtocolHandler);
    assertEquals(subProtocolHandlerRegistry.getSubProtocols(), new StompSubProtocolHandler().getSupportedProtocols());
}
Also used : StompSubProtocolHandler(org.springframework.web.socket.messaging.StompSubProtocolHandler) StompSubProtocolHandler(org.springframework.web.socket.messaging.StompSubProtocolHandler) SubProtocolHandler(org.springframework.web.socket.messaging.SubProtocolHandler) WebSocketSession(org.springframework.web.socket.WebSocketSession) Test(org.junit.Test)

Example 4 with StompSubProtocolHandler

use of org.springframework.web.socket.messaging.StompSubProtocolHandler in project spring-integration by spring-projects.

the class SubProtocolHandlerRegistryTests method testHandlerSelection.

@Test
public void testHandlerSelection() {
    SubProtocolHandler testProtocolHandler = new StompSubProtocolHandler();
    SubProtocolHandlerRegistry subProtocolHandlerRegistry = new SubProtocolHandlerRegistry(testProtocolHandler);
    WebSocketSession session = mock(WebSocketSession.class);
    when(session.getAcceptedProtocol()).thenReturn("foo", "", null);
    try {
        subProtocolHandlerRegistry.findProtocolHandler(session);
        fail("IllegalStateException expected");
    } catch (Exception e) {
        assertThat(e, instanceOf(IllegalStateException.class));
        assertThat(e.getMessage(), containsString("No handler for sub-protocol 'foo'"));
    }
    SubProtocolHandler protocolHandler = subProtocolHandlerRegistry.findProtocolHandler(session);
    assertNotNull(protocolHandler);
    assertSame(protocolHandler, testProtocolHandler);
    protocolHandler = subProtocolHandlerRegistry.findProtocolHandler(session);
    assertNotNull(protocolHandler);
    assertSame(protocolHandler, testProtocolHandler);
}
Also used : StompSubProtocolHandler(org.springframework.web.socket.messaging.StompSubProtocolHandler) StompSubProtocolHandler(org.springframework.web.socket.messaging.StompSubProtocolHandler) SubProtocolHandler(org.springframework.web.socket.messaging.SubProtocolHandler) WebSocketSession(org.springframework.web.socket.WebSocketSession) Test(org.junit.Test)

Example 5 with StompSubProtocolHandler

use of org.springframework.web.socket.messaging.StompSubProtocolHandler in project spring-framework by spring-projects.

the class SockJsWebSocketHandlerTests method getSubProtocols.

@Test
public void getSubProtocols() throws Exception {
    SubscribableChannel channel = mock(SubscribableChannel.class);
    SubProtocolWebSocketHandler handler = new SubProtocolWebSocketHandler(channel, channel);
    StompSubProtocolHandler stompHandler = new StompSubProtocolHandler();
    handler.addProtocolHandler(stompHandler);
    TaskScheduler scheduler = mock(TaskScheduler.class);
    DefaultSockJsService service = new DefaultSockJsService(scheduler);
    WebSocketServerSockJsSession session = new WebSocketServerSockJsSession("1", service, handler, null);
    SockJsWebSocketHandler sockJsHandler = new SockJsWebSocketHandler(service, handler, session);
    assertThat(sockJsHandler.getSubProtocols()).isEqualTo(stompHandler.getSupportedProtocols());
}
Also used : WebSocketServerSockJsSession(org.springframework.web.socket.sockjs.transport.session.WebSocketServerSockJsSession) SubProtocolWebSocketHandler(org.springframework.web.socket.messaging.SubProtocolWebSocketHandler) StompSubProtocolHandler(org.springframework.web.socket.messaging.StompSubProtocolHandler) TaskScheduler(org.springframework.scheduling.TaskScheduler) SubscribableChannel(org.springframework.messaging.SubscribableChannel) Test(org.junit.jupiter.api.Test)

Aggregations

StompSubProtocolHandler (org.springframework.web.socket.messaging.StompSubProtocolHandler)8 SubProtocolHandler (org.springframework.web.socket.messaging.SubProtocolHandler)6 Test (org.junit.Test)4 Test (org.junit.jupiter.api.Test)4 WebSocketSession (org.springframework.web.socket.WebSocketSession)4 SubProtocolWebSocketHandler (org.springframework.web.socket.messaging.SubProtocolWebSocketHandler)3 ArrayList (java.util.ArrayList)1 ScheduledThreadPoolExecutor (java.util.concurrent.ScheduledThreadPoolExecutor)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)1 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)1 ApplicationContext (org.springframework.context.ApplicationContext)1 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)1 MessageHandler (org.springframework.messaging.MessageHandler)1 SubscribableChannel (org.springframework.messaging.SubscribableChannel)1 SimpAnnotationMethodMessageHandler (org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler)1 DefaultSubscriptionRegistry (org.springframework.messaging.simp.broker.DefaultSubscriptionRegistry)1 SimpleBrokerMessageHandler (org.springframework.messaging.simp.broker.SimpleBrokerMessageHandler)1 StompBrokerRelayMessageHandler (org.springframework.messaging.simp.stomp.StompBrokerRelayMessageHandler)1 DefaultUserDestinationResolver (org.springframework.messaging.simp.user.DefaultUserDestinationResolver)1