fix tests

This commit is contained in:
Juergen Kunz
2025-06-06 08:23:37 +00:00
parent b9be6533ae
commit 5f175b4ca8
5 changed files with 25 additions and 344 deletions

View File

@ -50,13 +50,13 @@ tap.test('setup http router test environment', async () => {
router = new HttpRouter();
// Initialize with empty config
router.updateRoutes([]);
router.setRoutes([]);
});
// Test basic routing by hostname
tap.test('should route requests by hostname', async () => {
const config = createRouteConfig(TEST_DOMAIN);
router.updateRoutes([config]);
router.setRoutes([config]);
const req = createMockRequest(TEST_DOMAIN);
const result = router.routeReq(req);
@ -68,7 +68,7 @@ tap.test('should route requests by hostname', async () => {
// Test handling of hostname with port number
tap.test('should handle hostname with port number', async () => {
const config = createRouteConfig(TEST_DOMAIN);
router.updateRoutes([config]);
router.setRoutes([config]);
const req = createMockRequest(`${TEST_DOMAIN}:443`);
const result = router.routeReq(req);
@ -80,7 +80,7 @@ tap.test('should handle hostname with port number', async () => {
// Test case-insensitive hostname matching
tap.test('should perform case-insensitive hostname matching', async () => {
const config = createRouteConfig(TEST_DOMAIN.toLowerCase());
router.updateRoutes([config]);
router.setRoutes([config]);
const req = createMockRequest(TEST_DOMAIN.toUpperCase());
const result = router.routeReq(req);
@ -92,7 +92,7 @@ tap.test('should perform case-insensitive hostname matching', async () => {
// Test handling of unmatched hostnames
tap.test('should return undefined for unmatched hostnames', async () => {
const config = createRouteConfig(TEST_DOMAIN);
router.updateRoutes([config]);
router.setRoutes([config]);
const req = createMockRequest('unknown.domain.com');
const result = router.routeReq(req);
@ -104,7 +104,7 @@ tap.test('should return undefined for unmatched hostnames', async () => {
tap.test('should match requests using path patterns', async () => {
const config = createRouteConfig(TEST_DOMAIN);
config.match.path = '/api/users';
router.updateRoutes([config]);
router.setRoutes([config]);
// Test that path matches
const req1 = createMockRequest(TEST_DOMAIN, '/api/users');
@ -125,7 +125,7 @@ tap.test('should match requests using path patterns', async () => {
tap.test('should support wildcard path patterns', async () => {
const config = createRouteConfig(TEST_DOMAIN);
config.match.path = '/api/*';
router.updateRoutes([config]);
router.setRoutes([config]);
// Test with path that matches the wildcard pattern
const req = createMockRequest(TEST_DOMAIN, '/api/users/123');
@ -145,7 +145,7 @@ tap.test('should support wildcard path patterns', async () => {
tap.test('should extract path parameters from URL', async () => {
const config = createRouteConfig(TEST_DOMAIN);
config.match.path = '/users/:id/profile';
router.updateRoutes([config]);
router.setRoutes([config]);
const req = createMockRequest(TEST_DOMAIN, '/users/123/profile');
const result = router.routeReqWithDetails(req);
@ -167,7 +167,7 @@ tap.test('should support multiple configs for same hostname with different paths
webConfig.name = 'web-route';
// Add both configs
router.updateRoutes([apiConfig, webConfig]);
router.setRoutes([apiConfig, webConfig]);
// Test API path routes to API config
const apiReq = createMockRequest(TEST_DOMAIN, '/api/users');
@ -191,7 +191,7 @@ tap.test('should support multiple configs for same hostname with different paths
// Test wildcard subdomains
tap.test('should match wildcard subdomains', async () => {
const wildcardConfig = createRouteConfig(TEST_WILDCARD);
router.updateRoutes([wildcardConfig]);
router.setRoutes([wildcardConfig]);
// Test that subdomain.example.com matches *.example.com
const req = createMockRequest('subdomain.example.com');
@ -204,7 +204,7 @@ tap.test('should match wildcard subdomains', async () => {
// Test TLD wildcards (example.*)
tap.test('should match TLD wildcards', async () => {
const tldWildcardConfig = createRouteConfig('example.*');
router.updateRoutes([tldWildcardConfig]);
router.setRoutes([tldWildcardConfig]);
// Test that example.com matches example.*
const req1 = createMockRequest('example.com');
@ -227,7 +227,7 @@ tap.test('should match TLD wildcards', async () => {
// Test complex pattern matching (*.lossless*)
tap.test('should match complex wildcard patterns', async () => {
const complexWildcardConfig = createRouteConfig('*.lossless*');
router.updateRoutes([complexWildcardConfig]);
router.setRoutes([complexWildcardConfig]);
// Test that sub.lossless.com matches *.lossless*
const req1 = createMockRequest('sub.lossless.com');
@ -252,7 +252,7 @@ tap.test('should fall back to default configuration', async () => {
const defaultConfig = createRouteConfig('*');
const specificConfig = createRouteConfig(TEST_DOMAIN);
router.updateRoutes([defaultConfig, specificConfig]);
router.setRoutes([defaultConfig, specificConfig]);
// Test specific domain routes to specific config
const specificReq = createMockRequest(TEST_DOMAIN);
@ -272,7 +272,7 @@ tap.test('should prioritize exact hostname over wildcard', async () => {
const wildcardConfig = createRouteConfig(TEST_WILDCARD);
const exactConfig = createRouteConfig(TEST_SUBDOMAIN);
router.updateRoutes([wildcardConfig, exactConfig]);
router.setRoutes([wildcardConfig, exactConfig]);
// Test that exact match takes priority
const req = createMockRequest(TEST_SUBDOMAIN);
@ -283,11 +283,11 @@ tap.test('should prioritize exact hostname over wildcard', async () => {
// Test adding and removing configurations
tap.test('should manage configurations correctly', async () => {
router.updateRoutes([]);
router.setRoutes([]);
// Add a config
const config = createRouteConfig(TEST_DOMAIN);
router.updateRoutes([config]);
router.setRoutes([config]);
// Verify routing works
const req = createMockRequest(TEST_DOMAIN);
@ -296,7 +296,7 @@ tap.test('should manage configurations correctly', async () => {
expect(result).toEqual(config);
// Remove the config and verify it no longer routes
router.updateRoutes([]);
router.setRoutes([]);
result = router.routeReq(req);
expect(result).toBeUndefined();
@ -313,7 +313,7 @@ tap.test('should prioritize more specific path patterns', async () => {
specificConfig.name = 'specific-api';
specificConfig.priority = 10; // Higher priority
router.updateRoutes([genericConfig, specificConfig]);
router.setRoutes([genericConfig, specificConfig]);
// The more specific '/api/users' should match before the '/api/*' wildcard
const req = createMockRequest(TEST_DOMAIN, '/api/users');
@ -328,7 +328,7 @@ tap.test('should handle multiple configured hostnames', async () => {
createRouteConfig(TEST_DOMAIN),
createRouteConfig(TEST_SUBDOMAIN)
];
router.updateRoutes(routes);
router.setRoutes(routes);
// Test first domain routes correctly
const req1 = createMockRequest(TEST_DOMAIN);
@ -344,7 +344,7 @@ tap.test('should handle multiple configured hostnames', async () => {
// Test handling missing host header
tap.test('should handle missing host header', async () => {
const defaultConfig = createRouteConfig('*');
router.updateRoutes([defaultConfig]);
router.setRoutes([defaultConfig]);
const req = createMockRequest('');
req.headers.host = undefined;
@ -358,7 +358,7 @@ tap.test('should handle missing host header', async () => {
tap.test('should handle complex path parameters', async () => {
const config = createRouteConfig(TEST_DOMAIN);
config.match.path = '/api/:version/users/:userId/posts/:postId';
router.updateRoutes([config]);
router.setRoutes([config]);
const req = createMockRequest(TEST_DOMAIN, '/api/v1/users/123/posts/456');
const result = router.routeReqWithDetails(req);
@ -380,7 +380,7 @@ tap.test('should handle many configurations efficiently', async () => {
configs.push(createRouteConfig(`host-${i}.example.com`));
}
router.updateRoutes(configs);
router.setRoutes(configs);
// Test middle of the list to avoid best/worst case
const req = createMockRequest('host-50.example.com');
@ -392,7 +392,7 @@ tap.test('should handle many configurations efficiently', async () => {
// Test cleanup
tap.test('cleanup proxy router test environment', async () => {
// Clear all configurations
router.updateRoutes([]);
router.setRoutes([]);
// Verify empty state by testing that no routes match
const req = createMockRequest(TEST_DOMAIN);