private ListenableFuture<WebSocketSession> connect(ClientHttpResponse... responses) throws Exception { return connect(new TestRestTemplate(responses)); }
@Test public void connectReceiveAndCloseWithPrelude() throws Exception { StringBuilder sb = new StringBuilder(2048); for (int i = 0; i < 2048; i++) { sb.append('h'); } String body = sb.toString() + "\n" + "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 connectFailure() throws Exception { final HttpServerErrorException expected = new HttpServerErrorException(HttpStatus.INTERNAL_SERVER_ERROR); RestOperations restTemplate = mock(RestOperations.class); given(restTemplate.execute((URI) any(), eq(HttpMethod.POST), any(), any())).willThrow(expected); final CountDownLatch latch = new CountDownLatch(1); connect(restTemplate).addCallback( new ListenableFutureCallback<WebSocketSession>() { @Override public void onSuccess(WebSocketSession result) { } @Override public void onFailure(Throwable ex) { if (ex == expected) { latch.countDown(); } } } ); verifyNoMoreInteractions(this.webSocketHandler); }
@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 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 connectReceiveAndCloseWithStompFrame() throws Exception { StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.SEND); accessor.setDestination("/destination"); MessageHeaders headers = accessor.getMessageHeaders(); Message<byte[]> message = MessageBuilder.createMessage("body".getBytes(StandardCharsets.UTF_8), headers); byte[] bytes = new StompEncoder().encode(message); TextMessage textMessage = new TextMessage(bytes); SockJsFrame frame = SockJsFrame.messageFrame(new Jackson2SockJsMessageCodec(), textMessage.getPayload()); String body = "o\n" + frame.getContent() + "\n" + "c[3000,\"Go away!\"]"; ClientHttpResponse response = response(HttpStatus.OK, body); connect(response); verify(this.webSocketHandler).afterConnectionEstablished(any()); verify(this.webSocketHandler).handleMessage(any(), eq(textMessage)); verify(this.webSocketHandler).afterConnectionClosed(any(), eq(new CloseStatus(3000, "Go away!"))); verifyNoMoreInteractions(this.webSocketHandler); }