@Test public void applyAfterHandshakeOnly() { HandshakeInterceptorChain chain = new HandshakeInterceptorChain(interceptors, wsHandler); chain.applyAfterHandshake(request, response, null); verifyNoMoreInteractions(i1, i2, i3); }
@Test public void scheduleHeartbeatNotActive() throws Exception { this.session.setActive(false); this.session.scheduleHeartbeat(); verifyNoMoreInteractions(this.taskScheduler); }
@Test public void handleTransportError() throws Exception { final IllegalStateException ex = new IllegalStateException("Fake error"); this.session.handleTransportError(ex); verify(this.handler).handleTransportError(this.session, ex); verifyNoMoreInteractions(this.handler); }
@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 handleFrameMessageWithBadData() throws Exception { this.session.handleFrame(SockJsFrame.openFrame().getContent()); this.session.handleFrame("a['bad data"); assertThat(this.session.isOpen(), equalTo(false)); assertThat(this.session.disconnectStatus, equalTo(CloseStatus.BAD_DATA)); verify(this.handler).afterConnectionEstablished(this.session); verifyNoMoreInteractions(this.handler); }
@Test @SuppressWarnings("resource") public void getPrincipalNone() { Session nativeSession = Mockito.mock(Session.class); given(nativeSession.getUserPrincipal()).willReturn(null); StandardWebSocketSession session = new StandardWebSocketSession(this.headers, this.attributes, null, null); session.initializeNativeSession(nativeSession); reset(nativeSession); assertNull(session.getPrincipal()); verifyNoMoreInteractions(nativeSession); }
@Test @SuppressWarnings("resource") public void getAcceptedProtocol() { String protocol = "foo"; Session nativeSession = Mockito.mock(Session.class); given(nativeSession.getNegotiatedSubprotocol()).willReturn(protocol); StandardWebSocketSession session = new StandardWebSocketSession(this.headers, this.attributes, null, null); session.initializeNativeSession(nativeSession); reset(nativeSession); assertEquals(protocol, session.getAcceptedProtocol()); verifyNoMoreInteractions(nativeSession); }
@Test public void sendHeartbeat() throws Exception { this.session.setActive(true); this.session.sendHeartbeat(); assertEquals(1, this.session.getSockJsFramesWritten().size()); assertEquals(SockJsFrame.heartbeatFrame(), this.session.getSockJsFramesWritten().get(0)); verify(this.taskScheduler).schedule(any(Runnable.class), any(Date.class)); verifyNoMoreInteractions(this.taskScheduler); }
@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 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 delegateMessages() throws Exception { String msg1 = "message 1"; String msg2 = "message 2"; this.session.delegateMessages(msg1, msg2); verify(this.webSocketHandler).handleMessage(this.session, new TextMessage(msg1)); verify(this.webSocketHandler).handleMessage(this.session, new TextMessage(msg2)); verifyNoMoreInteractions(this.webSocketHandler); }
@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 handleFrameMessageWhenNotOpen() throws Exception { this.session.handleFrame(SockJsFrame.openFrame().getContent()); this.session.close(); reset(this.handler); this.session.handleFrame(SockJsFrame.messageFrame(CODEC, "foo", "bar").getContent()); verifyNoMoreInteractions(this.handler); }
@Test public void connectWebSocket() throws Exception { setupInfoRequest(true); this.sockJsClient.doHandshake(handler, URL).addCallback(this.connectCallback); assertTrue(this.webSocketTransport.invoked()); WebSocketSession session = mock(WebSocketSession.class); this.webSocketTransport.getConnectCallback().onSuccess(session); verify(this.connectCallback).onSuccess(session); verifyNoMoreInteractions(this.connectCallback); }
@Test public void responseClosedAfterDisconnected() throws Exception { String body = "o\n" + "c[3000,\"Go away!\"]\n" + "a[\"foo\"]\n"; ClientHttpResponse response = response(HttpStatus.OK, body); connect(response); verify(this.webSocketHandler).afterConnectionEstablished(any()); verify(this.webSocketHandler).afterConnectionClosed(any(), any()); verifyNoMoreInteractions(this.webSocketHandler); verify(response).close(); }
@Test public void applyBeforeHandshakeWithFalseReturnValue() throws Exception { given(i1.beforeHandshake(request, response, wsHandler, attributes)).willReturn(true); given(i2.beforeHandshake(request, response, wsHandler, attributes)).willReturn(false); HandshakeInterceptorChain chain = new HandshakeInterceptorChain(interceptors, wsHandler); chain.applyBeforeHandshake(request, response, attributes); verify(i1).beforeHandshake(request, response, wsHandler, attributes); verify(i1).afterHandshake(request, response, wsHandler, null); verify(i2).beforeHandshake(request, response, wsHandler, attributes); verifyNoMoreInteractions(i1, i2, i3); }
@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 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 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); }
@Test public void success() throws Exception { given(i1.beforeHandshake(request, response, wsHandler, attributes)).willReturn(true); given(i2.beforeHandshake(request, response, wsHandler, attributes)).willReturn(true); given(i3.beforeHandshake(request, response, wsHandler, attributes)).willReturn(true); HandshakeInterceptorChain chain = new HandshakeInterceptorChain(interceptors, wsHandler); chain.applyBeforeHandshake(request, response, attributes); verify(i1).beforeHandshake(request, response, wsHandler, attributes); verify(i2).beforeHandshake(request, response, wsHandler, attributes); verify(i3).beforeHandshake(request, response, wsHandler, attributes); verifyNoMoreInteractions(i1, i2, i3); }