-
-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(WebSocket): buffer sends until connection is open
- Loading branch information
1 parent
e32af72
commit dc0d25f
Showing
6 changed files
with
174 additions
and
29 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
118 changes: 118 additions & 0 deletions
118
test/modules/WebSocket/compliance/websocket.client.send.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,118 @@ | ||
// @vitest-environment node-with-websocket | ||
import { setTimeout } from 'node:timers/promises' | ||
import { beforeAll, afterEach, afterAll, vi, it, expect } from 'vitest' | ||
import { WebSocketServer } from 'ws' | ||
import { WebSocketInterceptor } from '../../../../src/interceptors/WebSocket' | ||
import { getWsUrl } from '../utils/getWsUrl' | ||
|
||
const interceptor = new WebSocketInterceptor() | ||
|
||
const wsServer = new WebSocketServer({ | ||
host: '127.0.0.1', | ||
port: 0, | ||
async verifyClient() { | ||
await setTimeout(500) | ||
return true | ||
}, | ||
}) | ||
|
||
beforeAll(() => { | ||
interceptor.apply() | ||
}) | ||
|
||
afterEach(() => { | ||
interceptor.removeAllListeners() | ||
wsServer.clients.forEach((client) => client.close()) | ||
}) | ||
|
||
afterAll(() => { | ||
interceptor.dispose() | ||
wsServer.close() | ||
}) | ||
|
||
it('buffers client sends until the connection is open', async () => { | ||
const events: Array<string> = [] | ||
|
||
interceptor.on('connection', ({ client }) => { | ||
client.socket.addEventListener('open', () => { | ||
events.push('open') | ||
}) | ||
client.socket.addEventListener('message', () => { | ||
events.push('send') | ||
}) | ||
|
||
client.send('hello world') | ||
}) | ||
|
||
const socket = new WebSocket(getWsUrl(wsServer)) | ||
const messageListener = vi.fn() | ||
socket.addEventListener('message', messageListener) | ||
|
||
await vi.waitFor(() => { | ||
expect(messageListener).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
data: 'hello world', | ||
}) | ||
) | ||
}) | ||
|
||
expect(events).toEqual(['open', 'send']) | ||
}) | ||
|
||
it('does not send data if the client connection is closing', async () => { | ||
const events: Array<string> = [] | ||
|
||
interceptor.on('connection', ({ client }) => { | ||
client.socket.addEventListener('close', () => { | ||
events.push('close') | ||
}) | ||
client.socket.addEventListener('message', () => { | ||
events.push('send') | ||
}) | ||
|
||
client.close() | ||
client.send('hello world') | ||
}) | ||
|
||
const socket = new WebSocket(getWsUrl(wsServer)) | ||
const messageListener = vi.fn() | ||
const closeListener = vi.fn() | ||
socket.addEventListener('message', messageListener) | ||
socket.addEventListener('close', closeListener) | ||
|
||
await vi.waitFor(() => { | ||
expect(closeListener).toHaveBeenCalledOnce() | ||
}) | ||
|
||
expect(messageListener).not.toHaveBeenCalled() | ||
expect(events).toEqual(['close']) | ||
}) | ||
|
||
it('does not send data if the client connection is closed', async () => { | ||
const events: Array<string> = [] | ||
|
||
interceptor.on('connection', ({ client }) => { | ||
client.socket.addEventListener('close', () => { | ||
events.push('close') | ||
}) | ||
client.socket.addEventListener('message', () => { | ||
events.push('send') | ||
}) | ||
|
||
client.close() | ||
queueMicrotask(() => client.send('hello world')) | ||
}) | ||
|
||
const socket = new WebSocket(getWsUrl(wsServer)) | ||
const messageListener = vi.fn() | ||
const closeListener = vi.fn() | ||
socket.addEventListener('message', messageListener) | ||
socket.addEventListener('close', closeListener) | ||
|
||
await vi.waitFor(() => { | ||
expect(closeListener).toHaveBeenCalledOnce() | ||
}) | ||
|
||
expect(messageListener).not.toHaveBeenCalled() | ||
expect(events).toEqual(['close']) | ||
}) |
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
4 changes: 1 addition & 3 deletions
4
test/modules/WebSocket/intercept/websocket.server.events.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