2022-10-05 17:08:17 +00:00
|
|
|
/// <reference path="../source/types.d.ts" />
|
|
|
|
|
|
|
|
import test from 'ava';
|
|
|
|
|
|
|
|
import {
|
2022-10-18 19:54:47 +00:00
|
|
|
matcherTypes,
|
2022-10-19 11:31:53 +00:00
|
|
|
narrowMatcherType,
|
2022-10-18 19:54:47 +00:00
|
|
|
narrowRedirectType,
|
2022-10-05 17:08:17 +00:00
|
|
|
parseRedirect,
|
2022-10-18 19:54:47 +00:00
|
|
|
redirectTypes,
|
2022-10-05 17:08:17 +00:00
|
|
|
HostnameRedirect,
|
|
|
|
Redirect,
|
2022-10-12 22:05:10 +00:00
|
|
|
Redirects,
|
2022-10-05 17:08:17 +00:00
|
|
|
RedirectParameters,
|
2022-11-02 10:10:29 +00:00
|
|
|
RegexRedirect,
|
2022-10-18 17:02:02 +00:00
|
|
|
SimpleRedirect,
|
2022-10-05 17:08:17 +00:00
|
|
|
} from '../source/redirect/exports.js';
|
|
|
|
|
2022-10-20 14:22:57 +00:00
|
|
|
const hostnameParameters: RedirectParameters = {
|
2022-10-26 12:55:04 +00:00
|
|
|
enabled: true,
|
2022-10-27 19:13:02 +00:00
|
|
|
id: 1,
|
2022-10-18 19:43:50 +00:00
|
|
|
matcherType: 'hostname',
|
2022-10-20 14:22:57 +00:00
|
|
|
matcherValue: 'example.com',
|
2022-10-18 20:21:55 +00:00
|
|
|
redirectType: 'hostname',
|
2022-10-20 14:22:57 +00:00
|
|
|
redirectValue: 'example.org',
|
2022-10-12 22:23:32 +00:00
|
|
|
};
|
|
|
|
|
2022-10-20 14:22:57 +00:00
|
|
|
const simpleParameters: RedirectParameters = {
|
2022-10-26 12:55:04 +00:00
|
|
|
enabled: true,
|
2022-10-27 19:13:02 +00:00
|
|
|
id: 2,
|
2022-10-18 19:43:50 +00:00
|
|
|
matcherType: 'hostname',
|
2022-10-20 14:22:57 +00:00
|
|
|
matcherValue: 'example.com',
|
2022-10-18 20:21:55 +00:00
|
|
|
redirectType: 'simple',
|
2022-10-20 14:22:57 +00:00
|
|
|
redirectValue: 'https://example.org/simple',
|
2022-10-18 17:02:02 +00:00
|
|
|
};
|
|
|
|
|
2022-11-02 10:10:29 +00:00
|
|
|
const regexParameters: RedirectParameters = {
|
|
|
|
enabled: true,
|
|
|
|
id: 3,
|
|
|
|
matcherType: 'regex',
|
|
|
|
matcherValue: '(.+)\\.com',
|
|
|
|
redirectType: 'regex',
|
|
|
|
redirectValue: '$1.org',
|
|
|
|
};
|
|
|
|
|
2022-10-05 17:08:17 +00:00
|
|
|
test('parseRedirect', (t) => {
|
2022-10-20 14:22:57 +00:00
|
|
|
const samples: RedirectParameters[] = [
|
2022-10-05 17:08:17 +00:00
|
|
|
{
|
|
|
|
test: 'Invalid parameters',
|
2022-10-20 14:22:57 +00:00
|
|
|
} as unknown as RedirectParameters,
|
|
|
|
undefined as unknown as RedirectParameters,
|
2022-10-12 22:23:32 +00:00
|
|
|
hostnameParameters,
|
2022-10-18 17:02:02 +00:00
|
|
|
simpleParameters,
|
2022-11-02 10:10:29 +00:00
|
|
|
regexParameters,
|
2022-10-05 17:08:17 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
for (const sample of samples) {
|
2022-10-27 19:13:02 +00:00
|
|
|
const redirect = parseRedirect(sample);
|
2022-10-05 17:08:17 +00:00
|
|
|
|
|
|
|
if (redirect === undefined) {
|
|
|
|
t.pass('parseRedirect returned undefined');
|
|
|
|
} else {
|
2022-10-27 19:13:02 +00:00
|
|
|
t.regex(redirect?.idString(), /^redirect:\d+$/i);
|
2022-10-05 17:08:17 +00:00
|
|
|
t.snapshot(redirect, `Class ${redirect.constructor.name}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Redirect.redirect', (t) => {
|
2022-10-12 22:23:32 +00:00
|
|
|
const hostnameRedirect = new HostnameRedirect(hostnameParameters);
|
2022-10-18 17:02:02 +00:00
|
|
|
const simpleRedirect = new SimpleRedirect(simpleParameters);
|
2022-11-02 10:10:29 +00:00
|
|
|
const regexRedirect = new RegexRedirect(regexParameters);
|
2022-10-05 17:08:17 +00:00
|
|
|
|
2022-11-02 10:10:29 +00:00
|
|
|
const samples: Array<[string | URL, Redirect]> = [
|
2022-10-05 17:08:17 +00:00
|
|
|
['https://example.com', hostnameRedirect],
|
|
|
|
['https://example.com/path#hash?query=test', hostnameRedirect],
|
2022-10-18 17:02:02 +00:00
|
|
|
['https://example.com', simpleRedirect],
|
|
|
|
['https://example.com/path', simpleRedirect],
|
2022-11-02 10:10:29 +00:00
|
|
|
['https://example.com', regexRedirect],
|
|
|
|
[new URL('https://example.com'), regexRedirect],
|
2022-10-05 17:08:17 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
for (const [index, [url, redirect]] of samples.entries()) {
|
|
|
|
t.snapshot(
|
|
|
|
{
|
2022-11-02 10:10:29 +00:00
|
|
|
original: url instanceof URL ? url.href : url,
|
2022-11-21 11:43:56 +00:00
|
|
|
redirected: new URL(redirect.redirect(url)).href,
|
2022-10-05 17:08:17 +00:00
|
|
|
},
|
|
|
|
`${index} ${redirect.constructor.name}`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
2022-10-12 22:21:46 +00:00
|
|
|
|
|
|
|
test('Redirect.isMatch', (t) => {
|
|
|
|
type UrlSamples = Array<[string, boolean]>;
|
|
|
|
|
2022-10-12 22:23:32 +00:00
|
|
|
const hostnameRedirect = new HostnameRedirect(hostnameParameters);
|
2022-10-12 22:21:46 +00:00
|
|
|
const hostnameSamples: UrlSamples = [
|
|
|
|
['https://example.com', true],
|
2022-10-18 20:24:59 +00:00
|
|
|
['https://www.example.com', true],
|
2022-10-12 22:21:46 +00:00
|
|
|
['https://example.org', false],
|
|
|
|
];
|
|
|
|
|
|
|
|
const invalidRedirect = new HostnameRedirect({
|
|
|
|
test: 'invalid',
|
2022-10-18 16:57:15 +00:00
|
|
|
} as unknown as HostnameRedirect['parameters']);
|
2022-10-12 22:21:46 +00:00
|
|
|
|
2022-10-20 22:14:58 +00:00
|
|
|
const regexMatch = new HostnameRedirect({
|
2022-10-26 12:55:04 +00:00
|
|
|
enabled: true,
|
2022-10-27 19:13:02 +00:00
|
|
|
id: 3,
|
2022-10-20 22:14:58 +00:00
|
|
|
matcherType: 'regex',
|
|
|
|
matcherValue: String.raw`^https://(www\.)?example.org/$`,
|
|
|
|
redirectType: 'simple',
|
|
|
|
redirectValue: '',
|
|
|
|
});
|
|
|
|
const regexSamples: UrlSamples = [
|
|
|
|
['https://example.org', true],
|
|
|
|
['https://www.example.org', true],
|
|
|
|
['https://example.org/path', false],
|
|
|
|
];
|
|
|
|
|
2022-10-12 22:21:46 +00:00
|
|
|
const samples: Array<[Redirects, UrlSamples]> = [
|
|
|
|
[invalidRedirect, [['https://example.org', false]]],
|
|
|
|
[hostnameRedirect, hostnameSamples],
|
2022-10-20 22:14:58 +00:00
|
|
|
[regexMatch, regexSamples],
|
2022-10-12 22:21:46 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
for (const [redirect, urlSamples] of samples) {
|
|
|
|
for (const [sample, expected] of urlSamples) {
|
2022-10-20 22:14:58 +00:00
|
|
|
t.is(redirect.isMatch(new URL(sample)), expected, `Sample ${sample}`);
|
2022-10-12 22:21:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2022-10-18 19:54:47 +00:00
|
|
|
|
|
|
|
test('Narrow match & redirect types', (t) => {
|
2022-10-19 11:31:53 +00:00
|
|
|
t.false(narrowMatcherType('invalid'));
|
2022-10-18 19:54:47 +00:00
|
|
|
t.false(narrowRedirectType('invalid'));
|
2022-10-19 11:31:53 +00:00
|
|
|
t.true(matcherTypes.every((value) => narrowMatcherType(value)));
|
2022-10-18 20:21:55 +00:00
|
|
|
t.true(redirectTypes.every((value) => narrowRedirectType(value)));
|
2022-10-18 19:54:47 +00:00
|
|
|
});
|