Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle HTTP handshakes for WebSocket connections #2436

Open
1 task
kettanaito opened this issue Feb 20, 2025 · 0 comments
Open
1 task

Handle HTTP handshakes for WebSocket connections #2436

kettanaito opened this issue Feb 20, 2025 · 0 comments
Labels

Comments

@kettanaito
Copy link
Member

Scope

Improves an existing behavior

Compatibility

  • This is a breaking change

Feature description

Summary

Some third-party WebSocket clients, like ws, may integrate a manual HTTP handshake for WebSocket connections. If such occur, Interceptors should handle them appropriately.

I am raising this in MSW because Interceptors may be too low-level of a library to handle this. Effectively, where we do this doesn't matter. Just has to be accounted for.

Proposal

The handshake handling itself isn't that complicated:

http.get(theSameUrlAsWsLink, ({ request }) => {
  if (request.headers.get('upgrade') === 'websocket') {
    return new HttpResponse(null, {
      status: 101,
      headers: {
        'Upgrade': 'websocket',
        'Connection': 'Upgrade',
        'Sec-WebSocket-Accept': 's3pPLMBiTxaQ9kYGzzhZRbK+xOo=',
      },
    })
  }
})

What's complicated is the ergonomics of a handler (ws link) that unfolds into multiple handlers (ws + http.get). MSW doesn't have a concept of a compound handler like that at the moment, and I would hate to ask people to spread ...ws.link() in their handlers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant