79 lines
3.3 KiB
TypeScript
79 lines
3.3 KiB
TypeScript
|
import { tap, expect } from '@git.zone/tstest/tapbundle';
|
||
|
import { DomainMatcher } from '../../../ts/core/routing/matchers/domain.js';
|
||
|
|
||
|
tap.test('DomainMatcher - exact match', async () => {
|
||
|
expect(DomainMatcher.match('example.com', 'example.com')).toEqual(true);
|
||
|
expect(DomainMatcher.match('example.com', 'example.net')).toEqual(false);
|
||
|
expect(DomainMatcher.match('sub.example.com', 'example.com')).toEqual(false);
|
||
|
});
|
||
|
|
||
|
tap.test('DomainMatcher - case insensitive', async () => {
|
||
|
expect(DomainMatcher.match('Example.COM', 'example.com')).toEqual(true);
|
||
|
expect(DomainMatcher.match('example.com', 'EXAMPLE.COM')).toEqual(true);
|
||
|
expect(DomainMatcher.match('ExAmPlE.cOm', 'eXaMpLe.CoM')).toEqual(true);
|
||
|
});
|
||
|
|
||
|
tap.test('DomainMatcher - wildcard matching', async () => {
|
||
|
// Leading wildcard
|
||
|
expect(DomainMatcher.match('*.example.com', 'sub.example.com')).toEqual(true);
|
||
|
expect(DomainMatcher.match('*.example.com', 'deep.sub.example.com')).toEqual(true);
|
||
|
expect(DomainMatcher.match('*.example.com', 'example.com')).toEqual(false);
|
||
|
|
||
|
// Multiple wildcards
|
||
|
expect(DomainMatcher.match('*.*.example.com', 'a.b.example.com')).toEqual(true);
|
||
|
expect(DomainMatcher.match('api.*.example.com', 'api.v1.example.com')).toEqual(true);
|
||
|
|
||
|
// Trailing wildcard
|
||
|
expect(DomainMatcher.match('example.*', 'example.com')).toEqual(true);
|
||
|
expect(DomainMatcher.match('example.*', 'example.net')).toEqual(true);
|
||
|
expect(DomainMatcher.match('example.*', 'example.co.uk')).toEqual(true);
|
||
|
});
|
||
|
|
||
|
tap.test('DomainMatcher - FQDN normalization', async () => {
|
||
|
expect(DomainMatcher.match('example.com.', 'example.com')).toEqual(true);
|
||
|
expect(DomainMatcher.match('example.com', 'example.com.')).toEqual(true);
|
||
|
expect(DomainMatcher.match('example.com.', 'example.com.')).toEqual(true);
|
||
|
});
|
||
|
|
||
|
tap.test('DomainMatcher - edge cases', async () => {
|
||
|
expect(DomainMatcher.match('', 'example.com')).toEqual(false);
|
||
|
expect(DomainMatcher.match('example.com', '')).toEqual(false);
|
||
|
expect(DomainMatcher.match('', '')).toEqual(false);
|
||
|
expect(DomainMatcher.match(null as any, 'example.com')).toEqual(false);
|
||
|
expect(DomainMatcher.match('example.com', null as any)).toEqual(false);
|
||
|
});
|
||
|
|
||
|
tap.test('DomainMatcher - specificity calculation', async () => {
|
||
|
// Exact domains are most specific
|
||
|
const exactScore = DomainMatcher.calculateSpecificity('api.example.com');
|
||
|
const wildcardScore = DomainMatcher.calculateSpecificity('*.example.com');
|
||
|
const leadingWildcardScore = DomainMatcher.calculateSpecificity('*.com');
|
||
|
|
||
|
expect(exactScore).toBeGreaterThan(wildcardScore);
|
||
|
expect(wildcardScore).toBeGreaterThan(leadingWildcardScore);
|
||
|
|
||
|
// More segments = more specific
|
||
|
const threeSegments = DomainMatcher.calculateSpecificity('api.v1.example.com');
|
||
|
const twoSegments = DomainMatcher.calculateSpecificity('example.com');
|
||
|
expect(threeSegments).toBeGreaterThan(twoSegments);
|
||
|
});
|
||
|
|
||
|
tap.test('DomainMatcher - findAllMatches', async () => {
|
||
|
const patterns = [
|
||
|
'example.com',
|
||
|
'*.example.com',
|
||
|
'api.example.com',
|
||
|
'*.api.example.com',
|
||
|
'*'
|
||
|
];
|
||
|
|
||
|
const matches = DomainMatcher.findAllMatches(patterns, 'v1.api.example.com');
|
||
|
|
||
|
// Should match: *.example.com, *.api.example.com, *
|
||
|
expect(matches).toHaveLength(3);
|
||
|
expect(matches[0]).toEqual('*.api.example.com'); // Most specific
|
||
|
expect(matches[1]).toEqual('*.example.com');
|
||
|
expect(matches[2]).toEqual('*'); // Least specific
|
||
|
});
|
||
|
|
||
|
tap.start();
|