@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); }
@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")); }
@Before public void setup() { this.delegate = mock(WebSocketHandler.class); this.decorator = new ExceptionWebSocketHandlerDecorator(this.delegate); this.session = new TestWebSocketSession(); this.session.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 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()); }
@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()); }
@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 @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")); }
instanceOf(BarTestInterceptor.class), instanceOf(OriginHandshakeInterceptor.class))); WebSocketSession session = new TestWebSocketSession("id"); wsHttpRequestHandler.getWebSocketHandler().afterConnectionEstablished(session); assertEquals(true, session.getAttributes().get("decorated"));