Skip to content

Add WebSocket Testing Support#25

Merged
sdogruyol merged 1 commit intomasterfrom
websocket
May 4, 2026
Merged

Add WebSocket Testing Support#25
sdogruyol merged 1 commit intomasterfrom
websocket

Conversation

@sdogruyol
Copy link
Copy Markdown
Member

@sdogruyol sdogruyol commented Apr 23, 2026

Summary

Implements #5

Adds two layers of WebSocket testing for Kemal apps: in-memory upgrade / handshake checks, and duplex tests that run real message exchange over a socket pair.

New helpers

  • websocket_request_headers — Valid WebSocket upgrade headers; optional sec_key and Origin.
  • get(path, …, websocket: true) — Merges upgrade headers and runs the request through SpecKemal.process_request. The ws block does not run (no upgrade_handler); use this for assertions on 101, Sec-WebSocket-Accept, or 400 / 426 on bad handshakes.
  • connect_websocket — Real upgrade via a connected socket pair (UNIXSocket.pair on Unix, loopback TCP on Windows); invokes response.upgrade_handler like production, and yields a client HTTP::WebSocket for send/receive in the block.
  • inner_connect_websocket — Advanced: access both the client WebSocket and the 101 HTTP::Client::Response inside the block.

Example

Assume your app prefixes server replies (so you can tell client send vs server send apart):

ws "/chat" do |socket, _env|
  socket.on_message { |msg| socket.send "server: #{msg}" }
end
  1. Handshake only — fast; the ws body never runs:
get "/chat", websocket: true
response.status_code.should eq 101
response.headers["Upgrade"].should eq "websocket"
  1. Real messages — runs the ws handler; assert the transformed reply, not the raw send:
connect_websocket "/chat" do |client|
  replies = Channel(String).new(1)
  client.on_message { |msg| replies.send msg }
  spawn { client.run }
  Fiber.yield

  client.send "hello"
  replies.receive.should eq "server: hello"
end

@sdogruyol sdogruyol merged commit 61f6fa4 into master May 4, 2026
8 checks passed
@sdogruyol sdogruyol mentioned this pull request May 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant