@Override public void afterConnectionEstablished(WebSocketSession session) throws Exception { WebSocketHandler handler = this.provider.getHandler(); this.handlers.put(session, handler); handler.afterConnectionEstablished(session); }
@Override public void afterConnectionEstablished(WebSocketSession session) { try { getDelegate().afterConnectionEstablished(session); } catch (Throwable ex) { tryCloseWithError(session, ex, logger); } }
@SuppressWarnings("unchecked") private TcpConnection<byte[]> getTcpConnection() throws Exception { WebSocketHandler webSocketHandler = connect(); webSocketHandler.afterConnectionEstablished(this.webSocketSession); return (TcpConnection<byte[]>) webSocketHandler; }
@OnWebSocketConnect public void onWebSocketConnect(Session session) { try { this.wsSession.initializeNativeSession(session); this.webSocketHandler.afterConnectionEstablished(this.wsSession); } catch (Throwable ex) { ExceptionWebSocketHandlerDecorator.tryCloseWithError(this.wsSession, ex, logger); } }
@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); }
@Test public void webSocketConnectionEstablished() throws Exception { connect().afterConnectionEstablished(this.webSocketSession); verify(this.stompSession).afterConnected(notNull()); }
@Test public void onOpen() throws Throwable { this.adapter.onWebSocketConnect(this.session); verify(this.webSocketHandler).afterConnectionEstablished(this.webSocketSession); }
@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 handleInitialRequest() throws Exception { this.session.handleInitialRequest(this.request, this.response, this.frameFormat); assertEquals("hhh\no", this.servletResponse.getContentAsString()); assertTrue(this.servletRequest.isAsyncStarted()); verify(this.webSocketHandler).afterConnectionEstablished(this.session); }
@Test public void afterConnectionEstablished() throws Exception { willThrow(new IllegalStateException("error")) .given(this.delegate).afterConnectionEstablished(this.session); this.decorator.afterConnectionEstablished(this.session); assertEquals(CloseStatus.SERVER_ERROR, this.session.getCloseStatus()); }
@Test public void handleFrameOpenWithWebSocketHandlerException() throws Exception { willThrow(new IllegalStateException("Fake error")).given(this.handler).afterConnectionEstablished(this.session); this.session.handleFrame(SockJsFrame.openFrame().getContent()); assertThat(this.session.isOpen(), is(true)); }
@Test public void close() throws Exception { this.session.handleFrame(SockJsFrame.openFrame().getContent()); this.session.close(); assertThat(this.session.isOpen(), equalTo(false)); assertThat(this.session.disconnectStatus, equalTo(CloseStatus.NORMAL)); verify(this.handler).afterConnectionEstablished(this.session); verifyNoMoreInteractions(this.handler); }
@Test public void handleFrameClose() throws Exception { this.session.handleFrame(SockJsFrame.openFrame().getContent()); this.session.handleFrame(SockJsFrame.closeFrame(1007, "").getContent()); assertThat(this.session.isOpen(), equalTo(false)); assertThat(this.session.disconnectStatus, equalTo(new CloseStatus(1007, ""))); verify(this.handler).afterConnectionEstablished(this.session); verifyNoMoreInteractions(this.handler); }
@Test public void eventSourceTransport() throws Exception { EventSourceTransportHandler transportHandler = new EventSourceTransportHandler(); transportHandler.initialize(this.sockJsConfig); StreamingSockJsSession session = transportHandler.createSession("1", this.webSocketHandler, null); transportHandler.handleRequest(this.request, this.response, this.webSocketHandler, session); assertEquals("text/event-stream;charset=UTF-8", this.response.getHeaders().getContentType().toString()); assertTrue("Streaming request not started", this.servletRequest.isAsyncStarted()); verify(this.webSocketHandler).afterConnectionEstablished(session); }
@Test public void handleFrameOpen() throws Exception { assertThat(this.session.isOpen(), is(false)); this.session.handleFrame(SockJsFrame.openFrame().getContent()); assertThat(this.session.isOpen(), is(true)); assertTrue(this.connectFuture.isDone()); assertThat(this.connectFuture.get(), sameInstance(this.session)); verify(this.handler).afterConnectionEstablished(this.session); verifyNoMoreInteractions(this.handler); }
@Test public void afterTransportClosed() throws Exception { this.session.handleFrame(SockJsFrame.openFrame().getContent()); this.session.afterTransportClosed(CloseStatus.SERVER_ERROR); assertThat(this.session.isOpen(), equalTo(false)); verify(this.handler).afterConnectionEstablished(this.session); verify(this.handler).afterConnectionClosed(this.session, CloseStatus.SERVER_ERROR); verifyNoMoreInteractions(this.handler); }
@Test public void handleRequestXhrStreaming() throws Exception { XhrStreamingTransportHandler transportHandler = new XhrStreamingTransportHandler(); transportHandler.initialize(this.sockJsConfig); AbstractSockJsSession session = transportHandler.createSession("1", this.webSocketHandler, null); transportHandler.handleRequest(this.request, this.response, this.webSocketHandler, session); assertEquals("application/javascript;charset=UTF-8", this.response.getHeaders().getContentType().toString()); assertTrue("Streaming request not started", this.servletRequest.isAsyncStarted()); verify(this.webSocketHandler).afterConnectionEstablished(session); }
@Test public void errorResponseStatus() throws Exception { connect(response(HttpStatus.OK, "o\n"), response(HttpStatus.INTERNAL_SERVER_ERROR, "Oops")); verify(this.webSocketHandler).afterConnectionEstablished(any()); verify(this.webSocketHandler).handleTransportError(any(), any()); verify(this.webSocketHandler).afterConnectionClosed(any(), any()); verifyNoMoreInteractions(this.webSocketHandler); }
@Test public void handleFrameMessage() throws Exception { this.session.handleFrame(SockJsFrame.openFrame().getContent()); this.session.handleFrame(SockJsFrame.messageFrame(CODEC, "foo", "bar").getContent()); verify(this.handler).afterConnectionEstablished(this.session); verify(this.handler).handleMessage(this.session, new TextMessage("foo")); verify(this.handler).handleMessage(this.session, new TextMessage("bar")); verifyNoMoreInteractions(this.handler); }
@Test public void connectReceiveAndClose() throws Exception { String body = "o\n" + "a[\"foo\"]\n" + "c[3000,\"Go away!\"]"; ClientHttpResponse response = response(HttpStatus.OK, body); connect(response); verify(this.webSocketHandler).afterConnectionEstablished(any()); verify(this.webSocketHandler).handleMessage(any(), eq(new TextMessage("foo"))); verify(this.webSocketHandler).afterConnectionClosed(any(), eq(new CloseStatus(3000, "Go away!"))); verifyNoMoreInteractions(this.webSocketHandler); }