WebSocket API Testing
Testing real-time APIs requires a totally different paradigm than REST. Standard HTTP requests are stateless ("fire and forget"), whereas WebSockets are stateful, requiring you to assert against bidirectional streams of data occurring over time.
Rumour handles this elegantly by executing sequential action scripts against a live connection.
The Scenario
We will construct a flow that connects to a secure WebSocket server, authenticates via headers, sends a JSON payload subscribing to a channel, waits for the server to broadcast a specific response, and finally terminates the connection cleanly.
1. Actionable Setup
Let's build this from scratch. First, we need a live environment to test against. We will use the official rumour-mock-server repository.
Start the Mock Server
In a new terminal window, boot up the multi-protocol mock server (which spins up a WebSocket echo server on port 4001):
cd rumour-mock-server/mock-server
npm install
npm start
Scaffold the Workspace
In your main terminal, initialize a fresh testing workspace and scaffold a new collection:
mkdir ws_tutorial
cd ws_tutorial
rumour init
rumour new realtime_flows
Open the newly generated workspace.env.toml and define the target endpoint:
[variables]
ws_url = "ws://localhost:4001"
Step 1: Scripting the Sequence
Create your request file: test_ws_lifecycle.toml.
By setting protocol = "websocket", Rumour knows to perform an HTTP/1.1 Upgrade Request instead of a standard GET/POST.
To dictate how Rumour behaves after the connection is upgraded, we use the [[websocket.messages]] array. This acts as a chronological script.
name = "ws_echo_sequence"
protocol = "websocket"
[request]
url = "{{ws_url}}"
# Standard HTTP headers are sent during the initial WebSocket Upgrade request
[headers]
Authorization = "Bearer {{ws_token}}"
[websocket]
# Global timeout for the entire connection lifecycle
timeout_ms = 5000
# Expected response substring to validate from the server
expect_message = "subscribed_to_updates"
# Send a payload to the server upon connection
[[websocket.messages]]
text = '{"event": "subscribe", "channel": "updates"}'
[assert]
# 101 Switching Protocols is the HTTP status code for a successful WebSocket upgrade
status = 101
Let's break down what just happened:
- The Handshake: Rumour sends an HTTP GET request to
ws://localhost:4001with theUpgrade: websocketheader and yourAuthorizationheader. It establishes the connection via101 Switching Protocols. - Sending Messages: Rumour transmits the messages queued in
[[websocket.messages]]down the TCP socket in chronological order. - Asserting Received Messages: Rumour reads incoming frames and verifies that the
expect_messagesubstring is received. If the pattern is not found beforetimeout_msexpires, Rumour marks the status as 417 Expectation Failed.
Advanced: Expect Message Failures
What happens if the server never sends the message, or sends the wrong data?
GET ws://localhost:4001
Header: Authorization: Bearer eyJhbG...
URL: ws://localhost:4001
✗ FAILED: test_ws_lifecycle.toml (5002ms) - WebSocket Error: Timeout waiting for message containing "subscribed_to_updates"
Rumour will sit gracefully until the timeout_ms limit is reached, then cleanly fail the node, providing you with exact diagnostic information about where the script halted.
For complex chat or trading applications, you can chain dozens of send and listen blocks together in a single TOML file to simulate a user's entire real-time session!