fix tests
This commit is contained in:
		| @@ -3,6 +3,7 @@ import { NetworkProxy } from '../ts/proxies/network-proxy/index.js'; | ||||
| import type { IRouteConfig } from '../ts/proxies/smart-proxy/models/route-types.js'; | ||||
| import type { IRouteContext } from '../ts/core/models/route-context.js'; | ||||
| import * as http from 'http'; | ||||
| import * as https from 'https'; | ||||
| import * as http2 from 'http2'; | ||||
|  | ||||
| const delay = (ms: number) => new Promise(resolve => setTimeout(resolve, ms)); | ||||
| @@ -62,10 +63,18 @@ tap.test('setup NetworkProxy function-based targets test environment', async () | ||||
|   // Create NetworkProxy instance | ||||
|   networkProxy = new NetworkProxy({ | ||||
|     port: 0, // Use dynamic port | ||||
|     logLevel: 'error' | ||||
|     logLevel: 'info', // Use info level to see more logs | ||||
|     // Disable ACME to avoid trying to bind to port 80 | ||||
|     acme: { | ||||
|       enabled: false | ||||
|     } | ||||
|   }); | ||||
|    | ||||
|   await networkProxy.start(); | ||||
|    | ||||
|   // Log the actual port being used | ||||
|   const actualPort = networkProxy.getListeningPort(); | ||||
|   console.log(`NetworkProxy actual listening port: ${actualPort}`); | ||||
| }); | ||||
|  | ||||
| // Test static host/port routes | ||||
| @@ -90,7 +99,7 @@ tap.test('should support static host/port routes', async () => { | ||||
|    | ||||
|   await networkProxy.updateRouteConfigs(routes); | ||||
|    | ||||
|   // Get proxy port | ||||
|   // Get proxy port using the improved getListeningPort() method  | ||||
|   const proxyPort = networkProxy.getListeningPort(); | ||||
|    | ||||
|   // Make request to proxy | ||||
| @@ -135,7 +144,7 @@ tap.test('should support function-based host', async () => { | ||||
|    | ||||
|   await networkProxy.updateRouteConfigs(routes); | ||||
|    | ||||
|   // Get proxy port | ||||
|   // Get proxy port using the improved getListeningPort() method  | ||||
|   const proxyPort = networkProxy.getListeningPort(); | ||||
|    | ||||
|   // Make request to proxy | ||||
| @@ -180,7 +189,7 @@ tap.test('should support function-based port', async () => { | ||||
|    | ||||
|   await networkProxy.updateRouteConfigs(routes); | ||||
|    | ||||
|   // Get proxy port | ||||
|   // Get proxy port using the improved getListeningPort() method  | ||||
|   const proxyPort = networkProxy.getListeningPort(); | ||||
|    | ||||
|   // Make request to proxy | ||||
| @@ -226,7 +235,7 @@ tap.test('should support function-based host AND port', async () => { | ||||
|    | ||||
|   await networkProxy.updateRouteConfigs(routes); | ||||
|    | ||||
|   // Get proxy port | ||||
|   // Get proxy port using the improved getListeningPort() method  | ||||
|   const proxyPort = networkProxy.getListeningPort(); | ||||
|    | ||||
|   // Make request to proxy | ||||
| @@ -275,7 +284,7 @@ tap.test('should support context-based routing with path', async () => { | ||||
|    | ||||
|   await networkProxy.updateRouteConfigs(routes); | ||||
|    | ||||
|   // Get proxy port | ||||
|   // Get proxy port using the improved getListeningPort() method  | ||||
|   const proxyPort = networkProxy.getListeningPort(); | ||||
|    | ||||
|   // Make request to proxy with /api path | ||||
| @@ -328,10 +337,14 @@ tap.test('cleanup NetworkProxy function-based targets test environment', async ( | ||||
|   } | ||||
| }); | ||||
|  | ||||
| // Helper function to make HTTP requests | ||||
| // Helper function to make HTTPS requests with self-signed certificate support | ||||
| async function makeRequest(options: http.RequestOptions): Promise<{ statusCode: number, headers: http.IncomingHttpHeaders, body: string }> { | ||||
|   return new Promise((resolve, reject) => { | ||||
|     const req = http.request(options, (res) => { | ||||
|     // Use HTTPS with rejectUnauthorized: false to accept self-signed certificates | ||||
|     const req = https.request({ | ||||
|       ...options, | ||||
|       rejectUnauthorized: false, // Accept self-signed certificates | ||||
|     }, (res) => { | ||||
|       let body = ''; | ||||
|       res.on('data', (chunk) => { | ||||
|         body += chunk; | ||||
| @@ -346,6 +359,7 @@ async function makeRequest(options: http.RequestOptions): Promise<{ statusCode: | ||||
|     }); | ||||
|      | ||||
|     req.on('error', (err) => { | ||||
|       console.error(`Request error: ${err.message}`); | ||||
|       reject(err); | ||||
|     }); | ||||
|      | ||||
|   | ||||
| @@ -82,7 +82,7 @@ tap.test('setup port proxy test environment', async () => { | ||||
|     ], | ||||
|     defaults: { | ||||
|       security: { | ||||
|         allowedIPs: ['127.0.0.1'] | ||||
|         allowedIps: ['127.0.0.1'] | ||||
|       } | ||||
|     } | ||||
|   }); | ||||
| @@ -121,7 +121,7 @@ tap.test('should forward TCP connections to custom host', async () => { | ||||
|     ], | ||||
|     defaults: { | ||||
|       security: { | ||||
|         allowedIPs: ['127.0.0.1'] | ||||
|         allowedIps: ['127.0.0.1'] | ||||
|       } | ||||
|     } | ||||
|   }); | ||||
| @@ -166,7 +166,7 @@ tap.test('should forward connections to custom IP', async () => { | ||||
|     ], | ||||
|     defaults: { | ||||
|       security: { | ||||
|         allowedIPs: ['127.0.0.1', '::ffff:127.0.0.1'] | ||||
|         allowedIps: ['127.0.0.1', '::ffff:127.0.0.1'] | ||||
|       } | ||||
|     } | ||||
|   }); | ||||
| @@ -261,7 +261,7 @@ tap.test('should support optional source IP preservation in chained proxies', as | ||||
|     ], | ||||
|     defaults: { | ||||
|       security: { | ||||
|         allowedIPs: ['127.0.0.1', '::ffff:127.0.0.1'] | ||||
|         allowedIps: ['127.0.0.1', '::ffff:127.0.0.1'] | ||||
|       } | ||||
|     } | ||||
|   }); | ||||
| @@ -282,7 +282,7 @@ tap.test('should support optional source IP preservation in chained proxies', as | ||||
|     ], | ||||
|     defaults: { | ||||
|       security: { | ||||
|         allowedIPs: ['127.0.0.1', '::ffff:127.0.0.1'] | ||||
|         allowedIps: ['127.0.0.1', '::ffff:127.0.0.1'] | ||||
|       } | ||||
|     } | ||||
|   }); | ||||
| @@ -320,7 +320,7 @@ tap.test('should support optional source IP preservation in chained proxies', as | ||||
|     ], | ||||
|     defaults: { | ||||
|       security: { | ||||
|         allowedIPs: ['127.0.0.1'] | ||||
|         allowedIps: ['127.0.0.1'] | ||||
|       }, | ||||
|       preserveSourceIP: true | ||||
|     }, | ||||
| @@ -343,7 +343,7 @@ tap.test('should support optional source IP preservation in chained proxies', as | ||||
|     ], | ||||
|     defaults: { | ||||
|       security: { | ||||
|         allowedIPs: ['127.0.0.1'] | ||||
|         allowedIps: ['127.0.0.1'] | ||||
|       }, | ||||
|       preserveSourceIP: true | ||||
|     }, | ||||
|   | ||||
| @@ -160,6 +160,14 @@ export class NetworkProxy implements IMetricsTracker { | ||||
|    * Useful for SmartProxy to determine where to forward connections | ||||
|    */ | ||||
|   public getListeningPort(): number { | ||||
|     // If the server is running, get the actual listening port | ||||
|     if (this.httpsServer && this.httpsServer.address()) { | ||||
|       const address = this.httpsServer.address(); | ||||
|       if (address && typeof address === 'object' && 'port' in address) { | ||||
|         return address.port; | ||||
|       } | ||||
|     } | ||||
|     // Fallback to configured port | ||||
|     return this.options.port; | ||||
|   } | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user