Compare commits

..

4 Commits

Author SHA1 Message Date
Bauke aa9a7121f8
Reduce duplication. 2022-10-13 00:23:32 +02:00
Bauke 814c718c41
Add Redirect.isMatch tests. 2022-10-13 00:21:46 +02:00
Bauke b9a56fe881
Update tests to include Matcher. 2022-10-13 00:05:10 +02:00
Bauke 5888dfdc97
Add Matcher to Redirects. 2022-10-13 00:04:51 +02:00
5 changed files with 55 additions and 13 deletions

View File

@ -2,8 +2,21 @@ import {HostnameParameters} from './hostname.js';
export type RedirectParameters = HostnameParameters; export type RedirectParameters = HostnameParameters;
export type Matcher = {
matchType: 'hostname';
toMatch: string;
};
export abstract class Redirect<P extends RedirectParameters> { export abstract class Redirect<P extends RedirectParameters> {
constructor(public parameters: P) {} constructor(public parameters: P & Matcher) {}
public isMatch(url: URL): boolean {
if (this.parameters.matchType === 'hostname') {
return url.hostname === this.parameters.toMatch;
}
return false;
}
public abstract redirect(url: URL | string): URL; public abstract redirect(url: URL | string): URL;
} }

View File

@ -1,4 +1,3 @@
import {RedirectParameters} from './base.js';
import {HostnameRedirect} from './hostname.js'; import {HostnameRedirect} from './hostname.js';
export * from './base.js'; export * from './base.js';
@ -6,7 +5,7 @@ export * from './hostname.js';
export type Redirects = HostnameRedirect; export type Redirects = HostnameRedirect;
export function parseRedirect<P extends RedirectParameters>( export function parseRedirect<P extends Redirects['parameters']>(
parameters: P, parameters: P,
): Redirects | undefined { ): Redirects | undefined {
if (parameters.type === 'hostname') { if (parameters.type === 'hostname') {

View File

@ -6,18 +6,23 @@ import {
parseRedirect, parseRedirect,
HostnameRedirect, HostnameRedirect,
Redirect, Redirect,
Redirects,
RedirectParameters, RedirectParameters,
} from '../source/redirect/exports.js'; } from '../source/redirect/exports.js';
const hostnameParameters: HostnameRedirect['parameters'] = {
hostname: 'example.org',
matchType: 'hostname',
toMatch: 'example.com',
type: 'hostname',
};
test('parseRedirect', (t) => { test('parseRedirect', (t) => {
const samples: RedirectParameters[] = [ const samples: Array<Redirects['parameters']> = [
{ {
test: 'Invalid parameters', test: 'Invalid parameters',
} as unknown as RedirectParameters, } as unknown as Redirects['parameters'],
{ hostnameParameters,
hostname: 'example.org',
type: 'hostname',
},
]; ];
for (const sample of samples) { for (const sample of samples) {
@ -32,10 +37,7 @@ test('parseRedirect', (t) => {
}); });
test('Redirect.redirect', (t) => { test('Redirect.redirect', (t) => {
const hostnameRedirect = new HostnameRedirect({ const hostnameRedirect = new HostnameRedirect(hostnameParameters);
hostname: 'example.org',
type: 'hostname',
});
const samples: Array<[string, Redirect<RedirectParameters>]> = [ const samples: Array<[string, Redirect<RedirectParameters>]> = [
['https://example.com', hostnameRedirect], ['https://example.com', hostnameRedirect],
@ -52,3 +54,29 @@ test('Redirect.redirect', (t) => {
); );
} }
}); });
test('Redirect.isMatch', (t) => {
type UrlSamples = Array<[string, boolean]>;
const hostnameRedirect = new HostnameRedirect(hostnameParameters);
const hostnameSamples: UrlSamples = [
['https://example.com', true],
['https://www.example.com', false],
['https://example.org', false],
];
const invalidRedirect = new HostnameRedirect({
test: 'invalid',
} as unknown as Redirects['parameters']);
const samples: Array<[Redirects, UrlSamples]> = [
[invalidRedirect, [['https://example.org', false]]],
[hostnameRedirect, hostnameSamples],
];
for (const [redirect, urlSamples] of samples) {
for (const [sample, expected] of urlSamples) {
t.is(redirect.isMatch(new URL(sample)), expected);
}
}
});

View File

@ -11,6 +11,8 @@ Generated by [AVA](https://avajs.dev).
HostnameRedirect { HostnameRedirect {
parameters: { parameters: {
hostname: 'example.org', hostname: 'example.org',
matchType: 'hostname',
toMatch: 'example.com',
type: 'hostname', type: 'hostname',
}, },
} }