Skip to content

Commit 3c462f7

Browse files
committed
fix: use close only when valid error code for messages
1 parent 4f73369 commit 3c462f7

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

lib/core/IncomingWebSocket.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@ class IncomingWebSocket extends EventEmitter {
117117
*/
118118
close(code, msg) {
119119
this.teardown = true;
120-
this.socket.close(code, msg);
120+
if(code >= 1000 && code < 1004)
121+
this.socket.close(code, msg);
121122
this.socket.terminate();
122123
}
123124

test/core/incomingWebSocket.test.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,23 +138,38 @@ describe('IncomingWebSocket', () => {
138138
describe('#close', () => {
139139
let terminateSpy;
140140
let closeSpy;
141-
before(() => {
141+
beforeEach(() => {
142142
terminateSpy = spy();
143143
closeSpy = spy();
144144
incomingWs.socket.terminate = terminateSpy;
145145
incomingWs.socket.close = closeSpy;
146-
incomingWs.close(1001, 'msg');
147146
});
148147

149-
it('should close the websocket', () => {
148+
it('should close and terminate the websocket when code is >= 1000 and < 1004', () => {
149+
incomingWs.close(1001, 'msg');
150150
assert(closeSpy.calledWith(1001, 'msg'));
151+
assert(terminateSpy.calledOnce);
152+
});
153+
154+
it('should not call close, only terminate the websocket when code is 1005', () => {
155+
incomingWs.close(1005, 'msg');
156+
assert.isFalse(closeSpy.called);
157+
assert(terminateSpy.calledOnce);
158+
});
159+
160+
it('should not call close, only terminate the websocket when code is 1006', () => {
161+
incomingWs.close(1006, 'msg');
162+
assert.isFalse(closeSpy.called);
163+
assert(terminateSpy.calledOnce);
151164
});
152165

153166
it('should terminate the websocket', () => {
167+
incomingWs.close(1001, 'msg');
154168
assert(terminateSpy.calledOnce);
155169
});
156170

157171
it('should set teardown to true', () => {
172+
incomingWs.close(1001, 'msg');
158173
expect(incomingWs.teardown).to.equal(true);
159174
});
160175
});

0 commit comments

Comments
 (0)