@@ -75,6 +75,8 @@ describe('WebSocketServer', () => {
75
75
} ,
76
76
( ) => {
77
77
const ws = new WebSocket ( `ws://localhost:${ wss . address ( ) . port } ` ) ;
78
+
79
+ ws . on ( 'open' , ws . close ) ;
78
80
}
79
81
) ;
80
82
@@ -103,6 +105,8 @@ describe('WebSocketServer', () => {
103
105
const port = 1337 ;
104
106
const wss = new WebSocket . Server ( { port } , ( ) => {
105
107
const ws = new WebSocket ( `ws://localhost:${ port } ` ) ;
108
+
109
+ ws . on ( 'open' , ws . close ) ;
106
110
} ) ;
107
111
108
112
wss . on ( 'connection' , ( ) => wss . close ( done ) ) ;
@@ -120,12 +124,14 @@ describe('WebSocketServer', () => {
120
124
121
125
server . listen ( 0 , ( ) => {
122
126
const wss = new WebSocket . Server ( { server } ) ;
123
- const ws = new WebSocket ( `ws://localhost:${ server . address ( ) . port } ` ) ;
124
127
125
128
wss . on ( 'connection' , ( ) => {
126
- wss . close ( ) ;
127
129
server . close ( done ) ;
128
130
} ) ;
131
+
132
+ const ws = new WebSocket ( `ws://localhost:${ server . address ( ) . port } ` ) ;
133
+
134
+ ws . on ( 'open' , ws . close ) ;
129
135
} ) ;
130
136
} ) ;
131
137
@@ -169,7 +175,11 @@ describe('WebSocketServer', () => {
169
175
assert . strictEqual ( req . url , '/foo?bar=bar' ) ;
170
176
} else {
171
177
assert . strictEqual ( req . url , '/' ) ;
172
- wss . close ( ) ;
178
+
179
+ for ( const client of wss . clients ) {
180
+ client . close ( ) ;
181
+ }
182
+
173
183
server . close ( done ) ;
174
184
}
175
185
} ) ;
@@ -209,30 +219,13 @@ describe('WebSocketServer', () => {
209
219
} ) ;
210
220
211
221
describe ( '#close' , ( ) => {
212
- it ( 'does not throw when called twice ' , ( done ) => {
222
+ it ( 'does not throw if called multiple times ' , ( done ) => {
213
223
const wss = new WebSocket . Server ( { port : 0 } , ( ) => {
224
+ wss . on ( 'close' , done ) ;
225
+
214
226
wss . close ( ) ;
215
227
wss . close ( ) ;
216
228
wss . close ( ) ;
217
-
218
- done ( ) ;
219
- } ) ;
220
- } ) ;
221
-
222
- it ( 'closes all clients' , ( done ) => {
223
- let closes = 0 ;
224
- const wss = new WebSocket . Server ( { port : 0 } , ( ) => {
225
- const ws = new WebSocket ( `ws://localhost:${ wss . address ( ) . port } ` ) ;
226
- ws . on ( 'close' , ( ) => {
227
- if ( ++ closes === 2 ) done ( ) ;
228
- } ) ;
229
- } ) ;
230
-
231
- wss . on ( 'connection' , ( ws ) => {
232
- ws . on ( 'close' , ( ) => {
233
- if ( ++ closes === 2 ) done ( ) ;
234
- } ) ;
235
- wss . close ( ) ;
236
229
} ) ;
237
230
} ) ;
238
231
@@ -254,6 +247,8 @@ describe('WebSocketServer', () => {
254
247
255
248
server . listen ( 0 , ( ) => {
256
249
const ws = new WebSocket ( `ws://localhost:${ server . address ( ) . port } ` ) ;
250
+
251
+ ws . on ( 'open' , ws . close ) ;
257
252
} ) ;
258
253
} ) ;
259
254
@@ -309,6 +304,16 @@ describe('WebSocketServer', () => {
309
304
} ) ;
310
305
} ) ;
311
306
307
+ it ( "emits the 'close' event if client tracking is disabled" , ( done ) => {
308
+ const wss = new WebSocket . Server ( {
309
+ noServer : true ,
310
+ clientTracking : false
311
+ } ) ;
312
+
313
+ wss . on ( 'close' , done ) ;
314
+ wss . close ( ) ;
315
+ } ) ;
316
+
312
317
it ( "emits the 'close' event if the server is already closed" , ( done ) => {
313
318
const wss = new WebSocket . Server ( { port : 0 } , ( ) => {
314
319
wss . close ( ( ) => {
@@ -324,7 +329,10 @@ describe('WebSocketServer', () => {
324
329
it ( 'returns a list of connected clients' , ( done ) => {
325
330
const wss = new WebSocket . Server ( { port : 0 } , ( ) => {
326
331
assert . strictEqual ( wss . clients . size , 0 ) ;
332
+
327
333
const ws = new WebSocket ( `ws://localhost:${ wss . address ( ) . port } ` ) ;
334
+
335
+ ws . on ( 'open' , ws . close ) ;
328
336
} ) ;
329
337
330
338
wss . on ( 'connection' , ( ) => {
@@ -404,17 +412,17 @@ describe('WebSocketServer', () => {
404
412
const wss = new WebSocket . Server ( { noServer : true } ) ;
405
413
406
414
server . on ( 'upgrade' , ( req , socket , head ) => {
407
- wss . handleUpgrade ( req , socket , head , ( client ) =>
408
- client . send ( 'hello' )
409
- ) ;
415
+ wss . handleUpgrade ( req , socket , head , ( ws ) => {
416
+ ws . send ( 'hello' ) ;
417
+ ws . close ( ) ;
418
+ } ) ;
410
419
} ) ;
411
420
412
421
const ws = new WebSocket ( `ws://localhost:${ server . address ( ) . port } ` ) ;
413
422
414
423
ws . on ( 'message' , ( message , isBinary ) => {
415
424
assert . deepStrictEqual ( message , Buffer . from ( 'hello' ) ) ;
416
425
assert . ok ( ! isBinary ) ;
417
- wss . close ( ) ;
418
426
server . close ( done ) ;
419
427
} ) ;
420
428
} ) ;
@@ -683,6 +691,7 @@ describe('WebSocketServer', () => {
683
691
684
692
socket . once ( 'data' , ( chunk ) => {
685
693
assert . strictEqual ( chunk [ 0 ] , 0x88 ) ;
694
+ socket . destroy ( ) ;
686
695
wss . close ( done ) ;
687
696
} ) ;
688
697
} ) ;
@@ -742,7 +751,6 @@ describe('WebSocketServer', () => {
742
751
} ) ;
743
752
744
753
wss . on ( 'connection' , ( ) => {
745
- wss . close ( ) ;
746
754
server . close ( done ) ;
747
755
} ) ;
748
756
@@ -751,6 +759,8 @@ describe('WebSocketServer', () => {
751
759
headers : { Origin : 'https://example.com' , foo : 'bar' } ,
752
760
rejectUnauthorized : false
753
761
} ) ;
762
+
763
+ ws . on ( 'open' , ws . close ) ;
754
764
} ) ;
755
765
} ) ;
756
766
@@ -762,6 +772,8 @@ describe('WebSocketServer', () => {
762
772
} ,
763
773
( ) => {
764
774
const ws = new WebSocket ( `ws://localhost:${ wss . address ( ) . port } ` ) ;
775
+
776
+ ws . on ( 'open' , ws . close ) ;
765
777
}
766
778
) ;
767
779
@@ -959,24 +971,30 @@ describe('WebSocketServer', () => {
959
971
wss . close ( done ) ;
960
972
} ) ;
961
973
} ) ;
974
+
975
+ wss . on ( 'connection' , ( ws ) => {
976
+ ws . close ( ) ;
977
+ } ) ;
962
978
} ) ;
963
979
} ) ;
964
980
965
981
it ( "emits the 'headers' event" , ( done ) => {
966
982
const wss = new WebSocket . Server ( { port : 0 } , ( ) => {
967
983
const ws = new WebSocket ( `ws://localhost:${ wss . address ( ) . port } ` ) ;
968
984
969
- wss . on ( 'headers' , ( headers , request ) => {
970
- assert . deepStrictEqual ( headers . slice ( 0 , 3 ) , [
971
- 'HTTP/1.1 101 Switching Protocols' ,
972
- 'Upgrade: websocket' ,
973
- 'Connection: Upgrade'
974
- ] ) ;
975
- assert . ok ( request instanceof http . IncomingMessage ) ;
976
- assert . strictEqual ( request . url , '/' ) ;
985
+ ws . on ( 'open' , ws . close ) ;
986
+ } ) ;
977
987
978
- wss . on ( 'connection' , ( ) => wss . close ( done ) ) ;
979
- } ) ;
988
+ wss . on ( 'headers' , ( headers , request ) => {
989
+ assert . deepStrictEqual ( headers . slice ( 0 , 3 ) , [
990
+ 'HTTP/1.1 101 Switching Protocols' ,
991
+ 'Upgrade: websocket' ,
992
+ 'Connection: Upgrade'
993
+ ] ) ;
994
+ assert . ok ( request instanceof http . IncomingMessage ) ;
995
+ assert . strictEqual ( request . url , '/' ) ;
996
+
997
+ wss . on ( 'connection' , ( ) => wss . close ( done ) ) ;
980
998
} ) ;
981
999
} ) ;
982
1000
} ) ;
@@ -985,6 +1003,8 @@ describe('WebSocketServer', () => {
985
1003
it ( 'is disabled by default' , ( done ) => {
986
1004
const wss = new WebSocket . Server ( { port : 0 } , ( ) => {
987
1005
const ws = new WebSocket ( `ws://localhost:${ wss . address ( ) . port } ` ) ;
1006
+
1007
+ ws . on ( 'open' , ws . close ) ;
988
1008
} ) ;
989
1009
990
1010
wss . on ( 'connection' , ( ws , req ) => {
@@ -1016,6 +1036,10 @@ describe('WebSocketServer', () => {
1016
1036
} ) ;
1017
1037
}
1018
1038
) ;
1039
+
1040
+ wss . on ( 'connection' , ( ws ) => {
1041
+ ws . close ( ) ;
1042
+ } ) ;
1019
1043
} ) ;
1020
1044
} ) ;
1021
1045
} ) ;
0 commit comments