@Test @SuppressWarnings("unchecked") public void checkSession() throws Exception { TestWebSocketSession session1 = new TestWebSocketSession("id1"); TestWebSocketSession session2 = new TestWebSocketSession("id2"); session1.setOpen(true); session2.setOpen(true); session1.setAcceptedProtocol("v12.stomp"); session2.setAcceptedProtocol("v12.stomp"); this.webSocketHandler.setProtocolHandlers(Arrays.asList(this.stompHandler)); this.webSocketHandler.afterConnectionEstablished(session1); this.webSocketHandler.afterConnectionEstablished(session2); DirectFieldAccessor handlerAccessor = new DirectFieldAccessor(this.webSocketHandler); Map<String, ?> map = (Map<String, ?>) handlerAccessor.getPropertyValue("sessions"); DirectFieldAccessor session1Accessor = new DirectFieldAccessor(map.get("id1")); DirectFieldAccessor session2Accessor = new DirectFieldAccessor(map.get("id2")); long sixtyOneSecondsAgo = System.currentTimeMillis() - 61 * 1000; handlerAccessor.setPropertyValue("lastSessionCheckTime", sixtyOneSecondsAgo); session1Accessor.setPropertyValue("createTime", sixtyOneSecondsAgo); session2Accessor.setPropertyValue("createTime", sixtyOneSecondsAgo); this.webSocketHandler.start(); this.webSocketHandler.handleMessage(session1, new TextMessage("foo")); assertTrue(session1.isOpen()); assertNull(session1.getCloseStatus()); assertFalse(session2.isOpen()); assertEquals(CloseStatus.SESSION_NOT_RELIABLE, session2.getCloseStatus()); assertNotEquals("lastSessionCheckTime not updated", sixtyOneSecondsAgo, handlerAccessor.getPropertyValue("lastSessionCheckTime")); }
@Test public void send() throws IOException { TestWebSocketSession session = new TestWebSocketSession(); session.setOpen(true); ConcurrentWebSocketSessionDecorator decorator = new ConcurrentWebSocketSessionDecorator(session, 1000, 1024); TextMessage textMessage = new TextMessage("payload"); decorator.sendMessage(textMessage); assertEquals(1, session.getSentMessages().size()); assertEquals(textMessage, session.getSentMessages().get(0)); assertEquals(0, decorator.getBufferSize()); assertEquals(0, decorator.getTimeSinceSendStarted()); assertTrue(session.isOpen()); }
@Override public void sendMessage(WebSocketMessage<?> message) throws IOException { super.sendMessage(message); if (this.nextMessageLatch != null) { this.nextMessageLatch.get().countDown(); } block(); }
@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")); }
@Before public void setup() { super.setUp(); this.webSocketSession = new TestWebSocketSession(); this.webSocketSession.setOpen(true); }
@Before public void setup() { MockitoAnnotations.initMocks(this); this.webSocketHandler = new SubProtocolWebSocketHandler(this.inClientChannel, this.outClientChannel); given(stompHandler.getSupportedProtocols()).willReturn(Arrays.asList("v10.stomp", "v11.stomp", "v12.stomp")); given(mqttHandler.getSupportedProtocols()).willReturn(Arrays.asList("MQTT")); this.session = new TestWebSocketSession(); this.session.setId("1"); this.session.setOpen(true); }
@Test public void handleMessageFromClientWithInvalidStompCommand() { TextMessage textMessage = new TextMessage("FOO\n\n\0"); this.protocolHandler.afterSessionStarted(this.session, this.channel); this.protocolHandler.handleMessageFromClient(this.session, textMessage, this.channel); verifyZeroInteractions(this.channel); assertEquals(1, this.session.getSentMessages().size()); TextMessage actual = (TextMessage) this.session.getSentMessages().get(0); assertTrue(actual.getPayload().startsWith("ERROR")); }
@Test public void webSocketScope() { Runnable runnable = Mockito.mock(Runnable.class); SimpAttributes simpAttributes = new SimpAttributes(this.session.getId(), this.session.getAttributes()); simpAttributes.setAttribute("name", "value"); simpAttributes.registerDestructionCallback("name", runnable); MessageChannel testChannel = new MessageChannel() { @Override public boolean send(Message<?> message) { SimpAttributes simpAttributes = SimpAttributesContextHolder.currentAttributes(); assertThat(simpAttributes.getAttribute("name"), is("value")); return true; } @Override public boolean send(Message<?> message, long timeout) { return false; } }; this.protocolHandler.afterSessionStarted(this.session, this.channel); StompHeaderAccessor headers = StompHeaderAccessor.create(StompCommand.CONNECT); Message<byte[]> message = MessageBuilder.createMessage(EMPTY_PAYLOAD, headers.getMessageHeaders()); TextMessage textMessage = new TextMessage(new StompEncoder().encode(message)); this.protocolHandler.handleMessageFromClient(this.session, textMessage, testChannel); assertEquals(Collections.<WebSocketMessage<?>>emptyList(), session.getSentMessages()); this.protocolHandler.afterSessionEnded(this.session, CloseStatus.BAD_DATA, testChannel); assertEquals(Collections.<WebSocketMessage<?>>emptyList(), this.session.getSentMessages()); verify(runnable, times(1)).run(); }
@Test public void handleMessageToClientWithDestinationUserNameProvider() { this.session.setPrincipal(new UniqueUser("joe")); StompHeaderAccessor headers = StompHeaderAccessor.create(StompCommand.CONNECTED); Message<byte[]> message = MessageBuilder.createMessage(EMPTY_PAYLOAD, headers.getMessageHeaders()); this.protocolHandler.handleMessageToClient(this.session, message); assertEquals(1, this.session.getSentMessages().size()); WebSocketMessage<?> textMessage = this.session.getSentMessages().get(0); assertEquals("CONNECTED\n" + "user-name:joe\n" + "\n" + "\u0000", textMessage.getPayload()); }
@Test public void handleTransportError() throws Exception { Exception exception = new Exception("transport error"); willThrow(new IllegalStateException("error")) .given(this.delegate).handleTransportError(this.session, exception); this.decorator.handleTransportError(this.session, exception); assertEquals(CloseStatus.SERVER_ERROR, this.session.getCloseStatus()); }
@Test(expected = IllegalStateException.class) public void subProtocolNoMatch() throws Exception { this.webSocketHandler.setDefaultProtocolHandler(defaultHandler); this.webSocketHandler.setProtocolHandlers(Arrays.asList(stompHandler, mqttHandler)); this.session.setAcceptedProtocol("wamp"); this.webSocketHandler.afterConnectionEstablished(session); }
@Test public void webSocketHandlerDecorator() throws Exception { ApplicationContext config = createConfig(WebSocketHandlerDecoratorConfig.class); WebSocketHandler handler = config.getBean(SubProtocolWebSocketHandler.class); assertNotNull(handler); SimpleUrlHandlerMapping mapping = (SimpleUrlHandlerMapping) config.getBean("stompWebSocketHandlerMapping"); WebSocketHttpRequestHandler httpHandler = (WebSocketHttpRequestHandler) mapping.getHandlerMap().get("/test"); handler = httpHandler.getWebSocketHandler(); WebSocketSession session = new TestWebSocketSession("id"); handler.afterConnectionEstablished(session); assertEquals(true, session.getAttributes().get("decorated")); }
@Test public void isActive() throws Exception { assertFalse(this.session.isActive()); this.session.initializeDelegateSession(this.webSocketSession); assertTrue(this.session.isActive()); this.webSocketSession.setOpen(false); assertFalse(this.session.isActive()); }
@Test public void afterSessionInitialized() throws Exception { this.session.initializeDelegateSession(this.webSocketSession); assertEquals(Collections.singletonList(new TextMessage("o")), this.webSocketSession.getSentMessages()); assertEquals(Arrays.asList("schedule"), this.session.heartbeatSchedulingEvents); verify(this.webSocketHandler).afterConnectionEstablished(this.session); verifyNoMoreInteractions(this.taskScheduler, this.webSocketHandler); }
@Before public void setup() { this.delegate = mock(WebSocketHandler.class); this.decorator = new ExceptionWebSocketHandlerDecorator(this.delegate); this.session = new TestWebSocketSession(); this.session.setOpen(true); }
@Test public void disconnect() throws Exception { this.session.initializeDelegateSession(this.webSocketSession); this.session.close(CloseStatus.NOT_ACCEPTABLE); assertEquals(CloseStatus.NOT_ACCEPTABLE, this.webSocketSession.getCloseStatus()); }
@Test public void subProtocolMatch() throws Exception { this.webSocketHandler.setProtocolHandlers(Arrays.asList(stompHandler, mqttHandler)); this.session.setAcceptedProtocol("v12.sToMp"); this.webSocketHandler.afterConnectionEstablished(session); verify(this.stompHandler).afterSessionStarted( isA(ConcurrentWebSocketSessionDecorator.class), eq(this.inClientChannel)); verify(this.mqttHandler, times(0)).afterSessionStarted(session, this.inClientChannel); }
@Test public void afterConnectionEstablished() throws Exception { @SuppressWarnings("resource") AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); context.refresh(); EchoHandler.reset(); PerConnectionWebSocketHandler handler = new PerConnectionWebSocketHandler(EchoHandler.class); handler.setBeanFactory(context.getBeanFactory()); WebSocketSession session = new TestWebSocketSession(); handler.afterConnectionEstablished(session); assertEquals(1, EchoHandler.initCount); assertEquals(0, EchoHandler.destroyCount); handler.afterConnectionClosed(session, CloseStatus.NORMAL); assertEquals(1, EchoHandler.initCount); assertEquals(1, EchoHandler.destroyCount); }
@Test public void handleMessageToClientWithConnectedFrame() { StompHeaderAccessor headers = StompHeaderAccessor.create(StompCommand.CONNECTED); Message<byte[]> message = MessageBuilder.createMessage(EMPTY_PAYLOAD, headers.getMessageHeaders()); this.protocolHandler.handleMessageToClient(this.session, message); assertEquals(1, this.session.getSentMessages().size()); WebSocketMessage<?> textMessage = this.session.getSentMessages().get(0); assertEquals("CONNECTED\n" + "user-name:joe\n" + "\n" + "\u0000", textMessage.getPayload()); }
@Test public void clientInboundChannelSendMessage() throws Exception { ApplicationContext config = createConfig(TestChannelConfig.class, TestConfigurer.class); TestChannel channel = config.getBean("clientInboundChannel", TestChannel.class); SubProtocolWebSocketHandler webSocketHandler = config.getBean(SubProtocolWebSocketHandler.class); List<ChannelInterceptor> interceptors = channel.getInterceptors(); assertEquals(ImmutableMessageChannelInterceptor.class, interceptors.get(interceptors.size()-1).getClass()); TestWebSocketSession session = new TestWebSocketSession("s1"); session.setOpen(true); webSocketHandler.afterConnectionEstablished(session); webSocketHandler.handleMessage(session, StompTextMessageBuilder.create(StompCommand.SEND).headers("destination:/foo").build()); Message<?> message = channel.messages.get(0); StompHeaderAccessor accessor = StompHeaderAccessor.getAccessor(message, StompHeaderAccessor.class); assertNotNull(accessor); assertFalse(accessor.isMutable()); assertEquals(SimpMessageType.MESSAGE, accessor.getMessageType()); assertEquals("/foo", accessor.getDestination()); }