Test websocket client

What is a good way to test websocket client?
One possible option is to run a simple test server during a test run, but what's the easiest way to achieve this?

What framework are you using? In actix-web you spin up a server during testing. The functionality is built into the framework. I don't know how other frameworks do this, but to me it seems a sensible approach to start a server and a client and let them communicate with each other in order to test that all works as you expect.

There is no framework. The actual server cannot be used for tests, so I need a test server. Even simple echo server will do.

Inside your test, you could start a thread that runs a server that accepts websocket connections (i.e. tungstenite). Then on your main thread you start your websocket client, connect it to the server and start sending messages.

Here pseudo-code of how I'd imagine something like this to look like:

use your_crate::WsClient;

use tungstenite::accept;

use std::net::TcpListener;
use std::thread;

fn test_websocket_client() {
    thread::spawn(|| { 
        let server = TcpListener::bind("").unwrap();
        let conn = server.incoming().next().unwrap();
        let mut websocket = accept(conn.unwrap()).unwrap();
        // echo 10 messages before shutting down
        for _ in 0..10 {
            let msg = websocket.read_message().unwrap();

            // We do not want to send back ping/pong messages.
            if msg.is_binary() || msg.is_text() {
    let client = WsClient::new("").unwrap();

    for _ in 0..10 {
        assert_eq!(client.receive(), "ping");

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.