@@ -36,8 +36,16 @@ describe('WebSocket', () => {
36
36
) ;
37
37
38
38
assert . throws (
39
- ( ) => new WebSocket ( 'https://websocket-echo.com' ) ,
40
- / ^ S y n t a x E r r o r : T h e U R L ' s p r o t o c o l m u s t b e o n e o f " w s : " , " w s s : " , o r " w s \+ u n i x : " $ /
39
+ ( ) => new WebSocket ( 'bad-scheme://websocket-echo.com' ) ,
40
+ ( err ) => {
41
+ assert . strictEqual (
42
+ err . message ,
43
+ 'The URL\'s protocol must be one of "ws:", "wss:", ' +
44
+ '"http:", "https", or "ws+unix:"'
45
+ ) ;
46
+
47
+ return true ;
48
+ }
41
49
) ;
42
50
43
51
assert . throws (
@@ -72,6 +80,30 @@ describe('WebSocket', () => {
72
80
const ws = new WebSocket ( new URL ( 'ws://[::1]' ) , { agent } ) ;
73
81
} ) ;
74
82
83
+ it ( 'allows the http scheme' , ( done ) => {
84
+ const agent = new CustomAgent ( ) ;
85
+
86
+ agent . addRequest = ( req , opts ) => {
87
+ assert . strictEqual ( opts . host , 'localhost' ) ;
88
+ assert . strictEqual ( opts . port , 80 ) ;
89
+ done ( ) ;
90
+ } ;
91
+
92
+ const ws = new WebSocket ( 'http://localhost' , { agent } ) ;
93
+ } ) ;
94
+
95
+ it ( 'allows the https scheme' , ( done ) => {
96
+ const agent = new https . Agent ( ) ;
97
+
98
+ agent . addRequest = ( req , opts ) => {
99
+ assert . strictEqual ( opts . host , 'localhost' ) ;
100
+ assert . strictEqual ( opts . port , 443 ) ;
101
+ done ( ) ;
102
+ } ;
103
+
104
+ const ws = new WebSocket ( 'https://localhost' , { agent } ) ;
105
+ } ) ;
106
+
75
107
describe ( 'options' , ( ) => {
76
108
it ( 'accepts the `options` object as 3rd argument' , ( ) => {
77
109
const agent = new http . Agent ( ) ;
@@ -539,10 +571,18 @@ describe('WebSocket', () => {
539
571
} ) ;
540
572
541
573
it ( 'exposes the server url' , ( ) => {
542
- const url = 'ws://localhost' ;
543
- const ws = new WebSocket ( url , { agent : new CustomAgent ( ) } ) ;
574
+ const schemes = new Map ( [
575
+ [ 'ws' , 'ws' ] ,
576
+ [ 'wss' , 'wss' ] ,
577
+ [ 'http' , 'ws' ] ,
578
+ [ 'https' , 'wss' ]
579
+ ] ) ;
580
+
581
+ for ( const [ key , value ] of schemes ) {
582
+ const ws = new WebSocket ( `${ key } ://localhost/` , { lookup ( ) { } } ) ;
544
583
545
- assert . strictEqual ( ws . url , url ) ;
584
+ assert . strictEqual ( ws . url , `${ value } ://localhost/` ) ;
585
+ }
546
586
} ) ;
547
587
} ) ;
548
588
} ) ;
@@ -1174,7 +1214,9 @@ describe('WebSocket', () => {
1174
1214
1175
1215
it ( 'emits an error if the redirect URL is invalid (2/2)' , ( done ) => {
1176
1216
server . once ( 'upgrade' , ( req , socket ) => {
1177
- socket . end ( 'HTTP/1.1 302 Found\r\nLocation: http://localhost\r\n\r\n' ) ;
1217
+ socket . end (
1218
+ 'HTTP/1.1 302 Found\r\nLocation: bad-scheme://localhost\r\n\r\n'
1219
+ ) ;
1178
1220
} ) ;
1179
1221
1180
1222
const ws = new WebSocket ( `ws://localhost:${ server . address ( ) . port } ` , {
@@ -1186,7 +1228,8 @@ describe('WebSocket', () => {
1186
1228
assert . ok ( err instanceof SyntaxError ) ;
1187
1229
assert . strictEqual (
1188
1230
err . message ,
1189
- 'The URL\'s protocol must be one of "ws:", "wss:", or "ws+unix:"'
1231
+ 'The URL\'s protocol must be one of "ws:", "wss:", ' +
1232
+ '"http:", "https", or "ws+unix:"'
1190
1233
) ;
1191
1234
assert . strictEqual ( ws . _redirects , 1 ) ;
1192
1235
0 commit comments