@@ -1158,6 +1158,35 @@ describe('WebSocket', () => {
1158
1158
} ) ;
1159
1159
} ) ;
1160
1160
1161
+ it ( 'honors the `createConnection` option' , ( done ) => {
1162
+ const wss = new WebSocket . Server ( { noServer : true , path : '/foo' } ) ;
1163
+
1164
+ server . once ( 'upgrade' , ( req , socket , head ) => {
1165
+ assert . strictEqual ( req . headers . host , 'google.com:22' ) ;
1166
+ wss . handleUpgrade ( req , socket , head , NOOP ) ;
1167
+ } ) ;
1168
+
1169
+ const ws = new WebSocket ( 'ws://google.com:22/foo' , {
1170
+ createConnection : ( options ) => {
1171
+ assert . strictEqual ( options . host , 'google.com' ) ;
1172
+ assert . strictEqual ( options . port , '22' ) ;
1173
+
1174
+ // Ignore the `options` argument, and use the correct hostname and
1175
+ // port to connect to the server.
1176
+ return net . createConnection ( {
1177
+ host : 'localhost' ,
1178
+ port : server . address ( ) . port
1179
+ } ) ;
1180
+ }
1181
+ } ) ;
1182
+
1183
+ ws . on ( 'open' , ( ) => {
1184
+ assert . strictEqual ( ws . url , 'ws://google.com:22/foo' ) ;
1185
+ ws . on ( 'close' , ( ) => done ( ) ) ;
1186
+ ws . close ( ) ;
1187
+ } ) ;
1188
+ } ) ;
1189
+
1161
1190
it ( 'does not follow redirects by default' , ( done ) => {
1162
1191
server . once ( 'upgrade' , ( req , socket ) => {
1163
1192
socket . end (
@@ -1224,34 +1253,6 @@ describe('WebSocket', () => {
1224
1253
} ) ;
1225
1254
} ) ;
1226
1255
1227
- it ( 'honors the `createConnection` option' , ( done ) => {
1228
- const wss = new WebSocket . Server ( { noServer : true , path : '/foo' } ) ;
1229
-
1230
- server . once ( 'upgrade' , ( req , socket , head ) => {
1231
- assert . strictEqual ( req . headers . host , 'google.com:22' ) ;
1232
- wss . handleUpgrade ( req , socket , head , NOOP ) ;
1233
- } ) ;
1234
-
1235
- const ws = new WebSocket ( 'ws://google.com:22/foo' , {
1236
- createConnection : ( options ) => {
1237
- assert . strictEqual ( options . host , 'google.com' ) ;
1238
- assert . strictEqual ( options . port , '22' ) ;
1239
-
1240
- // Ignore the invalid host address, and connect to the server manually:
1241
- return net . createConnection ( {
1242
- host : 'localhost' ,
1243
- port : server . address ( ) . port
1244
- } ) ;
1245
- }
1246
- } ) ;
1247
-
1248
- ws . on ( 'open' , ( ) => {
1249
- assert . strictEqual ( ws . url , 'ws://google.com:22/foo' ) ;
1250
- ws . on ( 'close' , ( ) => done ( ) ) ;
1251
- ws . close ( ) ;
1252
- } ) ;
1253
- } ) ;
1254
-
1255
1256
it ( 'emits an error if the redirect URL is invalid (1/2)' , ( done ) => {
1256
1257
server . once ( 'upgrade' , ( req , socket ) => {
1257
1258
socket . end ( 'HTTP/1.1 302 Found\r\nLocation: ws://\r\n\r\n' ) ;
0 commit comments