Skip to content

Add WebSocket traffic support to HAR generator and per-message mocking#3

Closed
waldekmastykarz wants to merge 2 commits into
nextfrom
waldekmastykarz-websocket-har-support
Closed

Add WebSocket traffic support to HAR generator and per-message mocking#3
waldekmastykarz wants to merge 2 commits into
nextfrom
waldekmastykarz-websocket-har-support

Conversation

@waldekmastykarz

Copy link
Copy Markdown
Owner

Summary

Adds two major WebSocket capabilities to Dev Proxy:

  1. HAR generator captures WebSocket messages — WebSocket traffic relayed through the proxy is now recorded in HAR files following the Chrome DevTools / mitmproxy convention (_resourceType: "websocket" + _webSocketMessages array on the upgrade entry).

  2. Per-message WebSocket interception — Converts the WebSocket mock plugin from full-connection takeover to per-message interception with origin passthrough, matching how HTTP mocking works: matched messages get mock responses, unmatched ones pass through to the origin.

Changes

WebSocket message capture (HAR)

  • Add WebSocketMessageRecord abstraction (direction, type, data, timestamp)
  • Add WebSocketMessages property to IProxySession
  • Replace raw byte relay in WebSocketRelay with WebSocket.CreateFromStream-based frame-level relay that captures every message
  • Add PrefixedStream to handle leftover handshake bytes
  • Add _resourceType and _webSocketMessages HAR extension fields (matching Chrome/mitmproxy convention)
  • Populate WebSocket messages in HarGeneratorPlugin for upgrade entries

Per-message interception

  • Add InterceptWebSocketMessages(interceptor, onConnected) to IProxySession
  • Split relay into RelayClientToOriginAsync and RelayOriginToClientAsync with SemaphoreSlim for thread-safe client sends
  • Add InterceptorClientConnection wrapper with send serialization and message capture
  • Convert WebSocketMockResponsePlugin from HandleWebSocket to InterceptWebSocketMessages
  • Add origin-unreachable fallback: when the origin can't be reached but an interceptor is registered, the proxy falls back to mock-only mode (same UX as before, just automatic)

Architecture

With origin reachable:
  Client ──msg──▶ Proxy ──interceptor──▶ Origin
                    │ matched? mock response ◀──┘
                    │ unmatched? forward to origin

With origin unreachable (fallback):
  Client ──msg──▶ Proxy (mock server)
                    │ interceptor handles matched messages
                    │ unmatched messages are dropped

Testing

  • All 306 tests pass (0 failures)
  • WebSocketRelayTests rewritten for frame-level relay with message capture verification
  • Both WebSocketMockIntegrationTests pass (mock-only fallback covers the no-origin scenario)

waldekmastykarz and others added 2 commits July 2, 2026 11:23
Replace raw byte WebSocket relay with frame-aware relay using
WebSocket.CreateFromStream on both client and origin sides. Each
relayed message is captured as a WebSocketMessageRecord and stored
on the session.

The HAR generator now detects WebSocket upgrade entries and embeds
captured messages using the _resourceType and _webSocketMessages
custom HAR fields, following the Chrome DevTools / mitmproxy
convention.

Changes:
- Add WebSocketMessageRecord abstraction (direction, type, data,
  timestamp)
- Add IProxySession.WebSocketMessages property
- Convert WebSocketRelay from raw byte splice to message-level
  relay with PrefixedStream for leftover handshake bytes
- Add HarWebSocketMessage model and _resourceType/_webSocketMessages
  fields to HarEntry
- Update HarGeneratorPlugin to populate WebSocket fields
- Update WebSocketRelayTests for frame-level relay and message
  capture verification

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Convert WebSocket mocking from full-connection takeover to per-message
interception: the proxy connects to the origin and relays traffic, but
each client message is offered to the interceptor first. Matched
messages get mock responses; unmatched ones pass through to the origin.

When the origin is unreachable but an interceptor is registered, the
proxy falls back to mock-only mode — same as the old HandleWebSocket
behavior.

- Add InterceptWebSocketMessages to IProxySession with interceptor and
  onConnected callbacks
- Split WebSocket relay into client-to-origin and origin-to-client tasks
  with thread-safe client sends via SemaphoreSlim
- Add InterceptorClientConnection wrapper with send serialization and
  message capture for HAR
- Convert WebSocketMockResponsePlugin from HandleWebSocket to
  InterceptWebSocketMessages
- Add origin-unreachable fallback in ProxyConnectionHandler

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant