@@ -1444,7 +1444,7 @@ describe('WebSocket', () => {
1444
1444
} ) ;
1445
1445
1446
1446
describe ( '#close' , ( ) => {
1447
- it ( 'closes the connection if called while connecting (1/2 )' , ( done ) => {
1447
+ it ( 'closes the connection if called while connecting (1/3 )' , ( done ) => {
1448
1448
const wss = new WebSocket . Server ( { port : 0 } , ( ) => {
1449
1449
const ws = new WebSocket ( `ws://localhost:${ wss . address ( ) . port } ` ) ;
1450
1450
@@ -1461,7 +1461,7 @@ describe('WebSocket', () => {
1461
1461
} ) ;
1462
1462
} ) ;
1463
1463
1464
- it ( 'closes the connection if called while connecting (2/2 )' , ( done ) => {
1464
+ it ( 'closes the connection if called while connecting (2/3 )' , ( done ) => {
1465
1465
const wss = new WebSocket . Server (
1466
1466
{
1467
1467
verifyClient : ( info , cb ) => setTimeout ( cb , 300 , true ) ,
@@ -1484,6 +1484,54 @@ describe('WebSocket', () => {
1484
1484
) ;
1485
1485
} ) ;
1486
1486
1487
+ it ( 'closes the connection if called while connecting (3/3)' , ( done ) => {
1488
+ const server = http . createServer ( ) ;
1489
+
1490
+ server . listen ( 0 , function ( ) {
1491
+ const ws = new WebSocket ( `ws://localhost:${ server . address ( ) . port } ` ) ;
1492
+
1493
+ ws . on ( 'open' , ( ) => done ( new Error ( "Unexpected 'open' event" ) ) ) ;
1494
+ ws . on ( 'error' , ( err ) => {
1495
+ assert . ok ( err instanceof Error ) ;
1496
+ assert . strictEqual (
1497
+ err . message ,
1498
+ 'WebSocket was closed before the connection was established'
1499
+ ) ;
1500
+ ws . on ( 'close' , ( ) => {
1501
+ server . close ( done ) ;
1502
+ } ) ;
1503
+ } ) ;
1504
+
1505
+ ws . on ( 'unexpected-response' , ( req , res ) => {
1506
+ assert . strictEqual ( res . statusCode , 502 ) ;
1507
+
1508
+ const chunks = [ ] ;
1509
+
1510
+ res . on ( 'data' , ( chunk ) => {
1511
+ chunks . push ( chunk ) ;
1512
+ } ) ;
1513
+
1514
+ res . on ( 'end' , ( ) => {
1515
+ assert . strictEqual ( Buffer . concat ( chunks ) . toString ( ) , 'foo' ) ;
1516
+ ws . close ( ) ;
1517
+ } ) ;
1518
+ } ) ;
1519
+ } ) ;
1520
+
1521
+ server . on ( 'upgrade' , ( req , socket ) => {
1522
+ socket . on ( 'end' , socket . end ) ;
1523
+
1524
+ socket . write (
1525
+ `HTTP/1.1 502 ${ http . STATUS_CODES [ 502 ] } \r\n` +
1526
+ 'Connection: keep-alive\r\n' +
1527
+ 'Content-type: text/html\r\n' +
1528
+ 'Content-Length: 3\r\n' +
1529
+ '\r\n' +
1530
+ 'foo'
1531
+ ) ;
1532
+ } ) ;
1533
+ } ) ;
1534
+
1487
1535
it ( 'can be called from an error listener while connecting' , ( done ) => {
1488
1536
const ws = new WebSocket ( 'ws://localhost:1337' ) ;
1489
1537
0 commit comments