Skip to content

fix: honor WithHTTPClient for WebSocket dial via coder/websocket DialOptions#248

Merged
philippseith merged 1 commit into
philippseith:masterfrom
sruehl:fix/246-websocket-httpclient
Jun 21, 2026
Merged

fix: honor WithHTTPClient for WebSocket dial via coder/websocket DialOptions#248
philippseith merged 1 commit into
philippseith:masterfrom
sruehl:fix/246-websocket-httpclient

Conversation

@sruehl

@sruehl sruehl commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

Fixes #246.

What changed

  • httpconnection.go: when the caller's Doer is an *http.Client, populate DialOptions.HTTPClient before calling websocket.Dial so that coder/websocket uses the caller's transport for the WebSocket upgrade handshake.
  • Updated WithHTTPClient godoc to reflect the new behaviour and document the *http.Client requirement for WebSocket propagation.
  • httpserver_test.go: added a test that starts a httptest.NewTLSServer, connects a client with WithHTTPClient(testServer.Client()) and WithTransports(TransportWebSockets), and asserts a successful Invoke round-trip.

Background

Issue #136 removed the custom client from the WebSocket path (for nhooyr.io/websocket) because custom transports that do not return writable response bodies broke the dial. The fix was correct for that library. Since the migration to github.com/coder/websocket, DialOptions.HTTPClient is explicitly supported and its docs note the writable-body constraint (http.Transport satisfies this since Go 1.12).

The type assertion (httpConn.client.(*http.Client)) is required because httpConn.client is typed as Doer while DialOptions.HTTPClient is *http.Client. Callers passing a non-*http.Client Doer continue to fall back to http.DefaultClient for the WebSocket dial, as before.

…Options

When the supplied Doer is an *http.Client, populate DialOptions.HTTPClient
so that coder/websocket uses the caller's transport (and therefore TLS
configuration) for the WebSocket upgrade handshake instead of falling
back to http.DefaultClient.

Previously, custom TLS settings (e.g. InsecureSkipVerify or a non-default
root CA pool) were silently dropped for WebSocket connections, causing
x509 certificate errors against servers with non-system-trusted certs.

The type assertion is required because httpConn.client is typed as Doer
while DialOptions.HTTPClient requires *http.Client. Callers passing a
non-*http.Client Doer continue to get the existing behaviour.

Closes philippseith#246
@codecov

codecov Bot commented Jun 8, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 78.91%. Comparing base (df663fa) to head (d168939).

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #248      +/-   ##
==========================================
+ Coverage   78.56%   78.91%   +0.34%     
==========================================
  Files          30       30              
  Lines        2431     2433       +2     
==========================================
+ Hits         1910     1920      +10     
+ Misses        386      380       -6     
+ Partials      135      133       -2     
Files with missing lines Coverage Δ
httpconnection.go 52.17% <100.00%> (+7.04%) ⬆️

... and 2 files with indirect coverage changes


Continue to review full report in Codecov by Harness.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update df663fa...d168939. Read the comment docs.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@sruehl

sruehl commented Jun 17, 2026

Copy link
Copy Markdown
Contributor Author

@philippseith ping

@philippseith philippseith merged commit 641cf39 into philippseith:master Jun 21, 2026
7 checks passed
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.

WebSocket dial does not honor WithHTTPClient after migration to coder/websocket

2 participants