121 lines
4.3 KiB
TypeScript
121 lines
4.3 KiB
TypeScript
import { tap, expect } from '@git.zone/tstest/tapbundle';
|
|
|
|
import {
|
|
mergeRouteConfigs,
|
|
cloneRoute,
|
|
routeMatchesPath,
|
|
} from '../ts/proxies/smart-proxy/utils/route-utils.js';
|
|
|
|
import {
|
|
validateRoutes,
|
|
validateRouteConfig,
|
|
} from '../ts/proxies/smart-proxy/utils/route-validator.js';
|
|
|
|
import type { IRouteConfig } from '../ts/proxies/smart-proxy/models/route-types.js';
|
|
|
|
tap.test('route creation - HTTPS terminate route has correct structure', async () => {
|
|
const route: IRouteConfig = {
|
|
match: { ports: 443, domains: 'secure.example.com' },
|
|
action: { type: 'forward', targets: [{ host: '127.0.0.1', port: 8443 }], tls: { mode: 'terminate', certificate: 'auto' } }
|
|
};
|
|
expect(route).toHaveProperty('match');
|
|
expect(route).toHaveProperty('action');
|
|
expect(route.action.type).toEqual('forward');
|
|
expect(route.action.tls).toBeDefined();
|
|
expect(route.action.tls!.mode).toEqual('terminate');
|
|
expect(route.match.domains).toEqual('secure.example.com');
|
|
});
|
|
|
|
tap.test('route validation - validateRoutes on a set of routes', async () => {
|
|
const routes: IRouteConfig[] = [
|
|
{ match: { ports: 80, domains: 'a.com' }, action: { type: 'forward', targets: [{ host: '127.0.0.1', port: 3000 }] } },
|
|
{ match: { ports: 80, domains: 'b.com' }, action: { type: 'forward', targets: [{ host: '127.0.0.1', port: 4000 }] } },
|
|
];
|
|
const result = validateRoutes(routes);
|
|
expect(result.valid).toBeTrue();
|
|
expect(result.errors).toHaveLength(0);
|
|
});
|
|
|
|
tap.test('route validation - validateRoutes catches invalid route in set', async () => {
|
|
const routes: any[] = [
|
|
{ match: { ports: 80, domains: 'valid.com' }, action: { type: 'forward', targets: [{ host: '127.0.0.1', port: 3000 }] } },
|
|
{ match: { ports: 80 } }, // missing action
|
|
];
|
|
const result = validateRoutes(routes);
|
|
expect(result.valid).toBeFalse();
|
|
expect(result.errors.length).toBeGreaterThan(0);
|
|
});
|
|
|
|
tap.test('path matching - routeMatchesPath with exact path', async () => {
|
|
const route: IRouteConfig = {
|
|
match: { ports: 80, domains: 'example.com', path: '/api' },
|
|
action: { type: 'forward', targets: [{ host: '127.0.0.1', port: 3000 }] }
|
|
};
|
|
expect(routeMatchesPath(route, '/api')).toBeTrue();
|
|
expect(routeMatchesPath(route, '/other')).toBeFalse();
|
|
});
|
|
|
|
tap.test('path matching - route without path matches everything', async () => {
|
|
const route: IRouteConfig = {
|
|
match: { ports: 80, domains: 'example.com' },
|
|
action: { type: 'forward', targets: [{ host: '127.0.0.1', port: 3000 }] }
|
|
};
|
|
expect(routeMatchesPath(route, '/anything')).toBeTrue();
|
|
expect(routeMatchesPath(route, '/')).toBeTrue();
|
|
});
|
|
|
|
tap.test('route merging - mergeRouteConfigs combines routes', async () => {
|
|
const base: IRouteConfig = {
|
|
match: { ports: 80, domains: 'example.com' },
|
|
action: { type: 'forward', targets: [{ host: '127.0.0.1', port: 3000 }] },
|
|
priority: 10,
|
|
name: 'base-route'
|
|
};
|
|
|
|
const merged = mergeRouteConfigs(base, {
|
|
priority: 50,
|
|
name: 'merged-route',
|
|
});
|
|
|
|
expect(merged.priority).toEqual(50);
|
|
expect(merged.name).toEqual('merged-route');
|
|
expect(merged.match.domains).toEqual('example.com');
|
|
expect(merged.action.targets![0].host).toEqual('127.0.0.1');
|
|
});
|
|
|
|
tap.test('route merging - mergeRouteConfigs does not mutate original', async () => {
|
|
const base: IRouteConfig = {
|
|
match: { ports: 80, domains: 'example.com' },
|
|
action: { type: 'forward', targets: [{ host: '127.0.0.1', port: 3000 }] },
|
|
name: 'original'
|
|
};
|
|
|
|
const merged = mergeRouteConfigs(base, { name: 'changed' });
|
|
expect(base.name).toEqual('original');
|
|
expect(merged.name).toEqual('changed');
|
|
});
|
|
|
|
tap.test('route cloning - cloneRoute produces independent copy', async () => {
|
|
const original: IRouteConfig = {
|
|
match: { ports: 80, domains: 'example.com' },
|
|
action: { type: 'forward', targets: [{ host: '127.0.0.1', port: 3000 }] },
|
|
priority: 42,
|
|
name: 'original-route'
|
|
};
|
|
|
|
const cloned = cloneRoute(original);
|
|
|
|
expect(cloned.match.domains).toEqual('example.com');
|
|
expect(cloned.priority).toEqual(42);
|
|
expect(cloned.name).toEqual('original-route');
|
|
expect(cloned.action.targets![0].host).toEqual('127.0.0.1');
|
|
expect(cloned.action.targets![0].port).toEqual(3000);
|
|
|
|
cloned.name = 'cloned-route';
|
|
cloned.priority = 99;
|
|
expect(original.name).toEqual('original-route');
|
|
expect(original.priority).toEqual(42);
|
|
});
|
|
|
|
export default tap.start();
|