181 lines
5.5 KiB
TypeScript
181 lines
5.5 KiB
TypeScript
/**
|
|
* Tests for the new route-based configuration system
|
|
*/
|
|
import { expect, tap } from '@push.rocks/tapbundle';
|
|
|
|
// Import from core modules
|
|
import {
|
|
SmartProxy,
|
|
createHttpRoute,
|
|
createHttpsRoute,
|
|
createPassthroughRoute,
|
|
createRedirectRoute,
|
|
createHttpToHttpsRedirect,
|
|
createHttpsServer,
|
|
createLoadBalancerRoute
|
|
} from '../ts/proxies/smart-proxy/index.js';
|
|
|
|
// Import test helpers
|
|
import { loadTestCertificates } from './helpers/certificates.js';
|
|
|
|
tap.test('Routes: Should create basic HTTP route', async () => {
|
|
// Create a simple HTTP route
|
|
const httpRoute = createHttpRoute({
|
|
ports: 8080,
|
|
domains: 'example.com',
|
|
target: {
|
|
host: 'localhost',
|
|
port: 3000
|
|
},
|
|
name: 'Basic HTTP Route'
|
|
});
|
|
|
|
// Validate the route configuration
|
|
expect(httpRoute.match.ports).toEqual(8080);
|
|
expect(httpRoute.match.domains).toEqual('example.com');
|
|
expect(httpRoute.action.type).toEqual('forward');
|
|
expect(httpRoute.action.target?.host).toEqual('localhost');
|
|
expect(httpRoute.action.target?.port).toEqual(3000);
|
|
expect(httpRoute.name).toEqual('Basic HTTP Route');
|
|
});
|
|
|
|
tap.test('Routes: Should create HTTPS route with TLS termination', async () => {
|
|
// Create an HTTPS route with TLS termination
|
|
const httpsRoute = createHttpsRoute({
|
|
domains: 'secure.example.com',
|
|
target: {
|
|
host: 'localhost',
|
|
port: 8080
|
|
},
|
|
certificate: 'auto',
|
|
name: 'HTTPS Route'
|
|
});
|
|
|
|
// Validate the route configuration
|
|
expect(httpsRoute.match.ports).toEqual(443); // Default HTTPS port
|
|
expect(httpsRoute.match.domains).toEqual('secure.example.com');
|
|
expect(httpsRoute.action.type).toEqual('forward');
|
|
expect(httpsRoute.action.tls?.mode).toEqual('terminate');
|
|
expect(httpsRoute.action.tls?.certificate).toEqual('auto');
|
|
expect(httpsRoute.action.target?.host).toEqual('localhost');
|
|
expect(httpsRoute.action.target?.port).toEqual(8080);
|
|
expect(httpsRoute.name).toEqual('HTTPS Route');
|
|
});
|
|
|
|
tap.test('Routes: Should create HTTP to HTTPS redirect', async () => {
|
|
// Create an HTTP to HTTPS redirect
|
|
const redirectRoute = createHttpToHttpsRedirect({
|
|
domains: 'example.com',
|
|
statusCode: 301
|
|
});
|
|
|
|
// Validate the route configuration
|
|
expect(redirectRoute.match.ports).toEqual(80);
|
|
expect(redirectRoute.match.domains).toEqual('example.com');
|
|
expect(redirectRoute.action.type).toEqual('redirect');
|
|
expect(redirectRoute.action.redirect?.to).toEqual('https://{domain}{path}');
|
|
expect(redirectRoute.action.redirect?.status).toEqual(301);
|
|
});
|
|
|
|
tap.test('Routes: Should create complete HTTPS server with redirects', async () => {
|
|
// Create a complete HTTPS server setup
|
|
const routes = createHttpsServer({
|
|
domains: 'example.com',
|
|
target: {
|
|
host: 'localhost',
|
|
port: 8080
|
|
},
|
|
certificate: 'auto',
|
|
addHttpRedirect: true
|
|
});
|
|
|
|
// Validate that we got two routes (HTTPS route and HTTP redirect)
|
|
expect(routes.length).toEqual(2);
|
|
|
|
// Validate HTTPS route
|
|
const httpsRoute = routes[0];
|
|
expect(httpsRoute.match.ports).toEqual(443);
|
|
expect(httpsRoute.match.domains).toEqual('example.com');
|
|
expect(httpsRoute.action.type).toEqual('forward');
|
|
expect(httpsRoute.action.tls?.mode).toEqual('terminate');
|
|
|
|
// Validate HTTP redirect route
|
|
const redirectRoute = routes[1];
|
|
expect(redirectRoute.match.ports).toEqual(80);
|
|
expect(redirectRoute.action.type).toEqual('redirect');
|
|
expect(redirectRoute.action.redirect?.to).toEqual('https://{domain}{path}');
|
|
});
|
|
|
|
tap.test('Routes: Should create load balancer route', async () => {
|
|
// Create a load balancer route
|
|
const lbRoute = createLoadBalancerRoute({
|
|
domains: 'app.example.com',
|
|
targets: ['10.0.0.1', '10.0.0.2', '10.0.0.3'],
|
|
targetPort: 8080,
|
|
tlsMode: 'terminate',
|
|
certificate: 'auto',
|
|
name: 'Load Balanced Route'
|
|
});
|
|
|
|
// Validate the route configuration
|
|
expect(lbRoute.match.domains).toEqual('app.example.com');
|
|
expect(lbRoute.action.type).toEqual('forward');
|
|
expect(Array.isArray(lbRoute.action.target?.host)).toBeTrue();
|
|
expect((lbRoute.action.target?.host as string[]).length).toEqual(3);
|
|
expect((lbRoute.action.target?.host as string[])[0]).toEqual('10.0.0.1');
|
|
expect(lbRoute.action.target?.port).toEqual(8080);
|
|
expect(lbRoute.action.tls?.mode).toEqual('terminate');
|
|
});
|
|
|
|
tap.test('SmartProxy: Should create instance with route-based config', async () => {
|
|
// Create TLS certificates for testing
|
|
const certs = loadTestCertificates();
|
|
|
|
// Create a SmartProxy instance with route-based configuration
|
|
const proxy = new SmartProxy({
|
|
routes: [
|
|
createHttpRoute({
|
|
ports: 8080,
|
|
domains: 'example.com',
|
|
target: {
|
|
host: 'localhost',
|
|
port: 3000
|
|
},
|
|
name: 'HTTP Route'
|
|
}),
|
|
createHttpsRoute({
|
|
domains: 'secure.example.com',
|
|
target: {
|
|
host: 'localhost',
|
|
port: 8443
|
|
},
|
|
certificate: {
|
|
key: certs.privateKey,
|
|
cert: certs.publicKey
|
|
},
|
|
name: 'HTTPS Route'
|
|
})
|
|
],
|
|
defaults: {
|
|
target: {
|
|
host: 'localhost',
|
|
port: 8080
|
|
},
|
|
security: {
|
|
allowedIPs: ['127.0.0.1', '192.168.0.*'],
|
|
maxConnections: 100
|
|
}
|
|
},
|
|
// Additional settings
|
|
initialDataTimeout: 10000,
|
|
inactivityTimeout: 300000,
|
|
enableDetailedLogging: true
|
|
});
|
|
|
|
// Simply verify the instance was created successfully
|
|
expect(typeof proxy).toEqual('object');
|
|
expect(typeof proxy.start).toEqual('function');
|
|
expect(typeof proxy.stop).toEqual('function');
|
|
});
|
|
|
|
export default tap.start(); |