Add WebSocket traffic support to HAR generator and per-message mocking#3
Closed
waldekmastykarz wants to merge 2 commits into
Closed
Add WebSocket traffic support to HAR generator and per-message mocking#3waldekmastykarz wants to merge 2 commits into
waldekmastykarz wants to merge 2 commits into
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds two major WebSocket capabilities to Dev Proxy:
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"+_webSocketMessagesarray on the upgrade entry).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)
WebSocketMessageRecordabstraction (direction, type, data, timestamp)WebSocketMessagesproperty toIProxySessionWebSocketRelaywithWebSocket.CreateFromStream-based frame-level relay that captures every messagePrefixedStreamto handle leftover handshake bytes_resourceTypeand_webSocketMessagesHAR extension fields (matching Chrome/mitmproxy convention)HarGeneratorPluginfor upgrade entriesPer-message interception
InterceptWebSocketMessages(interceptor, onConnected)toIProxySessionRelayClientToOriginAsyncandRelayOriginToClientAsyncwithSemaphoreSlimfor thread-safe client sendsInterceptorClientConnectionwrapper with send serialization and message captureWebSocketMockResponsePluginfromHandleWebSockettoInterceptWebSocketMessagesArchitecture
Testing
WebSocketRelayTestsrewritten for frame-level relay with message capture verificationWebSocketMockIntegrationTestspass (mock-only fallback covers the no-origin scenario)