From b098586bee9658bd04d20e979f0e4fc7ba9395bf Mon Sep 17 00:00:00 2001 From: Steven Luscher Date: Sun, 10 Sep 2023 23:08:46 -0700 Subject: [PATCH] Calling `close` in the `CONNECTING` state should not cause `onopen` to be called (#383) * test: assert that calling `close` in the `CONNECTING` state does not cause `onopen` to be called * fix: bail from the `WebSocket` constructor's delayed setup if the `readyState` has changed from `CONNECTING` to something else --- src/websocket.js | 3 +++ tests/functional/close-algorithm.test.js | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/src/websocket.js b/src/websocket.js index 5a9252aa..ecb3d2f3 100644 --- a/src/websocket.js +++ b/src/websocket.js @@ -51,6 +51,9 @@ class WebSocket extends EventTarget { * registered :-) */ delay(function delayCallback() { + if (this.readyState !== WebSocket.CONNECTING) { + return; + } if (server) { if ( server.options.verifyClient && diff --git a/tests/functional/close-algorithm.test.js b/tests/functional/close-algorithm.test.js index 2f840b46..62221849 100644 --- a/tests/functional/close-algorithm.test.js +++ b/tests/functional/close-algorithm.test.js @@ -88,6 +88,10 @@ test.cb('that if the readyState is CONNECTING we fail the connection and close', mockSocket.readyState = WebSocket.CONNECTING; + mockSocket.onopen = () => { + t.fail('open should not have been called'); + }; + mockSocket.onerror = () => { t.is(mockSocket.readyState, WebSocket.CLOSED); };