WebSocket
Decoy accepts WebSocket upgrades on its normal port, next to plain HTTP, gRPC and the Control API.
Point the client's socket URL at Decoy. With TLS on, the same port serves wss://.
Supported so far: text frames carrying JSON or plain text, on: connect Cases, timed replies,
close, Control API send, and binary formats through WebSocket Pipelines.
WebSocket Routes
One Route per socket. match takes the upgrade path and, optionally, query params the upgrade
must carry, so several sockets can share one path:
- Picking the Route: the path matches as for HTTP (
{param}captures a segment) and every listedqueryparam must be present with that value ('*'means any value). If several Routes match, the one listing morequerykeys wins. Two Routes that could tie fail at startup. An upgrade nowsRoute matches is handled as an HTTP GET: an HTTP Route on that path answers it, otherwise the HTTP miss (501). So does a repeated query key (?service=a&service=b) that would match two Routes equally. on: connect: at most one Case per Route. It takes nomatchand runs when the socket opens. Its templates see the upgrade request (${ query.service }). Without one, the server says nothing until a message arrives or the test callssend.- Message Cases match each inbound text frame.
matchtakesmessage(a deep partial match, like HTTPbody) pluspathParams,query,headersandcookiesof the upgrade, or one${ }predicate. A frame that parses as JSON is JSON, anything else stays a string:match: '${ message == `"ping"` }'. - Unmatched message: Decoy sends nothing, logs a miss, and keeps the socket open. Without a
pipeline, a binary frame is always a miss.
Collections and Sessions work as for HTTP: on: connect Cases are entries like any other.
Behaviors
Use message or messages, not both. Every string is templated against the message envelope:
{ path, pathParams, query, headers, cookies, message }.
The Behavior-level delay works with either message or messages. An item-level delay
adds a wait before that item, measured from the previous send. On the first item, the two
delays add up. Both fields accept a nonnegative integer in milliseconds or a duration string with ms or s.
Decoy sends subscribed after 1.25 seconds and update 0.5 seconds later.
Each reply runs on its own timer. If a message arrives while an earlier reply still has delayed items, both go out as their timers fire, interleaved. When the socket closes, pending items are dropped.
Sessions
A socket keeps the Session it opened with (x-mock-session on the upgrade). useCollection and
useRoute apply to its next message. An on: connect change applies to the Session's next socket.
Browsers can't set headers on new WebSocket(); under Playwright, the fixture's bridge adds it (see
Playwright WebSockets).
Pushing from a test
send pushes a message to every open socket of the Route in the
caller's Session, skipping Cases:
The ws-node example runs all of the above against a real client.
WebSocket Pipelines
Configure a Transformer Pipeline for setup shared by HTTP, gRPC, and WebSocket. On a WebSocket Route, input receives the frame payload before Case matching; an input command at the end of the Pipeline must emit JSON. output runs after the Behavior renders or when the Control API pushes a message. The final string becomes a text frame; the final bytes become a binary frame. Workers can use per-connection state.
The Control API send counts messages queued for delivery. The output Pipeline runs once per recipient connection. If it fails, Decoy logs the Transformer and skips that frame.
During a transform, each socket can buffer up to 64 subsequent data frames totaling at most 16 MiB. Across all sockets, the process budgets 64 MiB for read-ahead, charged in whole KiB with a minimum of 1 KiB per frame. Exceeding a read-ahead limit closes the socket requesting more space.
Open sockets keep the Pipeline selected at upgrade. Protobuf host calls use the currently loaded descriptors, including after a reload. WebSocket request events capture the rendered Behavior reply before output transforms and delays. A captured reply can therefore differ from the frames that reach the client.