@@ -3,6 +3,7 @@ import * as http from 'http';
3
3
import * as https from 'https' ;
4
4
import { once } from 'events' ;
5
5
import assert from 'assert' ;
6
+ import WebSocket , { WebSocketServer } from 'ws' ;
6
7
import { json , req } from 'agent-base' ;
7
8
import { ProxyServer , createProxy } from 'proxy' ;
8
9
// @ts -expect-error no types
@@ -20,8 +21,10 @@ const sslOptions = {
20
21
describe ( 'ProxyAgent' , ( ) => {
21
22
// target servers
22
23
let httpServer : http . Server ;
24
+ let httpWebSocketServer : WebSocketServer ;
23
25
let httpServerUrl : URL ;
24
26
let httpsServer : https . Server ;
27
+ let httpsWebSocketServer : WebSocketServer ;
25
28
let httpsServerUrl : URL ;
26
29
27
30
// proxy servers
@@ -36,12 +39,14 @@ describe('ProxyAgent', () => {
36
39
beforeAll ( async ( ) => {
37
40
// setup target HTTP server
38
41
httpServer = http . createServer ( ) ;
42
+ httpWebSocketServer = new WebSocketServer ( { server : httpServer } ) ;
39
43
httpServerUrl = await listen ( httpServer ) ;
40
44
} ) ;
41
45
42
46
beforeAll ( async ( ) => {
43
47
// setup target SSL HTTPS server
44
48
httpsServer = https . createServer ( sslOptions ) ;
49
+ httpsWebSocketServer = new WebSocketServer ( { server : httpsServer } ) ;
45
50
httpsServerUrl = await listen ( httpsServer ) ;
46
51
} ) ;
47
52
@@ -79,9 +84,13 @@ describe('ProxyAgent', () => {
79
84
beforeEach ( ( ) => {
80
85
delete process . env . HTTP_PROXY ;
81
86
delete process . env . HTTPS_PROXY ;
87
+ delete process . env . WS_PROXY ;
88
+ delete process . env . WSS_PROXY ;
82
89
delete process . env . NO_PROXY ;
83
90
httpServer . removeAllListeners ( 'request' ) ;
84
91
httpsServer . removeAllListeners ( 'request' ) ;
92
+ httpWebSocketServer . removeAllListeners ( 'connection' ) ;
93
+ httpsWebSocketServer . removeAllListeners ( 'connection' ) ;
85
94
} ) ;
86
95
87
96
describe ( '"http" module' , ( ) => {
@@ -278,4 +287,57 @@ describe('ProxyAgent', () => {
278
287
assert ( requestUrl . href === urlParameter ) ;
279
288
} ) ;
280
289
} ) ;
290
+
291
+ describe ( '"ws" module' , ( ) => {
292
+ it ( 'should work over "http" proxy to `ws:` URL' , async ( ) => {
293
+ let requestCount = 0 ;
294
+ let connectionCount = 0 ;
295
+ httpServer . once ( 'request' , function ( req , res ) {
296
+ requestCount ++ ;
297
+ res . end ( ) ;
298
+ } ) ;
299
+ httpWebSocketServer . on ( 'connection' , ( ws ) => {
300
+ connectionCount ++ ;
301
+ ws . send ( 'OK' ) ;
302
+ } ) ;
303
+
304
+ process . env . WS_PROXY = httpProxyServerUrl . href ;
305
+ const agent = new ProxyAgent ( ) ;
306
+
307
+ const ws = new WebSocket ( httpServerUrl . href . replace ( 'http' , 'ws' ) , {
308
+ agent,
309
+ } ) ;
310
+ const [ message ] = await once ( ws , 'message' ) ;
311
+ expect ( connectionCount ) . toEqual ( 1 ) ;
312
+ expect ( requestCount ) . toEqual ( 0 ) ;
313
+ expect ( message . toString ( ) ) . toEqual ( 'OK' ) ;
314
+ ws . close ( ) ;
315
+ } ) ;
316
+
317
+ it ( 'should work over "http" proxy to `wss:` URL' , async ( ) => {
318
+ let requestCount = 0 ;
319
+ let connectionCount = 0 ;
320
+ httpsServer . once ( 'request' , function ( req , res ) {
321
+ requestCount ++ ;
322
+ res . end ( ) ;
323
+ } ) ;
324
+ httpsWebSocketServer . on ( 'connection' , ( ws ) => {
325
+ connectionCount ++ ;
326
+ ws . send ( 'OK' ) ;
327
+ } ) ;
328
+
329
+ process . env . WSS_PROXY = httpProxyServerUrl . href ;
330
+ const agent = new ProxyAgent ( ) ;
331
+
332
+ const ws = new WebSocket ( httpsServerUrl . href . replace ( 'https' , 'wss' ) , {
333
+ agent,
334
+ rejectUnauthorized : false
335
+ } ) ;
336
+ const [ message ] = await once ( ws , 'message' ) ;
337
+ expect ( connectionCount ) . toEqual ( 1 ) ;
338
+ expect ( requestCount ) . toEqual ( 0 ) ;
339
+ expect ( message . toString ( ) ) . toEqual ( 'OK' ) ;
340
+ ws . close ( ) ;
341
+ } ) ;
342
+ } ) ;
281
343
} ) ;
0 commit comments