import * as path from 'path'; import { tap, expect } from '@git.zone/tstest/tapbundle'; import { SmartProxy } from '../ts/proxies/smart-proxy/index.js'; import { SocketHandlers } from '../ts/proxies/smart-proxy/utils/socket-handlers.js'; import type { IRouteConfig } from '../ts/proxies/smart-proxy/models/route-types.js'; tap.test('Route-based configuration examples', async (tools) => { const httpOnlyRoute: IRouteConfig = { match: { ports: 80, domains: 'http.example.com' }, action: { type: 'forward', targets: [{ host: 'localhost', port: 3000 }] }, name: 'Basic HTTP Route' }; console.log('HTTP-only route created successfully:', httpOnlyRoute.name); expect(httpOnlyRoute.action.type).toEqual('forward'); expect(httpOnlyRoute.match.domains).toEqual('http.example.com'); const httpsPassthroughRoute: IRouteConfig = { match: { ports: 443, domains: 'pass.example.com' }, action: { type: 'forward', targets: [{ host: '10.0.0.1', port: 443 }, { host: '10.0.0.2', port: 443 }], tls: { mode: 'passthrough' } }, name: 'HTTPS Passthrough Route' }; expect(httpsPassthroughRoute).toBeTruthy(); expect(httpsPassthroughRoute.action.tls?.mode).toEqual('passthrough'); expect(Array.isArray(httpsPassthroughRoute.action.targets)).toBeTrue(); const terminateToHttpRoute: IRouteConfig = { match: { ports: 443, domains: 'secure.example.com' }, action: { type: 'forward', targets: [{ host: 'localhost', port: 8080 }], tls: { mode: 'terminate', certificate: 'auto' } }, name: 'HTTPS Termination to HTTP Backend' }; const httpToHttpsRedirect: IRouteConfig = { match: { ports: 80, domains: 'secure.example.com' }, action: { type: 'socket-handler', socketHandler: SocketHandlers.httpRedirect('https://{domain}:443{path}', 301) }, name: 'HTTP to HTTPS Redirect for secure.example.com' }; expect(terminateToHttpRoute).toBeTruthy(); expect(terminateToHttpRoute.action.tls?.mode).toEqual('terminate'); const loadBalancerRoute: IRouteConfig = { match: { ports: 443, domains: 'proxy.example.com' }, action: { type: 'forward', targets: [ { host: 'internal-api-1.local', port: 8443 }, { host: 'internal-api-2.local', port: 8443 } ], tls: { mode: 'terminate-and-reencrypt', certificate: 'auto' } }, name: 'Load Balanced HTTPS Route' }; expect(loadBalancerRoute).toBeTruthy(); expect(loadBalancerRoute.action.tls?.mode).toEqual('terminate-and-reencrypt'); expect(Array.isArray(loadBalancerRoute.action.targets)).toBeTrue(); const apiRoute: IRouteConfig = { match: { ports: 443, domains: 'api.example.com', path: '/api' }, action: { type: 'forward', targets: [{ host: 'localhost', port: 8081 }], tls: { mode: 'terminate', certificate: 'auto' } }, name: 'API Route' }; expect(apiRoute.action.type).toEqual('forward'); expect(apiRoute.match.path).toBeTruthy(); const httpsRoute: IRouteConfig = { match: { ports: 443, domains: 'complete.example.com' }, action: { type: 'forward', targets: [{ host: 'localhost', port: 8080 }], tls: { mode: 'terminate', certificate: 'auto' } }, name: 'Complete HTTPS Server' }; const httpsRedirectRoute: IRouteConfig = { match: { ports: 80, domains: 'complete.example.com' }, action: { type: 'socket-handler', socketHandler: SocketHandlers.httpRedirect('https://{domain}:443{path}', 301) }, name: 'Complete HTTPS Server - Redirect' }; const webSocketRoute: IRouteConfig = { match: { ports: 443, domains: 'ws.example.com', path: '/ws' }, action: { type: 'forward', targets: [{ host: 'localhost', port: 8082 }], tls: { mode: 'terminate', certificate: 'auto' }, websocket: { enabled: true } }, name: 'WebSocket Route' }; expect(webSocketRoute.action.type).toEqual('forward'); expect(webSocketRoute.action.websocket?.enabled).toBeTrue(); const allRoutes: IRouteConfig[] = [ httpOnlyRoute, httpsPassthroughRoute, terminateToHttpRoute, httpToHttpsRedirect, loadBalancerRoute, apiRoute, httpsRoute, httpsRedirectRoute, webSocketRoute ]; const smartProxy = new SmartProxy({ routes: allRoutes }); console.log(`Created ${allRoutes.length} example routes`); expect(allRoutes.length).toEqual(9); }); export default tap.start();