platformservice/test/test.dcrouter.ts

119 lines
3.5 KiB
TypeScript

import { tap, expect } from '@push.rocks/tapbundle';
import * as plugins from '../ts/plugins.js';
import {
DcRouter,
type IDcRouterOptions,
type ISmtpForwardingConfig,
type IDomainRoutingConfig
} from '../ts/dcrouter/index.js';
tap.test('DcRouter class - basic functionality', async () => {
// Create a simple DcRouter instance
const options: IDcRouterOptions = {
tls: {
contactEmail: 'test@example.com'
}
};
const router = new DcRouter(options);
expect(router).toBeTruthy();
expect(router instanceof DcRouter).toEqual(true);
expect(router.options.tls.contactEmail).toEqual('test@example.com');
});
tap.test('DcRouter class - HTTP routing configuration', async () => {
// Create HTTP routing configuration
const httpRoutes: IDomainRoutingConfig[] = [
{
domain: 'example.com',
targetServer: '192.168.1.10',
targetPort: 8080,
useTls: true
},
{
domain: '*.example.org',
targetServer: '192.168.1.20',
targetPort: 9000,
useTls: false
}
];
const options: IDcRouterOptions = {
httpDomainRoutes: httpRoutes,
tls: {
contactEmail: 'test@example.com'
}
};
const router = new DcRouter(options);
expect(router.options.httpDomainRoutes.length).toEqual(2);
expect(router.options.httpDomainRoutes[0].domain).toEqual('example.com');
expect(router.options.httpDomainRoutes[1].domain).toEqual('*.example.org');
});
tap.test('DcRouter class - SMTP forwarding configuration', async () => {
// Create SMTP forwarding configuration
const smtpForwarding: ISmtpForwardingConfig = {
enabled: true,
ports: [25, 587, 465],
defaultServer: 'mail.example.com',
defaultPort: 25,
useTls: true,
preserveSourceIp: true,
domainRoutes: [
{
domain: 'example.com',
server: 'mail1.example.com',
port: 25
},
{
domain: 'example.org',
server: 'mail2.example.org',
port: 587
}
]
};
const options: IDcRouterOptions = {
smtpForwarding,
tls: {
contactEmail: 'test@example.com'
}
};
const router = new DcRouter(options);
expect(router.options.smtpForwarding.enabled).toEqual(true);
expect(router.options.smtpForwarding.ports.length).toEqual(3);
expect(router.options.smtpForwarding.domainRoutes.length).toEqual(2);
expect(router.options.smtpForwarding.domainRoutes[0].domain).toEqual('example.com');
});
tap.test('DcRouter class - Domain pattern matching', async () => {
const router = new DcRouter({});
// Use the internal method for testing if accessible
// This requires knowledge of the implementation, so it's a bit brittle
if (typeof router['isDomainMatch'] === 'function') {
// Test exact match
expect(router['isDomainMatch']('example.com', 'example.com')).toEqual(true);
expect(router['isDomainMatch']('example.com', 'example.org')).toEqual(false);
// Test wildcard match
expect(router['isDomainMatch']('sub.example.com', '*.example.com')).toEqual(true);
expect(router['isDomainMatch']('sub.sub.example.com', '*.example.com')).toEqual(true);
expect(router['isDomainMatch']('example.com', '*.example.com')).toEqual(false);
expect(router['isDomainMatch']('sub.example.org', '*.example.com')).toEqual(false);
}
});
// Final clean-up test
tap.test('clean up after tests', async () => {
// No-op - just to make sure everything is cleaned up properly
});
tap.test('stop', async () => {
await tap.stopForcefully();
});
// Export a function to run all tests
export default tap.start();