@@ -420,6 +420,18 @@ describe('WebSocket', function () {
420420 } ) ;
421421
422422 describe ( '#pause and #resume' , function ( ) {
423+ it ( 'throws an error when `readyState` is not `OPEN` (pause)' , function ( ) {
424+ const ws = new WebSocket ( 'ws://localhost' , { agent : new CustomAgent ( ) } ) ;
425+
426+ assert . throws ( ( ) => ws . pause ( ) , / ^ E r r o r : n o t o p e n e d $ / ) ;
427+ } ) ;
428+
429+ it ( 'throws an error when `readyState` is not `OPEN` (resume)' , function ( ) {
430+ const ws = new WebSocket ( 'ws://localhost' , { agent : new CustomAgent ( ) } ) ;
431+
432+ assert . throws ( ( ) => ws . resume ( ) , / ^ E r r o r : n o t o p e n e d $ / ) ;
433+ } ) ;
434+
423435 it ( 'pauses the underlying stream' , function ( done ) {
424436 // this test is sort-of racecondition'y, since an unlikely slow connection
425437 // to localhost can cause the test to succeed even when the stream pausing
@@ -1067,35 +1079,30 @@ describe('WebSocket', function () {
10671079 } ) ;
10681080
10691081 describe ( 'WHATWG API emulation' , function ( ) {
1070- it ( 'should not throw errors when getting and setting' , function ( done ) {
1071- server . createServer ( ++ port , ( srv ) => {
1072- const listener = ( ) => { } ;
1073- const ws = new WebSocket ( `ws://localhost:${ port } ` ) ;
1074-
1075- assert . strictEqual ( ws . onmessage , undefined ) ;
1076- assert . strictEqual ( ws . onclose , undefined ) ;
1077- assert . strictEqual ( ws . onerror , undefined ) ;
1078- assert . strictEqual ( ws . onopen , undefined ) ;
1082+ it ( 'should not throw errors when getting and setting' , function ( ) {
1083+ const listener = ( ) => { } ;
1084+ const ws = new WebSocket ( 'ws://localhost' , { agent : new CustomAgent ( ) } ) ;
10791085
1080- ws . onmessage = listener ;
1081- ws . onerror = listener ;
1082- ws . onclose = listener ;
1083- ws . onopen = listener ;
1086+ assert . strictEqual ( ws . onmessage , undefined ) ;
1087+ assert . strictEqual ( ws . onclose , undefined ) ;
1088+ assert . strictEqual ( ws . onerror , undefined ) ;
1089+ assert . strictEqual ( ws . onopen , undefined ) ;
10841090
1085- assert . strictEqual ( ws . binaryType , 'nodebuffer' ) ;
1086- ws . binaryType = 'arraybuffer' ;
1087- assert . strictEqual ( ws . binaryType , 'arraybuffer' ) ;
1088- ws . binaryType = 'nodebuffer' ;
1089- assert . strictEqual ( ws . binaryType , 'nodebuffer' ) ;
1091+ ws . onmessage = listener ;
1092+ ws . onerror = listener ;
1093+ ws . onclose = listener ;
1094+ ws . onopen = listener ;
10901095
1091- assert . strictEqual ( ws . onmessage , listener ) ;
1092- assert . strictEqual ( ws . onclose , listener ) ;
1093- assert . strictEqual ( ws . onerror , listener ) ;
1094- assert . strictEqual ( ws . onopen , listener ) ;
1096+ assert . strictEqual ( ws . binaryType , 'nodebuffer' ) ;
1097+ ws . binaryType = 'arraybuffer' ;
1098+ assert . strictEqual ( ws . binaryType , 'arraybuffer' ) ;
1099+ ws . binaryType = 'nodebuffer' ;
1100+ assert . strictEqual ( ws . binaryType , 'nodebuffer' ) ;
10951101
1096- srv . close ( done ) ;
1097- ws . terminate ( ) ;
1098- } ) ;
1102+ assert . strictEqual ( ws . onmessage , listener ) ;
1103+ assert . strictEqual ( ws . onclose , listener ) ;
1104+ assert . strictEqual ( ws . onerror , listener ) ;
1105+ assert . strictEqual ( ws . onopen , listener ) ;
10991106 } ) ;
11001107
11011108 it ( 'should throw an error when setting an invalid binary type' , function ( ) {
@@ -1134,17 +1141,36 @@ describe('WebSocket', function () {
11341141 } ) ;
11351142 } ) ;
11361143
1137- it ( 'should receive text data wrapped in a MessageEvent when using addEventListener ' , function ( done ) {
1138- server . createServer ( ++ port , ( srv ) => {
1139- const ws = new WebSocket ( ` ws://localhost: ${ port } ` ) ;
1144+ it ( 'doesn\'t return event listeners added with `on` ' , function ( ) {
1145+ const listener = ( ) => { } ;
1146+ const ws = new WebSocket ( ' ws://localhost' , { agent : new CustomAgent ( ) } ) ;
11401147
1141- ws . addEventListener ( 'open' , ( ) => ws . send ( 'hi' ) ) ;
1142- ws . addEventListener ( 'message' , ( messageEvent ) => {
1143- assert . strictEqual ( messageEvent . data , 'hi' ) ;
1144- srv . close ( done ) ;
1145- ws . terminate ( ) ;
1146- } ) ;
1147- } ) ;
1148+ ws . on ( 'open' , listener ) ;
1149+
1150+ assert . deepStrictEqual ( ws . listeners ( 'open' ) , [ listener ] ) ;
1151+ assert . strictEqual ( ws . onopen , undefined ) ;
1152+ } ) ;
1153+
1154+ it ( 'doesn\'t remove event listeners added with `on`' , function ( ) {
1155+ const listener = ( ) => { } ;
1156+ const ws = new WebSocket ( 'ws://localhost' , { agent : new CustomAgent ( ) } ) ;
1157+
1158+ ws . on ( 'close' , listener ) ;
1159+ ws . onclose = listener ;
1160+
1161+ let listeners = ws . listeners ( 'close' ) ;
1162+
1163+ assert . strictEqual ( listeners . length , 2 ) ;
1164+ assert . strictEqual ( listeners [ 0 ] , listener ) ;
1165+ assert . strictEqual ( listeners [ 1 ] . _listener , listener ) ;
1166+
1167+ ws . onclose = listener ;
1168+
1169+ listeners = ws . listeners ( 'close' ) ;
1170+
1171+ assert . strictEqual ( listeners . length , 2 ) ;
1172+ assert . strictEqual ( listeners [ 0 ] , listener ) ;
1173+ assert . strictEqual ( listeners [ 1 ] . _listener , listener ) ;
11481174 } ) ;
11491175
11501176 it ( 'registers listeners for custom events with addEventListener' , function ( ) {
@@ -1186,6 +1212,19 @@ describe('WebSocket', function () {
11861212 assert . strictEqual ( ws . listeners ( 'foo' ) . length , 0 ) ;
11871213 } ) ;
11881214
1215+ it ( 'should receive text data wrapped in a MessageEvent when using addEventListener' , function ( done ) {
1216+ server . createServer ( ++ port , ( srv ) => {
1217+ const ws = new WebSocket ( `ws://localhost:${ port } ` ) ;
1218+
1219+ ws . addEventListener ( 'open' , ( ) => ws . send ( 'hi' ) ) ;
1220+ ws . addEventListener ( 'message' , ( messageEvent ) => {
1221+ assert . strictEqual ( messageEvent . data , 'hi' ) ;
1222+ srv . close ( done ) ;
1223+ ws . terminate ( ) ;
1224+ } ) ;
1225+ } ) ;
1226+ } ) ;
1227+
11891228 it ( 'should receive valid CloseEvent when server closes with code 1000' , function ( done ) {
11901229 const wss = new WebSocketServer ( { port : ++ port } , ( ) => {
11911230 const ws = new WebSocket ( `ws://localhost:${ port } ` ) ;
0 commit comments