@@ -2341,6 +2341,52 @@ describe('WebSocket', () => {
2341
2341
ws . on ( 'message' , ( message ) => ws . send ( message , { compress : true } ) ) ;
2342
2342
} ) ;
2343
2343
} ) ;
2344
+
2345
+ it ( 'calls the callback if the socket is closed prematurely' , ( done ) => {
2346
+ const wss = new WebSocket . Server (
2347
+ { perMessageDeflate : true , port : 0 } ,
2348
+ ( ) => {
2349
+ const called = [ ] ;
2350
+ const ws = new WebSocket ( `ws://localhost:${ wss . address ( ) . port } ` , {
2351
+ perMessageDeflate : { threshold : 0 }
2352
+ } ) ;
2353
+
2354
+ ws . on ( 'open' , ( ) => {
2355
+ ws . send ( 'foo' ) ;
2356
+ ws . send ( 'bar' , ( err ) => {
2357
+ called . push ( 1 ) ;
2358
+
2359
+ assert . strictEqual ( ws . readyState , WebSocket . CLOSING ) ;
2360
+ assert . ok ( err instanceof Error ) ;
2361
+ assert . strictEqual (
2362
+ err . message ,
2363
+ 'The socket was closed while data was being compressed'
2364
+ ) ;
2365
+ } ) ;
2366
+ ws . send ( 'baz' ) ;
2367
+ ws . send ( 'qux' , ( err ) => {
2368
+ called . push ( 2 ) ;
2369
+
2370
+ assert . strictEqual ( ws . readyState , WebSocket . CLOSING ) ;
2371
+ assert . ok ( err instanceof Error ) ;
2372
+ assert . strictEqual (
2373
+ err . message ,
2374
+ 'The socket was closed while data was being compressed'
2375
+ ) ;
2376
+ } ) ;
2377
+ } ) ;
2378
+
2379
+ ws . on ( 'close' , ( ) => {
2380
+ assert . deepStrictEqual ( called , [ 1 , 2 ] ) ;
2381
+ wss . close ( done ) ;
2382
+ } ) ;
2383
+ }
2384
+ ) ;
2385
+
2386
+ wss . on ( 'connection' , ( ws ) => {
2387
+ ws . _socket . end ( ) ;
2388
+ } ) ;
2389
+ } ) ;
2344
2390
} ) ;
2345
2391
2346
2392
describe ( '#terminate' , ( ) => {
@@ -2356,19 +2402,22 @@ describe('WebSocket', () => {
2356
2402
} ) ;
2357
2403
2358
2404
ws . on ( 'open' , ( ) => {
2359
- ws . send ( 'hi' , ( ) =>
2360
- done ( new Error ( 'Unexpected callback invocation' ) )
2361
- ) ;
2405
+ ws . send ( 'hi' , ( err ) => {
2406
+ assert . strictEqual ( ws . readyState , WebSocket . CLOSING ) ;
2407
+ assert . ok ( err instanceof Error ) ;
2408
+ assert . strictEqual (
2409
+ err . message ,
2410
+ 'The socket was closed while data was being compressed'
2411
+ ) ;
2412
+
2413
+ ws . on ( 'close' , ( ) => {
2414
+ wss . close ( done ) ;
2415
+ } ) ;
2416
+ } ) ;
2362
2417
ws . terminate ( ) ;
2363
2418
} ) ;
2364
2419
}
2365
2420
) ;
2366
-
2367
- wss . on ( 'connection' , ( ws ) => {
2368
- ws . on ( 'close' , ( ) => {
2369
- wss . close ( done ) ;
2370
- } ) ;
2371
- } ) ;
2372
2421
} ) ;
2373
2422
2374
2423
it ( 'can be used while data is being decompressed' , ( done ) => {
0 commit comments