Skip to content

Commit

Permalink
Calling close in the CONNECTING state should not cause onopen t…
Browse files Browse the repository at this point in the history
…o 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
  • Loading branch information
steveluscher authored Sep 11, 2023
1 parent df592e1 commit b098586
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ class WebSocket extends EventTarget {
* registered :-)
*/
delay(function delayCallback() {
if (this.readyState !== WebSocket.CONNECTING) {
return;
}
if (server) {
if (
server.options.verifyClient &&
Expand Down
4 changes: 4 additions & 0 deletions tests/functional/close-algorithm.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Expand Down

0 comments on commit b098586

Please sign in to comment.