@Before public void setup() { this.protocolHandler = new StompSubProtocolHandler(); this.channel = Mockito.mock(MessageChannel.class); this.messageCaptor = ArgumentCaptor.forClass(Message.class); when(this.channel.send(any())).thenReturn(true); this.session = new TestWebSocketSession(); this.session.setId("s1"); this.session.setPrincipal(new TestPrincipal("joe")); }
public WebMvcStompEndpointRegistry(WebSocketHandler webSocketHandler, WebSocketTransportRegistration transportRegistration, TaskScheduler defaultSockJsTaskScheduler) { Assert.notNull(webSocketHandler, "WebSocketHandler is required "); Assert.notNull(transportRegistration, "WebSocketTransportRegistration is required"); this.webSocketHandler = webSocketHandler; this.subProtocolWebSocketHandler = unwrapSubProtocolWebSocketHandler(webSocketHandler); if (transportRegistration.getSendTimeLimit() != null) { this.subProtocolWebSocketHandler.setSendTimeLimit(transportRegistration.getSendTimeLimit()); } if (transportRegistration.getSendBufferSizeLimit() != null) { this.subProtocolWebSocketHandler.setSendBufferSizeLimit(transportRegistration.getSendBufferSizeLimit()); } if (transportRegistration.getTimeToFirstMessage() != null) { this.subProtocolWebSocketHandler.setTimeToFirstMessage(transportRegistration.getTimeToFirstMessage()); } this.stompHandler = new StompSubProtocolHandler(); if (transportRegistration.getMessageSizeLimit() != null) { this.stompHandler.setMessageSizeLimit(transportRegistration.getMessageSizeLimit()); } this.sockJsScheduler = defaultSockJsTaskScheduler; }
@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); assertEquals(stompHandler.getSupportedProtocols(), sockJsHandler.getSubProtocols()); }
@Test public void handleMessageFromClientWithoutImmutableMessageInterceptor() { AtomicReference<Boolean> mutable = new AtomicReference<>(); ExecutorSubscribableChannel channel = new ExecutorSubscribableChannel(); channel.addInterceptor(new ChannelInterceptor() { @Override public Message<?> preSend(Message<?> message, MessageChannel channel) { mutable.set(MessageHeaderAccessor.getAccessor(message, MessageHeaderAccessor.class).isMutable()); return message; } }); StompSubProtocolHandler handler = new StompSubProtocolHandler(); handler.afterSessionStarted(this.session, channel); TextMessage message = StompTextMessageBuilder.create(StompCommand.CONNECT).build(); handler.handleMessageFromClient(this.session, message, channel); assertNotNull(mutable.get()); assertFalse(mutable.get()); }
@Test public void handleMessageFromClientWithImmutableMessageInterceptor() { AtomicReference<Boolean> mutable = new AtomicReference<>(); ExecutorSubscribableChannel channel = new ExecutorSubscribableChannel(); channel.addInterceptor(new ChannelInterceptor() { @Override public Message<?> preSend(Message<?> message, MessageChannel channel) { mutable.set(MessageHeaderAccessor.getAccessor(message, MessageHeaderAccessor.class).isMutable()); return message; } }); channel.addInterceptor(new ImmutableMessageChannelInterceptor()); StompSubProtocolHandler handler = new StompSubProtocolHandler(); handler.afterSessionStarted(this.session, channel); TextMessage message = StompTextMessageBuilder.create(StompCommand.CONNECT).build(); handler.handleMessageFromClient(this.session, message, channel); assertNotNull(mutable.get()); assertTrue(mutable.get()); }
@Test // SPR-14690 public void handleMessageFromClientWithTokenAuthentication() { ExecutorSubscribableChannel channel = new ExecutorSubscribableChannel(); channel.addInterceptor(new AuthenticationInterceptor("__pete__@gmail.com")); channel.addInterceptor(new ImmutableMessageChannelInterceptor()); TestMessageHandler messageHandler = new TestMessageHandler(); channel.subscribe(messageHandler); StompSubProtocolHandler handler = new StompSubProtocolHandler(); handler.afterSessionStarted(this.session, channel); TextMessage wsMessage = StompTextMessageBuilder.create(StompCommand.CONNECT).build(); handler.handleMessageFromClient(this.session, wsMessage, channel); assertEquals(1, messageHandler.getMessages().size()); Message<?> message = messageHandler.getMessages().get(0); Principal user = SimpMessageHeaderAccessor.getUser(message.getHeaders()); assertNotNull(user); assertEquals("__pete__@gmail.com", user.getName()); }
@Bean public SubProtocolHandler stompSubProtocolHandler() { return new StompSubProtocolHandler(); }
@Bean public SubProtocolHandler stompSubProtocolHandler() { return new StompSubProtocolHandler(); }
@Bean public SubProtocolHandler stompSubProtocolHandler() { return new StompSubProtocolHandler(); }
@Bean public SubProtocolHandler stompSubProtocolHandler() { return new StompSubProtocolHandler(); }
@Bean public SubProtocolHandler stompSubProtocolHandler() { return new StompSubProtocolHandler(); }
@Bean public SubProtocolHandler stompSubProtocolHandler() { return new StompSubProtocolHandler(); }
@Bean public SubProtocolHandler stompSubProtocolHandler() { StompSubProtocolHandler stompSubProtocolHandler = new StompSubProtocolHandler(); stompSubProtocolHandler.setEncoder(new ClientStompEncoder()); return stompSubProtocolHandler; }
@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()); }
@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); }
@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); }
@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); }
public WebMvcStompEndpointRegistry(WebSocketHandler webSocketHandler, WebSocketTransportRegistration transportRegistration, TaskScheduler defaultSockJsTaskScheduler) { Assert.notNull(webSocketHandler, "WebSocketHandler is required "); Assert.notNull(transportRegistration, "WebSocketTransportRegistration is required"); this.webSocketHandler = webSocketHandler; this.subProtocolWebSocketHandler = unwrapSubProtocolWebSocketHandler(webSocketHandler); if (transportRegistration.getSendTimeLimit() != null) { this.subProtocolWebSocketHandler.setSendTimeLimit(transportRegistration.getSendTimeLimit()); } if (transportRegistration.getSendBufferSizeLimit() != null) { this.subProtocolWebSocketHandler.setSendBufferSizeLimit(transportRegistration.getSendBufferSizeLimit()); } this.stompHandler = new StompSubProtocolHandler(); if (transportRegistration.getMessageSizeLimit() != null) { this.stompHandler.setMessageSizeLimit(transportRegistration.getMessageSizeLimit()); } this.sockJsScheduler = defaultSockJsTaskScheduler; }
public WebMvcStompEndpointRegistry(WebSocketHandler webSocketHandler, WebSocketTransportRegistration transportRegistration, TaskScheduler defaultSockJsTaskScheduler) { Assert.notNull(webSocketHandler, "WebSocketHandler is required "); Assert.notNull(transportRegistration, "WebSocketTransportRegistration is required"); this.webSocketHandler = webSocketHandler; this.subProtocolWebSocketHandler = unwrapSubProtocolWebSocketHandler(webSocketHandler); if (transportRegistration.getSendTimeLimit() != null) { this.subProtocolWebSocketHandler.setSendTimeLimit(transportRegistration.getSendTimeLimit()); } if (transportRegistration.getSendBufferSizeLimit() != null) { this.subProtocolWebSocketHandler.setSendBufferSizeLimit(transportRegistration.getSendBufferSizeLimit()); } if (transportRegistration.getTimeToFirstMessage() != null) { this.subProtocolWebSocketHandler.setTimeToFirstMessage(transportRegistration.getTimeToFirstMessage()); } this.stompHandler = new StompSubProtocolHandler(); if (transportRegistration.getMessageSizeLimit() != null) { this.stompHandler.setMessageSizeLimit(transportRegistration.getMessageSizeLimit()); } this.sockJsScheduler = defaultSockJsTaskScheduler; }