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 { getCertificate } 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).to.equal(8080);
|
|
expect(httpRoute.match.domains).to.equal('example.com');
|
|
expect(httpRoute.action.type).to.equal('forward');
|
|
expect(httpRoute.action.target?.host).to.equal('localhost');
|
|
expect(httpRoute.action.target?.port).to.equal(3000);
|
|
expect(httpRoute.name).to.equal('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).to.equal(443); // Default HTTPS port
|
|
expect(httpsRoute.match.domains).to.equal('secure.example.com');
|
|
expect(httpsRoute.action.type).to.equal('forward');
|
|
expect(httpsRoute.action.tls?.mode).to.equal('terminate');
|
|
expect(httpsRoute.action.tls?.certificate).to.equal('auto');
|
|
expect(httpsRoute.action.target?.host).to.equal('localhost');
|
|
expect(httpsRoute.action.target?.port).to.equal(8080);
|
|
expect(httpsRoute.name).to.equal('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).to.equal(80);
|
|
expect(redirectRoute.match.domains).to.equal('example.com');
|
|
expect(redirectRoute.action.type).to.equal('redirect');
|
|
expect(redirectRoute.action.redirect?.to).to.equal('https://{domain}{path}');
|
|
expect(redirectRoute.action.redirect?.status).to.equal(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).to.equal(2);
|
|
|
|
// Validate HTTPS route
|
|
const httpsRoute = routes[0];
|
|
expect(httpsRoute.match.ports).to.equal(443);
|
|
expect(httpsRoute.match.domains).to.equal('example.com');
|
|
expect(httpsRoute.action.type).to.equal('forward');
|
|
expect(httpsRoute.action.tls?.mode).to.equal('terminate');
|
|
|
|
// Validate HTTP redirect route
|
|
const redirectRoute = routes[1];
|
|
expect(redirectRoute.match.ports).to.equal(80);
|
|
expect(redirectRoute.action.type).to.equal('redirect');
|
|
expect(redirectRoute.action.redirect?.to).to.equal('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).to.equal('app.example.com');
|
|
expect(lbRoute.action.type).to.equal('forward');
|
|
expect(Array.isArray(lbRoute.action.target?.host)).to.equal(true);
|
|
expect((lbRoute.action.target?.host as string[]).length).to.equal(3);
|
|
expect((lbRoute.action.target?.host as string[])[0]).to.equal('10.0.0.1');
|
|
expect(lbRoute.action.target?.port).to.equal(8080);
|
|
expect(lbRoute.action.tls?.mode).to.equal('terminate');
|
|
});
|
|
|
|
tap.test('SmartProxy: Should create instance with route-based config', async () => {
|
|
// Create TLS certificates for testing
|
|
const cert = await getCertificate();
|
|
|
|
// 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: cert.key,
|
|
cert: cert.cert
|
|
},
|
|
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(proxy).to.be.an('object');
|
|
expect(proxy.start).to.be.a('function');
|
|
expect(proxy.stop).to.be.a('function');
|
|
});
|
|
|
|
export default tap.start(); |