@@ -6,9 +6,11 @@ const safeBuffer = require('safe-buffer');
6
6
const assert = require ( 'assert' ) ;
7
7
const crypto = require ( 'crypto' ) ;
8
8
const https = require ( 'https' ) ;
9
+ const dns = require ( 'dns' ) ;
9
10
const http = require ( 'http' ) ;
10
11
const net = require ( 'net' ) ;
11
12
const fs = require ( 'fs' ) ;
13
+ const os = require ( 'os' ) ;
12
14
13
15
const constants = require ( '../lib/Constants' ) ;
14
16
const WebSocket = require ( '..' ) ;
@@ -100,25 +102,38 @@ describe('WebSocket', function () {
100
102
} ) ;
101
103
102
104
it ( 'accepts the family option' , function ( done ) {
103
- const wss = new WebSocket . Server ( { host : '::1' , port : 0 } , ( ) => {
104
- const port = wss . _server . address ( ) . port ;
105
- const ws = new WebSocket ( `ws://localhost:${ port } ` , { family : 6 } ) ;
105
+ const re = process . platform === 'win32' ? / L o o p b a c k P s e u d o - I n t e r f a c e / : / l o / ;
106
+ const ifaces = os . networkInterfaces ( ) ;
107
+ const hasIPv6 = Object . keys ( ifaces ) . some ( ( name ) => {
108
+ return re . test ( name ) && ifaces [ name ] . some ( ( info ) => info . family === 'IPv6' ) ;
106
109
} ) ;
107
110
108
- wss . on ( 'error' , ( err ) => {
109
- wss . close ( ( ) => {
110
- //
111
- // Skip this test on machines where IPv6 is not supported.
112
- //
113
- if ( err . code === 'EADDRNOTAVAIL' ) return this . skip ( ) ;
111
+ //
112
+ // Skip this test on machines where IPv6 is not supported.
113
+ //
114
+ if ( ! hasIPv6 ) return this . skip ( ) ;
115
+
116
+ dns . lookup ( 'localhost' , { family : 6 , all : true } , ( err , addresses ) => {
117
+ //
118
+ // Skip this test if localhost does not resolve to ::1.
119
+ //
120
+ if ( err ) {
121
+ return err . code === 'ENOTFOUND' || err . code === 'EAI_AGAIN'
122
+ ? this . skip ( )
123
+ : done ( err ) ;
124
+ }
125
+
126
+ if ( ! addresses . some ( ( val ) => val . address === '::1' ) ) return this . skip ( ) ;
114
127
115
- done ( err ) ;
128
+ const wss = new WebSocket . Server ( { host : '::1' , port : 0 } , ( ) => {
129
+ const port = wss . _server . address ( ) . port ;
130
+ const ws = new WebSocket ( `ws://localhost:${ port } ` , { family : 6 } ) ;
116
131
} ) ;
117
- } ) ;
118
132
119
- wss . on ( 'connection' , ( ws , req ) => {
120
- assert . strictEqual ( req . connection . remoteAddress , '::1' ) ;
121
- wss . close ( done ) ;
133
+ wss . on ( 'connection' , ( ws , req ) => {
134
+ assert . strictEqual ( req . connection . remoteAddress , '::1' ) ;
135
+ wss . close ( done ) ;
136
+ } ) ;
122
137
} ) ;
123
138
} ) ;
124
139
} ) ;
0 commit comments