-
-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ws): add "socket" property on
server
object (#647)
- Loading branch information
1 parent
39b4198
commit ad0cdda
Showing
5 changed files
with
93 additions
and
39 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
62 changes: 62 additions & 0 deletions
62
test/modules/WebSocket/compliance/websocket.server.socket.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,62 @@ | ||
// @vitest-environment node-with-websocket | ||
import { it, expect, beforeAll, afterEach, afterAll } from 'vitest' | ||
import { WebSocketInterceptor } from '../../../../src/interceptors/WebSocket' | ||
import { waitForWebSocketEvent } from '../utils/waitForWebSocketEvent' | ||
import { DeferredPromise } from '@open-draft/deferred-promise' | ||
|
||
const interceptor = new WebSocketInterceptor() | ||
|
||
beforeAll(() => { | ||
interceptor.apply() | ||
}) | ||
|
||
afterEach(() => { | ||
interceptor.removeAllListeners() | ||
}) | ||
|
||
afterAll(() => { | ||
interceptor.dispose() | ||
}) | ||
|
||
it('throws an error when accessing "server.socket" before calling "server.connect()"', async () => { | ||
const socketPromise = new DeferredPromise<WebSocket>() | ||
interceptor.on('connection', ({ server }) => { | ||
try { | ||
// Accessing "server.socket" before calling "server.connect()" is a no-op. | ||
const socket = server.socket | ||
socketPromise.resolve(socket) | ||
} catch (error) { | ||
socketPromise.reject(error) | ||
} | ||
}) | ||
|
||
const clientSocket = new WebSocket('wss://localhost') | ||
await waitForWebSocketEvent('open', clientSocket) | ||
|
||
await expect(socketPromise).rejects.toThrow( | ||
'Cannot access "socket" on the original WebSocket server object: the connection is not open. Did you forget to call `server.connect()`?' | ||
) | ||
|
||
// Client connection must remain open. | ||
expect(clientSocket.readyState).toBe(WebSocket.OPEN) | ||
}) | ||
|
||
it('returns the WebSocket instance after calling "server.connect()"', async () => { | ||
const socketPromise = new DeferredPromise<WebSocket>() | ||
interceptor.on('connection', ({ server }) => { | ||
server.connect() | ||
try { | ||
const socket = server.socket | ||
socketPromise.resolve(socket) | ||
} catch (error) { | ||
socketPromise.reject(error) | ||
} | ||
}) | ||
|
||
await waitForWebSocketEvent('open', new WebSocket('wss://localhost')) | ||
|
||
const serverSocket = await socketPromise | ||
expect(serverSocket).toBeInstanceOf(WebSocket) | ||
expect(serverSocket.url).toBe('wss://localhost/') | ||
expect(serverSocket.readyState).toBe(WebSocket.CONNECTING) | ||
}) |
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