-
-
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 018cafc
Showing
6 changed files
with
164 additions
and
30 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
104 changes: 104 additions & 0 deletions
104
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,104 @@ | ||
// @vitest-environment node-with-websocket | ||
import { beforeAll, afterEach, afterAll, vi, it, expect } from 'vitest' | ||
import { WebSocketInterceptor } from '../../../../src/interceptors/WebSocket' | ||
|
||
const interceptor = new WebSocketInterceptor() | ||
|
||
beforeAll(() => { | ||
interceptor.apply() | ||
}) | ||
|
||
afterEach(() => { | ||
interceptor.removeAllListeners() | ||
}) | ||
|
||
afterAll(() => { | ||
interceptor.dispose() | ||
}) | ||
|
||
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('ws://localhost') | ||
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('ws://localhost') | ||
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('ws://localhost') | ||
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