* Asserts the upgrade is rejected (the socket errors/closes without ever * opening, so no `server_hello` is received). Resolves on the first `error` or * `close`; rejects if the socket opens or nothing happens within the timeout.
(url: string, opts?: ConnectOptions)
| 182 | * `close`; rejects if the socket opens or nothing happens within the timeout. |
| 183 | */ |
| 184 | function expectRejected(url: string, opts?: ConnectOptions): Promise<void> { |
| 185 | return new Promise((resolve, reject) => { |
| 186 | const ws = new WebSocket(url, opts?.protocols, { headers: opts?.headers }); |
| 187 | const done = (err?: Error): void => { |
| 188 | clearTimeout(t); |
| 189 | ws.removeAllListeners(); |
| 190 | try { |
| 191 | ws.terminate(); |
| 192 | } catch { |
| 193 | // ignore |
| 194 | } |
| 195 | if (err !== undefined) reject(err); |
| 196 | else resolve(); |
| 197 | }; |
| 198 | const t = setTimeout( |
| 199 | () => done(new Error('connection was not rejected within timeout')), |
| 200 | 1500, |
| 201 | ); |
| 202 | ws.once('open', () => done(new Error('connection unexpectedly opened'))); |
| 203 | ws.once('error', () => done()); |
| 204 | ws.once('close', () => done()); |
| 205 | }); |
| 206 | } |
| 207 | |
| 208 | describe('ws upgrade auth', () => { |
| 209 | it('accepts a valid bearer subprotocol and echoes it', async () => { |
no test coverage detected