use crate::ws_mock_server::WsMockServer;
use futures_util::stream::SplitStream;
use futures_util::{SinkExt, StreamExt};
use std::time::Duration;
use tokio::net::TcpStream;
use tokio_tungstenite::tungstenite::Message;
use tokio_tungstenite::{connect_async, MaybeTlsStream, WebSocketStream};
pub async fn send_to_server(
server: &WsMockServer,
message: String,
) -> SplitStream<WebSocketStream<MaybeTlsStream<TcpStream>>> {
let (stream, _resp) = connect_async(server.uri().await)
.await
.expect("Connecting failed");
let (mut send, recv) = stream.split();
send.send(Message::from(message)).await.unwrap();
recv
}
pub async fn collect_all_messages(
mut ws_recv: SplitStream<WebSocketStream<MaybeTlsStream<TcpStream>>>,
timeout: Duration,
) -> Vec<Message> {
let mut received = Vec::new();
while let Ok(Some(Ok(message))) = tokio::time::timeout(timeout, ws_recv.next()).await {
received.push(message);
}
received
}