Skip to content

Commit 837f141

Browse files
committed
fix: add unhandled websocket connection handling
1 parent 209b65c commit 837f141

File tree

4 files changed

+22
-10
lines changed

4 files changed

+22
-10
lines changed

src/core/new/frames/websocket-frame.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export abstract class WebSocketNetworkFrame extends BaseNetworkFrame<
3030

3131
public async getUnhandledFrameMessage(): Promise<string> {
3232
const { connection } = this.data
33-
const details = `\n\n \u2022 ${connection.client.url}\n\n`
33+
const details = `\n\n \u2022 ${connection.client.url}\n\n`
3434

3535
return `intercepted a WebSocket connection without a matching event handler:${details}If you still wish to intercept this unhandled connection, please create an event handler for it.\nRead more: https://mswjs.io/docs/websocket`
3636
}

src/core/new/on-unhandled-frame.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,18 +77,24 @@ export function fromLegacyOnUnhandledRequest(
7777
getLegacyValue: () => UnhandledRequestStrategy | undefined,
7878
): UnhandledFrameCallback {
7979
return ({ frame, defaults }) => {
80-
if (frame.protocol !== 'http') {
81-
return
82-
}
83-
8480
const legacyOnUnhandledRequestStrategy = getLegacyValue()
8581

8682
if (legacyOnUnhandledRequestStrategy === undefined) {
8783
return
8884
}
8985

9086
if (typeof legacyOnUnhandledRequestStrategy === 'function') {
91-
return legacyOnUnhandledRequestStrategy(frame.data.request, {
87+
const request =
88+
frame.protocol === 'http'
89+
? frame.data.request
90+
: new Request(frame.data.connection.client.url, {
91+
headers: {
92+
connection: 'upgrade',
93+
upgrade: 'websocket',
94+
},
95+
})
96+
97+
return legacyOnUnhandledRequestStrategy(request, {
9298
warning: defaults.warn,
9399
error: defaults.error,
94100
})

src/node/setup-server.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ export class SetupServerApi implements SetupServer {
6868
* @fixme This expects a readonly emitter (subset of methods).
6969
*/
7070
this.events = this.#network.events as any
71+
72+
/**
73+
* @fixme Remove this method drilling in the future.
74+
* Drop the `SetupServerApi` class altogether and implement `setupServer`
75+
* as a simple function that can reference the network methods directly.
76+
*/
7177
this.use = this.#network.use.bind(this.#network)
7278
this.resetHandlers = this.#network.resetHandlers.bind(this.#network)
7379
this.restoreHandlers = this.#network.restoreHandlers.bind(this.#network)

test/node/ws-api/on-unhandled-request/warn.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ it(
3434

3535
expect(console.warn).toHaveBeenCalledWith(
3636
`\
37-
[MSW] Warning: intercepted a request without a matching request handler:
37+
[MSW] Warning: intercepted a WebSocket connection without a matching event handler:
3838
39-
GET wss://localhost:4321/
39+
• wss://localhost:4321/
4040
41-
If you still wish to intercept this unhandled request, please create a request handler for it.
42-
Read more: https://mswjs.io/docs/http/intercepting-requests`,
41+
If you still wish to intercept this unhandled connection, please create an event handler for it.
42+
Read more: https://mswjs.io/docs/websocket`,
4343
)
4444
}),
4545
)

0 commit comments

Comments
 (0)