-
-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into mikicho-patch-1
- Loading branch information
Showing
7 changed files
with
197 additions
and
23 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
83 changes: 83 additions & 0 deletions
83
test/modules/WebSocket/compliance/websocket.reuse-listeners.test.ts
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// @vitest-environment node-with-websocket | ||
import { vi, it, expect, beforeAll, afterEach, afterAll } from 'vitest' | ||
import { WebSocketServer } from 'ws' | ||
import { WebSocketInterceptor } from '../../../../src/interceptors/WebSocket' | ||
import { waitForWebSocketEvent } from '../utils/waitForWebSocketEvent' | ||
import { getWsUrl } from '../utils/getWsUrl' | ||
|
||
const wsServer = new WebSocketServer({ | ||
host: '127.0.0.1', | ||
port: 0, | ||
}) | ||
|
||
const interceptor = new WebSocketInterceptor() | ||
|
||
beforeAll(async () => { | ||
interceptor.apply() | ||
}) | ||
|
||
afterEach(() => { | ||
interceptor.removeAllListeners() | ||
wsServer.clients.forEach((client) => client.close()) | ||
}) | ||
|
||
afterAll(async () => { | ||
interceptor.dispose() | ||
wsServer.close() | ||
}) | ||
|
||
it('allows reusing the same function for multiple client listeners', async () => { | ||
const clientMessageListener = vi.fn() | ||
|
||
interceptor.on('connection', ({ client }) => { | ||
client.addEventListener('message', clientMessageListener) | ||
client.addEventListener('message', clientMessageListener) | ||
client.addEventListener('message', clientMessageListener) | ||
|
||
/** | ||
* @note Use `process.nextTick()` because `queueMicrotask()` has a | ||
* higher priority. We need the connection to open, handle messages, | ||
* and then close. | ||
*/ | ||
process.nextTick(() => { | ||
client.close() | ||
}) | ||
}) | ||
|
||
const socket = new WebSocket('wss://example.com') | ||
socket.onopen = () => socket.send('hello world') | ||
|
||
await waitForWebSocketEvent('close', socket) | ||
|
||
/** | ||
* @note The same event listner for the same event is deduped. | ||
* It will only be called once. That is correct. | ||
*/ | ||
expect(clientMessageListener).toHaveBeenCalledTimes(1) | ||
}) | ||
|
||
it('allows reusing the same function for multiple server listeners', async () => { | ||
wsServer.once('connection', (ws) => { | ||
ws.send('hello from server') | ||
queueMicrotask(() => ws.close()) | ||
}) | ||
|
||
const serverMessageListener = vi.fn() | ||
|
||
interceptor.on('connection', ({ server }) => { | ||
server.connect() | ||
server.addEventListener('message', serverMessageListener) | ||
server.addEventListener('message', serverMessageListener) | ||
server.addEventListener('message', serverMessageListener) | ||
}) | ||
|
||
const socket = new WebSocket(getWsUrl(wsServer)) | ||
|
||
await waitForWebSocketEvent('close', socket) | ||
|
||
/** | ||
* @note The same event listner for the same event is deduped. | ||
* It will only be called once. That is correct. | ||
*/ | ||
expect(serverMessageListener).toHaveBeenCalledTimes(1) | ||
}) |
80 changes: 80 additions & 0 deletions
80
test/modules/WebSocket/exchange/websocket.readystate.test.ts
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// @vitest-environment node-with-websocket | ||
import { vi, it, expect, beforeAll, afterEach, afterAll } from 'vitest' | ||
import { WebSocketInterceptor } from '../../../../src/interceptors/WebSocket' | ||
import { waitForWebSocketEvent } from '../utils/waitForWebSocketEvent' | ||
import { waitForNextTick } from '../utils/waitForNextTick' | ||
|
||
const interceptor = new WebSocketInterceptor() | ||
|
||
beforeAll(() => { | ||
interceptor.apply() | ||
}) | ||
|
||
afterEach(() => { | ||
interceptor.removeAllListeners() | ||
}) | ||
|
||
afterAll(() => { | ||
interceptor.dispose() | ||
}) | ||
|
||
it('dispatches the connection even when the client "readyState" is OPEN', async () => { | ||
const readyStateListener = vi.fn<[number]>() | ||
|
||
interceptor.on('connection', ({ client }) => { | ||
// CONNECTING. | ||
readyStateListener(client.socket.readyState) | ||
|
||
/** | ||
* @note Use `process.nextTick()` because it has lower priority | ||
* than `queueMicrotask()`. If you queue a microtask here, it will | ||
* run BEFORE a queued microtask that dispatches the open event. | ||
*/ | ||
process.nextTick(() => { | ||
readyStateListener(client.socket.readyState) | ||
client.close() | ||
}) | ||
}) | ||
|
||
const socket = new WebSocket('wss://localhost') | ||
await waitForWebSocketEvent('close', socket) | ||
|
||
// The client ready state must be OPEN when the connection listener is called. | ||
expect(readyStateListener).toHaveBeenNthCalledWith(1, WebSocket.CONNECTING) | ||
expect(readyStateListener).toHaveBeenNthCalledWith(2, WebSocket.OPEN) | ||
expect(readyStateListener).toHaveBeenCalledTimes(2) | ||
}) | ||
|
||
it('updates "readyState" correctly when closing the connection in the interceptor', async () => { | ||
const readyStateListener = vi.fn<[number]>() | ||
|
||
interceptor.on('connection', ({ client }) => { | ||
// CONNECTING. | ||
readyStateListener(client.socket.readyState) | ||
|
||
client.close() | ||
|
||
// CLOSING. | ||
readyStateListener(client.socket.readyState) | ||
|
||
process.nextTick(() => { | ||
// CLOSED. | ||
readyStateListener(client.socket.readyState) | ||
}) | ||
}) | ||
|
||
const openEventListener = vi.fn() | ||
const socket = new WebSocket('wss://localhost') | ||
socket.onopen = openEventListener | ||
await waitForWebSocketEvent('close', socket) | ||
|
||
expect(readyStateListener).toHaveBeenNthCalledWith(1, WebSocket.CONNECTING) | ||
// Must set ready state to CLOSING in the same frame as "client.close()". | ||
expect(readyStateListener).toHaveBeenNthCalledWith(2, WebSocket.CLOSING) | ||
|
||
await waitForNextTick() | ||
// Must set ready state to CLOSED in the next frame. | ||
expect(readyStateListener).toHaveBeenNthCalledWith(3, WebSocket.CLOSED) | ||
|
||
expect(openEventListener).not.toHaveBeenCalled() | ||
}) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
export async function waitForNextTick(): Promise<void> { | ||
return new Promise((resolve) => { | ||
queueMicrotask(() => resolve()) | ||
process.nextTick(() => resolve()) | ||
}) | ||
} |