WebSocketMessageConverter implements MessageConverter<org.springframework.web.socket.WebSocketMessage, org.springframework.web.socket.WebSocketMessage, WebSocketEndpointConfiguration> { @Override public org.springframework.web.socket.WebSocketMessage convertOutbound(Message message, WebSocketEndpointConfiguration endpointConfiguration, TestContext context) { WebSocketMessage internalMessage; if (message instanceof WebSocketMessage) { internalMessage = (WebSocketMessage) message; } else { internalMessage = new WebSocketMessage(message); } org.springframework.web.socket.WebSocketMessage webSocketMessage; Object payload = internalMessage.getPayload(); if (payload instanceof String) { webSocketMessage = new TextMessage(payload.toString(), internalMessage.isLast()); } else if (payload instanceof ByteBuffer) { webSocketMessage = new BinaryMessage((ByteBuffer) payload, internalMessage.isLast()); } else if (payload instanceof byte[]) { webSocketMessage = new BinaryMessage((byte[]) payload, internalMessage.isLast()); } else { try { webSocketMessage = new TextMessage(internalMessage.getPayload(String.class), internalMessage.isLast()); } catch (ConversionNotSupportedException e) { throw new CitrusRuntimeException(String.format("Found unsupported payload type: '%s'", payload.getClass().getCanonicalName()), e); } } convertOutbound(webSocketMessage, internalMessage, endpointConfiguration, context); return webSocketMessage; }  @Override org.springframework.web.socket.WebSocketMessage convertOutbound(Message message, WebSocketEndpointConfiguration endpointConfiguration, TestContext context); @Override void convertOutbound(org.springframework.web.socket.WebSocketMessage externalMessage, Message internalMessage, WebSocketEndpointConfiguration endpointConfiguration, TestContext context); @Override Message convertInbound(org.springframework.web.socket.WebSocketMessage externalMessage, WebSocketEndpointConfiguration endpointConfiguration, TestContext context);  }
@Test public void testConvertTextMessageOutbound() throws Exception { WebSocketEndpointConfiguration endpointConfiguration = new WebSocketClientEndpointConfiguration(); WebSocketMessage message = new WebSocketMessage("Hello WebSocket!"); org.springframework.web.socket.WebSocketMessage result = messageConverter.convertOutbound(message, endpointConfiguration, context); Assert.assertTrue(TextMessage.class.isInstance(result)); Assert.assertEquals(((TextMessage) result).getPayload(), "Hello WebSocket!"); Assert.assertEquals(result.isLast(), true); message = new WebSocketMessage("Hello WebSocket - stay tuned!").last(false); result = messageConverter.convertOutbound(message, endpointConfiguration, context); Assert.assertTrue(TextMessage.class.isInstance(result)); Assert.assertEquals(((TextMessage) result).getPayload(), "Hello WebSocket - stay tuned!"); Assert.assertEquals(result.isLast(), false); }
@Test public void testConvertBinaryMessageOutbound() throws Exception { WebSocketEndpointConfiguration endpointConfiguration = new WebSocketClientEndpointConfiguration(); WebSocketMessage message = new WebSocketMessage("Hello WebSocket!".getBytes(Charset.forName("UTF-8"))); org.springframework.web.socket.WebSocketMessage result = messageConverter.convertOutbound(message, endpointConfiguration, context); Assert.assertTrue(BinaryMessage.class.isInstance(result)); Assert.assertEquals(((BinaryMessage) result).getPayload().array(), "Hello WebSocket!".getBytes(Charset.forName("UTF-8"))); Assert.assertEquals(result.isLast(), true); }