Skip to content

Commit

Permalink
feat(WebSocket): add connection "info" to the "connection" event payl…
Browse files Browse the repository at this point in the history
…oad (#577)

Co-authored-by: Artem Zakharchenko <kettanaito@gmail.com>
  • Loading branch information
DanielleHuisman and kettanaito authored Jun 6, 2024
1 parent efac32f commit 8c633ad
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -322,10 +322,11 @@ intereceptor.on('connection', ({ client }) => {

The `connection` event exposes the following arguments:

| Name | Type | Description |
| -------- | --------------------------------------------------------- | ---------------------------------------------------------------- |
| `client` | [`WebSocketClientConnection`](#websocketclientconnection) | An object representing a connected WebSocket client instance. |
| `server` | [`WebSocketServerConnection`](#websocketserverconnection) | An object representing the original WebSocket server connection. |
| Name | Type | Description |
| -------- | --------------------------------------------------------- | ----------------------------------------------------------------------------------- |
| `client` | [`WebSocketClientConnection`](#websocketclientconnection) | An object representing a connected WebSocket client instance. |
| `server` | [`WebSocketServerConnection`](#websocketserverconnection) | An object representing the original WebSocket server connection. |
| `info` | `object` | Additional WebSocket connection information (like the original client `protocols`). |

### `WebSocketClientConnection`

Expand Down
13 changes: 13 additions & 0 deletions src/interceptors/WebSocket/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ export type WebSocketConnectionData = {
* The original WebSocket server connection.
*/
server: WebSocketServerConnection

/**
* The connection information.
*/
info: {
/**
* The protocols supported by the WebSocket client.
*/
protocols: string | Array<string> | undefined
}
}

/**
Expand Down Expand Up @@ -82,6 +92,9 @@ export class WebSocketInterceptor extends Interceptor<WebSocketEventMap> {
transport,
createConnection
),
info: {
protocols,
},
})
})

Expand Down
22 changes: 22 additions & 0 deletions test/modules/WebSocket/compliance/websocket.connection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ it('emits the correct "connection" event on the interceptor', async () => {
addEventListener: expect.any(Function),
removeEventListener: expect.any(Function),
}),
info: {
protocols: undefined,
},
})
)
})
Expand All @@ -74,3 +77,22 @@ it('does not connect to the actual WebSocket server by default', async () => {
expect(connectionListener).toHaveBeenCalledTimes(1)
expect(realConnectionListener).not.toHaveBeenCalled()
})

it('includes connection information in the "connection" event payload', async () => {
const connectionListener = vi.fn()
interceptor.once('connection', connectionListener)

new WebSocket('wss://example.com', ['protocol1', 'protocol2'])
await waitForNextTick()

expect(connectionListener).toHaveBeenCalledTimes(1)
expect(connectionListener).toHaveBeenNthCalledWith(
1,
expect.objectContaining({
info: {
// Preserves the client protocols as-is.
protocols: ['protocol1', 'protocol2'],
},
})
)
})

0 comments on commit 8c633ad

Please sign in to comment.