@@ -12,7 +12,7 @@ const { URL } = require('url');
12
12
13
13
const Sender = require ( '../lib/sender' ) ;
14
14
const WebSocket = require ( '..' ) ;
15
- const { GUID , NOOP } = require ( '../lib/constants' ) ;
15
+ const { EMPTY_BUFFER , GUID , NOOP } = require ( '../lib/constants' ) ;
16
16
17
17
class CustomAgent extends http . Agent {
18
18
addRequest ( ) { }
@@ -3053,7 +3053,7 @@ describe('WebSocket', () => {
3053
3053
} ) ;
3054
3054
} ) ;
3055
3055
3056
- describe ( 'Connection close edge cases ' , ( ) => {
3056
+ describe ( 'Connection close' , ( ) => {
3057
3057
it ( 'closes cleanly after simultaneous errors (1/2)' , ( done ) => {
3058
3058
let clientCloseEventEmitted = false ;
3059
3059
let serverClientCloseEventEmitted = false ;
@@ -3165,5 +3165,59 @@ describe('WebSocket', () => {
3165
3165
} ) ;
3166
3166
} ) ;
3167
3167
} ) ;
3168
+
3169
+ it ( 'resumes the socket when an error occurs' , ( done ) => {
3170
+ const maxPayload = 16 * 1024 ;
3171
+ const wss = new WebSocket . Server ( { maxPayload, port : 0 } , ( ) => {
3172
+ const ws = new WebSocket ( `ws://localhost:${ wss . address ( ) . port } ` ) ;
3173
+ } ) ;
3174
+
3175
+ wss . on ( 'connection' , ( ws ) => {
3176
+ const list = [
3177
+ ...Sender . frame ( Buffer . alloc ( maxPayload + 1 ) , {
3178
+ fin : true ,
3179
+ opcode : 0x02 ,
3180
+ mask : true ,
3181
+ readOnly : false
3182
+ } )
3183
+ ] ;
3184
+
3185
+ ws . on ( 'error' , ( err ) => {
3186
+ assert . ok ( err instanceof RangeError ) ;
3187
+ assert . strictEqual ( err . code , 'WS_ERR_UNSUPPORTED_MESSAGE_LENGTH' ) ;
3188
+ assert . strictEqual ( err . message , 'Max payload size exceeded' ) ;
3189
+
3190
+ ws . on ( 'close' , ( code , reason ) => {
3191
+ assert . strictEqual ( code , 1006 ) ;
3192
+ assert . strictEqual ( reason , '' ) ;
3193
+ wss . close ( done ) ;
3194
+ } ) ;
3195
+ } ) ;
3196
+
3197
+ ws . _socket . push ( Buffer . concat ( list ) ) ;
3198
+ } ) ;
3199
+ } ) ;
3200
+
3201
+ it ( 'resumes the socket when the close frame is received' , ( done ) => {
3202
+ const wss = new WebSocket . Server ( { port : 0 } , ( ) => {
3203
+ const ws = new WebSocket ( `ws://localhost:${ wss . address ( ) . port } ` ) ;
3204
+ } ) ;
3205
+
3206
+ wss . on ( 'connection' , ( ws ) => {
3207
+ const opts = { fin : true , mask : true , readOnly : false } ;
3208
+ const list = [
3209
+ ...Sender . frame ( Buffer . alloc ( 16 * 1024 ) , { opcode : 0x02 , ...opts } ) ,
3210
+ ...Sender . frame ( EMPTY_BUFFER , { opcode : 0x08 , ...opts } )
3211
+ ] ;
3212
+
3213
+ ws . on ( 'close' , ( code , reason ) => {
3214
+ assert . strictEqual ( code , 1005 ) ;
3215
+ assert . strictEqual ( reason , '' ) ;
3216
+ wss . close ( done ) ;
3217
+ } ) ;
3218
+
3219
+ ws . _socket . push ( Buffer . concat ( list ) ) ;
3220
+ } ) ;
3221
+ } ) ;
3168
3222
} ) ;
3169
3223
} ) ;
0 commit comments