Repository navigation
Expand file tree
/
Copy pathredirects.test.ts
More file actions
133 lines (122 loc) · 4.75 KB
/
Copy pathredirects.test.ts
File metadata and controls
133 lines (122 loc) · 4.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import {unstable_getResponseFromNextConfig} from 'next/experimental/testing/server';
import {describe, expect, it} from 'vitest';
import {userDocsRedirects, withMarkdownRedirects} from './redirects';
async function redirectLocation(
list: {destination: string; source: string}[],
path: string
) {
const response = await unstable_getResponseFromNextConfig({
url: `https://docs.sentry.io${path}`,
nextConfig: {
trailingSlash: true,
redirects: () =>
Promise.resolve(withMarkdownRedirects(list).map(r => ({...r, permanent: true}))),
},
});
return response.headers.get('location');
}
describe('withMarkdownRedirects', () => {
it('folds the .md URL into the same rule', () => {
const list = [{source: '/old/', destination: '/new/'}];
expect(withMarkdownRedirects(list)).toHaveLength(list.length);
});
it.each([
['/new/', '/old/', '/new/'],
['/new/', '/old.md', '/new.md'],
['/new/#section', '/old/', '/new/#section'],
['/new/#section', '/old.md', '/new.md#section'],
['https://mcp.sentry.dev', '/old.md', 'https://mcp.sentry.dev/'],
['/platform-redirect/?next=/x/', '/old.md', '/platform-redirect/?next=%2Fx%2F'],
])('redirects to %s: %s -> %s', async (destination, path, expected) => {
expect(await redirectLocation([{source: '/old/', destination}], path)).toBe(
new URL(expected, 'https://docs.sentry.io').href
);
});
it.each([
[
'/platforms/:platform/old/',
'/platforms/:platform/new/',
'/platforms/python/old',
'/platforms/python/new',
],
[
'/platforms/:platform/sourcemaps/:section(generating|validating)/',
'/platforms/:platform/sourcemaps/',
'/platforms/python/sourcemaps/validating',
'/platforms/python/sourcemaps',
],
])('folds named params from %s', async (source, destination, from, to) => {
const list = [{source, destination}];
expect(withMarkdownRedirects(list)).toHaveLength(1);
expect(await redirectLocation(list, `${from}.md`)).toBe(
`https://docs.sentry.io${to}.md`
);
expect(await redirectLocation(list, `${from}/`)).toBe(`https://docs.sentry.io${to}/`);
});
it('adds a separate .md rule when the destination ends in a param', async () => {
const list = [
{
source: '/platforms/dotnet/guides/:guide/compatibility/',
destination: '/platforms/dotnet/guides/:guide/',
},
];
expect(withMarkdownRedirects(list)).toHaveLength(2);
expect(
await redirectLocation(list, '/platforms/dotnet/guides/aspnet/compatibility/')
).toBe('https://docs.sentry.io/platforms/dotnet/guides/aspnet/');
expect(
await redirectLocation(list, '/platforms/dotnet/guides/aspnet/compatibility.md')
).toBe('https://docs.sentry.io/platforms/dotnet/guides/aspnet.md');
});
it('does not match other extensions', async () => {
const list = [{source: '/old/', destination: '/new/'}];
expect(await redirectLocation(list, '/old.mdx')).toBeNull();
expect(await redirectLocation(list, '/oldx/')).toBeNull();
});
it.each([
['/old.md', '/new.md'],
['/old/page.md', '/new.md'],
['/old/nested/page.md', '/new.md'],
['/old/page/', '/new/'],
['/old/page.mdx', '/new/'],
])('preserves the format of wildcard requests: %s', async (from, to) => {
expect(
await redirectLocation([{source: '/old/:path*', destination: '/new/'}], from)
).toBe(`https://docs.sentry.io${to}`);
});
it.each([
['/new/#section', 'https://docs.sentry.io/new.md#section'],
['/', 'https://docs.sentry.io/index.md'],
['/new/:path*', 'https://docs.sentry.io/new/nested/page.md'],
['https://mcp.sentry.dev/', 'https://mcp.sentry.dev/'],
['/platform-redirect/', 'https://docs.sentry.io/platform-redirect/'],
[
'/platform-redirect/?next=/x/',
'https://docs.sentry.io/platform-redirect/?next=%2Fx%2F',
],
])('handles wildcard destinations: %s', async (destination, expected) => {
expect(
await redirectLocation(
[{source: '/old/:path*', destination}],
'/old/nested/page.md'
)
).toBe(expected);
});
it.each([
['/product/insights/mcp.md', '/product/mcp-servers.md'],
['/product/crons.md', '/product/monitors-and-alerts/monitors/crons.md'],
['/product/insights/http.md', '/product/dashboards/sentry-dashboards.md'],
[
'/platforms/python/guides/django/configuration/capture/context.md',
'/platforms/python/guides/django/usage.md',
],
])('redirects %s using the real redirect list', async (from, to) => {
expect(await redirectLocation(userDocsRedirects, from)).toBe(
`https://docs.sentry.io${to}`
);
});
it('leaves root sources unchanged', () => {
const list = [{source: '/', destination: '/new/'}];
expect(withMarkdownRedirects(list)).toEqual(list);
});
});