ws_mock::utils

Function collect_all_messages

source
pub async fn collect_all_messages(
    ws_recv: SplitStream<WebSocketStream<MaybeTlsStream<TcpStream>>>,
    timeout: Duration,
) -> Vec<Message>
Expand description

Given a SplitStream receiver, receive messages until timing out and return all messages in a Vec<String>.

Useful for receiving a batch of messages and later making assertions about them.

Examples found in repository?
examples/forwarding_messages.rs (line 35)
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
pub async fn main() {
    let server = WsMockServer::start().await;

    let (mpsc_send, mpsc_recv) = mpsc::channel::<Message>(32);

    WsMock::new()
        .forward_from_channel(mpsc_recv)
        .mount(&server)
        .await;

    let (stream, _resp) = connect_async(server.uri().await)
        .await
        .expect("Connecting failed");

    let (_send, ws_recv) = stream.split();

    mpsc_send
        .send(Message::Text("message-1".to_string()))
        .await
        .unwrap();
    mpsc_send
        .send(Message::Text("message-2".to_string()))
        .await
        .unwrap();

    let received = collect_all_messages(ws_recv, Duration::from_millis(250)).await;

    server.verify().await;
    assert_eq!(
        vec![
            Message::Text("message-1".to_string()),
            Message::Text("message-2".to_string())
        ],
        received
    );
}