diff --git a/src/node/server.test.js b/src/node/server.test.js index f8dd2d5..09aabe9 100644 --- a/src/node/server.test.js +++ b/src/node/server.test.js @@ -213,16 +213,30 @@ describe('streamwall server', () => { }) it('rejects websocket upgrade if origin is invalid', async () => { - // Make a raw WebSocket connection with a bogus origin - const badWs = new WebSocket(`ws://localhost:${port}/ws`, [], { - origin: 'http://evilsite.com', // not the expectedOrigin - }) - const closePromise = once(badWs, 'close') - const [code] = await closePromise - expect(code).toBe(401) - // or just expect the server forcibly closed the connection - }) - + // Create a promise that will reject if the connection succeeds + const connectPromise = new Promise((resolve, reject) => { + const badWs = new WebSocket(`ws://localhost:${port}/ws`, [], { + origin: 'http://evilsite.com', + }); + + badWs.on('open', () => { + badWs.close(); + reject(new Error('WebSocket connection should not succeed')); + }); + + badWs.on('error', (err) => { + // We expect this to fail with a 401 + if (err.message.includes('401')) { + resolve(); + } else { + reject(err); + } + }); + }); + + await connectPromise; + }); + describe('admin role', () => { it('can view tokens', async () => { await auth.createToken({