@@ -3,6 +3,7 @@ import * as http from 'http';
33import * as https from 'https' ;
44import { once } from 'events' ;
55import assert from 'assert' ;
6+ import WebSocket , { WebSocketServer } from 'ws' ;
67import { json , req } from 'agent-base' ;
78import { ProxyServer , createProxy } from 'proxy' ;
89// @ts -expect-error no types
@@ -20,8 +21,10 @@ const sslOptions = {
2021describe ( 'ProxyAgent' , ( ) => {
2122 // target servers
2223 let httpServer : http . Server ;
24+ let httpWebSocketServer : WebSocketServer ;
2325 let httpServerUrl : URL ;
2426 let httpsServer : https . Server ;
27+ let httpsWebSocketServer : WebSocketServer ;
2528 let httpsServerUrl : URL ;
2629
2730 // proxy servers
@@ -36,12 +39,14 @@ describe('ProxyAgent', () => {
3639 beforeAll ( async ( ) => {
3740 // setup target HTTP server
3841 httpServer = http . createServer ( ) ;
42+ httpWebSocketServer = new WebSocketServer ( { server : httpServer } ) ;
3943 httpServerUrl = await listen ( httpServer ) ;
4044 } ) ;
4145
4246 beforeAll ( async ( ) => {
4347 // setup target SSL HTTPS server
4448 httpsServer = https . createServer ( sslOptions ) ;
49+ httpsWebSocketServer = new WebSocketServer ( { server : httpsServer } ) ;
4550 httpsServerUrl = await listen ( httpsServer ) ;
4651 } ) ;
4752
@@ -79,9 +84,13 @@ describe('ProxyAgent', () => {
7984 beforeEach ( ( ) => {
8085 delete process . env . HTTP_PROXY ;
8186 delete process . env . HTTPS_PROXY ;
87+ delete process . env . WS_PROXY ;
88+ delete process . env . WSS_PROXY ;
8289 delete process . env . NO_PROXY ;
8390 httpServer . removeAllListeners ( 'request' ) ;
8491 httpsServer . removeAllListeners ( 'request' ) ;
92+ httpWebSocketServer . removeAllListeners ( 'connection' ) ;
93+ httpsWebSocketServer . removeAllListeners ( 'connection' ) ;
8594 } ) ;
8695
8796 describe ( '"http" module' , ( ) => {
@@ -278,4 +287,57 @@ describe('ProxyAgent', () => {
278287 assert ( requestUrl . href === urlParameter ) ;
279288 } ) ;
280289 } ) ;
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+ } ) ;
281343} ) ;
0 commit comments