update
This commit is contained in:
@ -4,9 +4,15 @@ import type { IForwardConfig, TForwardingType } from '../ts/forwarding/config/fo
|
||||
|
||||
// First, import the components directly to avoid issues with compiled modules
|
||||
import { ForwardingHandlerFactory } from '../ts/forwarding/factory/forwarding-factory.js';
|
||||
import { createDomainConfig } from '../ts/forwarding/config/domain-config.js';
|
||||
import { DomainManager } from '../ts/forwarding/config/domain-manager.js';
|
||||
import { httpOnly, tlsTerminateToHttp, tlsTerminateToHttps, httpsPassthrough } from '../ts/forwarding/config/forwarding-types.js';
|
||||
// Import route-based helpers
|
||||
import {
|
||||
createHttpRoute,
|
||||
createHttpsTerminateRoute,
|
||||
createHttpsPassthroughRoute,
|
||||
createHttpToHttpsRedirect,
|
||||
createCompleteHttpsServer
|
||||
} from '../ts/proxies/smart-proxy/utils/route-helpers.js';
|
||||
|
||||
const helpers = {
|
||||
httpOnly,
|
||||
@ -15,6 +21,24 @@ const helpers = {
|
||||
httpsPassthrough
|
||||
};
|
||||
|
||||
// Route-based utility functions for testing
|
||||
function findRouteForDomain(routes: any[], domain: string): any {
|
||||
return routes.find(route => {
|
||||
const domains = Array.isArray(route.match.domains)
|
||||
? route.match.domains
|
||||
: [route.match.domains];
|
||||
|
||||
return domains.some(d => {
|
||||
// Handle wildcard domains
|
||||
if (d.startsWith('*.')) {
|
||||
const suffix = d.substring(2);
|
||||
return domain.endsWith(suffix) && domain.split('.').length > suffix.split('.').length;
|
||||
}
|
||||
return d === domain;
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
tap.test('ForwardingHandlerFactory - apply defaults based on type', async () => {
|
||||
// HTTP-only defaults
|
||||
const httpConfig: IForwardConfig = {
|
||||
@ -102,98 +126,108 @@ tap.test('ForwardingHandlerFactory - validate configuration', async () => {
|
||||
|
||||
expect(() => ForwardingHandlerFactory.validateConfig(invalidConfig4)).toThrow();
|
||||
});
|
||||
tap.test('DomainManager - manage domain configurations', async () => {
|
||||
const domainManager = new DomainManager();
|
||||
tap.test('Route Management - manage route configurations', async () => {
|
||||
// Create an array to store routes
|
||||
const routes: any[] = [];
|
||||
|
||||
// Add a domain configuration
|
||||
await domainManager.addDomainConfig(
|
||||
createDomainConfig('example.com', helpers.httpOnly({
|
||||
target: { host: 'localhost', port: 3000 }
|
||||
}))
|
||||
);
|
||||
// Add a route configuration
|
||||
const httpRoute = createHttpRoute('example.com', { host: 'localhost', port: 3000 });
|
||||
routes.push(httpRoute);
|
||||
|
||||
// Check that the configuration was added
|
||||
const configs = domainManager.getDomainConfigs();
|
||||
expect(configs.length).toEqual(1);
|
||||
expect(configs[0].domains[0]).toEqual('example.com');
|
||||
expect(configs[0].forwarding.type).toEqual('http-only');
|
||||
expect(routes.length).toEqual(1);
|
||||
expect(routes[0].match.domains).toEqual('example.com');
|
||||
expect(routes[0].action.type).toEqual('forward');
|
||||
expect(routes[0].action.target.host).toEqual('localhost');
|
||||
expect(routes[0].action.target.port).toEqual(3000);
|
||||
|
||||
// Find a handler for a domain
|
||||
const handler = domainManager.findHandlerForDomain('example.com');
|
||||
expect(handler).toBeDefined();
|
||||
// Find a route for a domain
|
||||
const foundRoute = findRouteForDomain(routes, 'example.com');
|
||||
expect(foundRoute).toBeDefined();
|
||||
|
||||
// Remove a domain configuration
|
||||
const removed = domainManager.removeDomainConfig('example.com');
|
||||
expect(removed).toBeTrue();
|
||||
// Remove a route configuration
|
||||
const initialLength = routes.length;
|
||||
const domainToRemove = 'example.com';
|
||||
const indexToRemove = routes.findIndex(route => {
|
||||
const domains = Array.isArray(route.match.domains) ? route.match.domains : [route.match.domains];
|
||||
return domains.includes(domainToRemove);
|
||||
});
|
||||
|
||||
if (indexToRemove !== -1) {
|
||||
routes.splice(indexToRemove, 1);
|
||||
}
|
||||
|
||||
expect(routes.length).toEqual(initialLength - 1);
|
||||
|
||||
// Check that the configuration was removed
|
||||
const configsAfterRemoval = domainManager.getDomainConfigs();
|
||||
expect(configsAfterRemoval.length).toEqual(0);
|
||||
expect(routes.length).toEqual(0);
|
||||
|
||||
// Check that no handler exists anymore
|
||||
const handlerAfterRemoval = domainManager.findHandlerForDomain('example.com');
|
||||
expect(handlerAfterRemoval).toBeUndefined();
|
||||
// Check that no route exists anymore
|
||||
const notFoundRoute = findRouteForDomain(routes, 'example.com');
|
||||
expect(notFoundRoute).toBeUndefined();
|
||||
});
|
||||
|
||||
tap.test('DomainManager - support wildcard domains', async () => {
|
||||
const domainManager = new DomainManager();
|
||||
tap.test('Route Management - support wildcard domains', async () => {
|
||||
// Create an array to store routes
|
||||
const routes: any[] = [];
|
||||
|
||||
// Add a wildcard domain configuration
|
||||
await domainManager.addDomainConfig(
|
||||
createDomainConfig('*.example.com', helpers.httpOnly({
|
||||
target: { host: 'localhost', port: 3000 }
|
||||
}))
|
||||
);
|
||||
// Add a wildcard domain route
|
||||
const wildcardRoute = createHttpRoute('*.example.com', { host: 'localhost', port: 3000 });
|
||||
routes.push(wildcardRoute);
|
||||
|
||||
// Find a handler for a subdomain
|
||||
const handler = domainManager.findHandlerForDomain('test.example.com');
|
||||
expect(handler).toBeDefined();
|
||||
// Find a route for a subdomain
|
||||
const foundRoute = findRouteForDomain(routes, 'test.example.com');
|
||||
expect(foundRoute).toBeDefined();
|
||||
|
||||
// Find a handler for a different domain (should not match)
|
||||
const noHandler = domainManager.findHandlerForDomain('example.org');
|
||||
expect(noHandler).toBeUndefined();
|
||||
// Find a route for a different domain (should not match)
|
||||
const notFoundRoute = findRouteForDomain(routes, 'example.org');
|
||||
expect(notFoundRoute).toBeUndefined();
|
||||
});
|
||||
tap.test('Helper Functions - create http-only forwarding config', async () => {
|
||||
const config = helpers.httpOnly({
|
||||
target: { host: 'localhost', port: 3000 }
|
||||
});
|
||||
expect(config.type).toEqual('http-only');
|
||||
expect(config.target.host).toEqual('localhost');
|
||||
expect(config.target.port).toEqual(3000);
|
||||
expect(config.http?.enabled).toBeTrue();
|
||||
tap.test('Route Helper Functions - create HTTP route', async () => {
|
||||
const route = createHttpRoute('example.com', { host: 'localhost', port: 3000 });
|
||||
expect(route.match.domains).toEqual('example.com');
|
||||
expect(route.match.ports).toEqual(80);
|
||||
expect(route.action.type).toEqual('forward');
|
||||
expect(route.action.target.host).toEqual('localhost');
|
||||
expect(route.action.target.port).toEqual(3000);
|
||||
});
|
||||
|
||||
tap.test('Helper Functions - create https-terminate-to-http config', async () => {
|
||||
const config = helpers.tlsTerminateToHttp({
|
||||
target: { host: 'localhost', port: 3000 }
|
||||
});
|
||||
expect(config.type).toEqual('https-terminate-to-http');
|
||||
expect(config.target.host).toEqual('localhost');
|
||||
expect(config.target.port).toEqual(3000);
|
||||
expect(config.http?.redirectToHttps).toBeTrue();
|
||||
expect(config.acme?.enabled).toBeTrue();
|
||||
expect(config.acme?.maintenance).toBeTrue();
|
||||
tap.test('Route Helper Functions - create HTTPS terminate route', async () => {
|
||||
const route = createHttpsTerminateRoute('example.com', { host: 'localhost', port: 3000 });
|
||||
expect(route.match.domains).toEqual('example.com');
|
||||
expect(route.match.ports).toEqual(443);
|
||||
expect(route.action.type).toEqual('forward');
|
||||
expect(route.action.target.host).toEqual('localhost');
|
||||
expect(route.action.target.port).toEqual(3000);
|
||||
expect(route.action.tls?.mode).toEqual('terminate');
|
||||
expect(route.action.tls?.certificate).toEqual('auto');
|
||||
});
|
||||
|
||||
tap.test('Helper Functions - create https-terminate-to-https config', async () => {
|
||||
const config = helpers.tlsTerminateToHttps({
|
||||
target: { host: 'localhost', port: 8443 }
|
||||
});
|
||||
expect(config.type).toEqual('https-terminate-to-https');
|
||||
expect(config.target.host).toEqual('localhost');
|
||||
expect(config.target.port).toEqual(8443);
|
||||
expect(config.http?.redirectToHttps).toBeTrue();
|
||||
expect(config.acme?.enabled).toBeTrue();
|
||||
expect(config.acme?.maintenance).toBeTrue();
|
||||
tap.test('Route Helper Functions - create complete HTTPS server', async () => {
|
||||
const routes = createCompleteHttpsServer('example.com', { host: 'localhost', port: 8443 });
|
||||
expect(routes.length).toEqual(2);
|
||||
|
||||
// HTTPS route
|
||||
expect(routes[0].match.domains).toEqual('example.com');
|
||||
expect(routes[0].match.ports).toEqual(443);
|
||||
expect(routes[0].action.type).toEqual('forward');
|
||||
expect(routes[0].action.target.host).toEqual('localhost');
|
||||
expect(routes[0].action.target.port).toEqual(8443);
|
||||
expect(routes[0].action.tls?.mode).toEqual('terminate');
|
||||
|
||||
// HTTP redirect route
|
||||
expect(routes[1].match.domains).toEqual('example.com');
|
||||
expect(routes[1].match.ports).toEqual(80);
|
||||
expect(routes[1].action.type).toEqual('redirect');
|
||||
});
|
||||
|
||||
tap.test('Helper Functions - create https-passthrough config', async () => {
|
||||
const config = helpers.httpsPassthrough({
|
||||
target: { host: 'localhost', port: 443 }
|
||||
});
|
||||
expect(config.type).toEqual('https-passthrough');
|
||||
expect(config.target.host).toEqual('localhost');
|
||||
expect(config.target.port).toEqual(443);
|
||||
expect(config.https?.forwardSni).toBeTrue();
|
||||
tap.test('Route Helper Functions - create HTTPS passthrough route', async () => {
|
||||
const route = createHttpsPassthroughRoute('example.com', { host: 'localhost', port: 443 });
|
||||
expect(route.match.domains).toEqual('example.com');
|
||||
expect(route.match.ports).toEqual(443);
|
||||
expect(route.action.type).toEqual('forward');
|
||||
expect(route.action.target.host).toEqual('localhost');
|
||||
expect(route.action.target.port).toEqual(443);
|
||||
expect(route.action.tls?.mode).toEqual('passthrough');
|
||||
});
|
||||
export default tap.start();
|
Reference in New Issue
Block a user