Files
gitbook/src/lib/middleware.test.ts
T
Johan Preynat e462ac837e Redirect URLs containing uppercase characters to lowercase (#266)
* Add tests for normalizeURL

* Redirect requests containing uppercase in pathname to lowercase via normalizeURL

* Fix typo
2024-03-15 11:24:41 +01:00

132 lines
4.2 KiB
TypeScript

import { describe, it, expect } from 'bun:test';
import { getURLLookupAlternatives, normalizeURL } from './middleware';
describe('getURLLookupAlternatives', () => {
it('should return all URLs up to the root', () => {
expect(getURLLookupAlternatives(new URL('https://docs.mycompany.com/a/b/c'))).toEqual([
{
extraPath: 'a/b/c',
url: 'https://docs.mycompany.com/',
primary: false,
},
{
extraPath: 'b/c',
url: 'https://docs.mycompany.com/a',
primary: false,
},
{
extraPath: '',
url: 'https://docs.mycompany.com/a/b/c',
primary: true,
},
]);
});
it('should not match before the variant for a variant url', () => {
expect(
getURLLookupAlternatives(new URL('https://test.gitbook.io/v/variant/space')),
).toEqual([
{
url: 'https://test.gitbook.io/v/variant',
extraPath: 'space',
primary: false,
},
{
url: 'https://test.gitbook.io/v/variant/space',
extraPath: '',
primary: true,
},
]);
});
it('should not match before a revision ID', () => {
expect(
getURLLookupAlternatives(new URL('https://docs.mycompany.com/~/revisions/id/hello')),
).toEqual([
{
extraPath: 'hello',
url: 'https://docs.mycompany.com/~/revisions/id',
primary: false,
},
{
extraPath: '',
url: 'https://docs.mycompany.com/~/revisions/id/hello',
primary: true,
},
]);
});
it('should not match before a revision ID', () => {
expect(
getURLLookupAlternatives(new URL('https://docs.mycompany.com/~/changes/id/hello')),
).toEqual([
{
extraPath: 'hello',
url: 'https://docs.mycompany.com/~/changes/id',
primary: false,
},
{
extraPath: '',
url: 'https://docs.mycompany.com/~/changes/id/hello',
primary: true,
},
]);
});
it('should normalize duplicated slashes', () => {
expect(getURLLookupAlternatives(new URL('https://docs.mycompany.com//hello'))).toEqual([
{
extraPath: 'hello',
url: 'https://docs.mycompany.com/',
primary: false,
},
{
extraPath: '',
url: 'https://docs.mycompany.com/hello',
primary: true,
},
]);
});
it('should normalize trailing slash', () => {
expect(getURLLookupAlternatives(new URL('https://docs.mycompany.com/hello/'))).toEqual([
{
extraPath: 'hello',
url: 'https://docs.mycompany.com/',
primary: false,
},
{
extraPath: '',
url: 'https://docs.mycompany.com/hello',
primary: true,
},
]);
});
});
describe('normalizeURL', () => {
it('should remove trailing slashes', () => {
expect(normalizeURL(new URL('https://docs.mycompany.com/hello/'))).toEqual(
new URL('https://docs.mycompany.com/hello'),
);
});
it('should remove duplicate slashes', () => {
expect(normalizeURL(new URL('https://docs.mycompany.com//hello//there'))).toEqual(
new URL('https://docs.mycompany.com/hello/there'),
);
});
it('should convert uppercase characters in the path to lowercase', () => {
expect(normalizeURL(new URL('https://docs.mycompany.com/Hello/My/pAge'))).toEqual(
new URL('https://docs.mycompany.com/hello/my/page'),
);
});
it('should not affect uppercase characters in querystring parameters', () => {
expect(normalizeURL(new URL('https://docs.mycompany.com/Hello/My/pAge?Q=MySearch'))).toEqual(
new URL('https://docs.mycompany.com/hello/my/page?Q=MySearch'),
);
});
})