@docusaurus/plugin-client-redirects 2.0.0-beta.15a2b59f9 → 2.0.0-beta.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/collectRedirects.d.ts +1 -1
- package/lib/collectRedirects.js +30 -35
- package/lib/createRedirectPageContent.js +2 -4
- package/lib/extensionRedirects.d.ts +1 -1
- package/lib/extensionRedirects.js +9 -16
- package/lib/index.d.ts +3 -3
- package/lib/index.js +5 -5
- package/lib/normalizePluginOptions.d.ts +1 -1
- package/lib/normalizePluginOptions.js +2 -2
- package/lib/redirectValidation.d.ts +1 -1
- package/lib/types.d.ts +2 -14
- package/lib/writeRedirectFiles.d.ts +1 -1
- package/lib/writeRedirectFiles.js +15 -14
- package/package.json +16 -14
- package/src/collectRedirects.ts +36 -46
- package/src/createRedirectPageContent.ts +4 -4
- package/src/extensionRedirects.ts +9 -17
- package/src/index.ts +5 -4
- package/src/normalizePluginOptions.ts +6 -2
- package/src/plugin-client-redirects.d.ts +27 -0
- package/src/redirectValidation.ts +1 -1
- package/src/types.ts +2 -22
- package/src/writeRedirectFiles.ts +17 -14
- package/lib/.tsbuildinfo +0 -1
- package/src/__tests__/__snapshots__/collectRedirects.test.ts.snap +0 -27
- package/src/__tests__/__snapshots__/createRedirectPageContent.test.ts.snap +0 -29
- package/src/__tests__/__snapshots__/normalizePluginOptions.test.ts.snap +0 -35
- package/src/__tests__/__snapshots__/redirectValidation.test.ts.snap +0 -11
- package/src/__tests__/__snapshots__/writeRedirectFiles.test.ts.snap +0 -147
- package/src/__tests__/collectRedirects.test.ts +0 -340
- package/src/__tests__/createRedirectPageContent.test.ts +0 -26
- package/src/__tests__/extensionRedirects.test.ts +0 -143
- package/src/__tests__/normalizePluginOptions.test.ts +0 -76
- package/src/__tests__/redirectValidation.test.ts +0 -66
- package/src/__tests__/writeRedirectFiles.test.ts +0 -204
- package/tsconfig.json +0 -10
|
@@ -1,340 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
-
*
|
|
4
|
-
* This source code is licensed under the MIT license found in the
|
|
5
|
-
* LICENSE file in the root directory of this source tree.
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
import {PluginContext, UserPluginOptions} from '../types';
|
|
9
|
-
import collectRedirects from '../collectRedirects';
|
|
10
|
-
import normalizePluginOptions from '../normalizePluginOptions';
|
|
11
|
-
import {removeTrailingSlash} from '@docusaurus/utils';
|
|
12
|
-
|
|
13
|
-
function createTestPluginContext(
|
|
14
|
-
options?: UserPluginOptions,
|
|
15
|
-
relativeRoutesPaths: string[] = [],
|
|
16
|
-
): PluginContext {
|
|
17
|
-
return {
|
|
18
|
-
outDir: '/tmp',
|
|
19
|
-
baseUrl: 'https://docusaurus.io',
|
|
20
|
-
relativeRoutesPaths,
|
|
21
|
-
options: normalizePluginOptions(options),
|
|
22
|
-
};
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
describe('collectRedirects', () => {
|
|
26
|
-
test('should collect no redirect for undefined config', () => {
|
|
27
|
-
expect(
|
|
28
|
-
collectRedirects(
|
|
29
|
-
createTestPluginContext(undefined, ['/', '/path']),
|
|
30
|
-
undefined,
|
|
31
|
-
),
|
|
32
|
-
).toEqual([]);
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
test('should collect no redirect for empty config', () => {
|
|
36
|
-
expect(collectRedirects(createTestPluginContext({}), undefined)).toEqual(
|
|
37
|
-
[],
|
|
38
|
-
);
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
test('should collect redirects to html/exe extension', () => {
|
|
42
|
-
expect(
|
|
43
|
-
collectRedirects(
|
|
44
|
-
createTestPluginContext(
|
|
45
|
-
{
|
|
46
|
-
fromExtensions: ['html', 'exe'],
|
|
47
|
-
},
|
|
48
|
-
['/', '/somePath', '/otherPath.html'],
|
|
49
|
-
),
|
|
50
|
-
undefined,
|
|
51
|
-
),
|
|
52
|
-
).toEqual([
|
|
53
|
-
{
|
|
54
|
-
from: '/somePath.html',
|
|
55
|
-
to: '/somePath',
|
|
56
|
-
},
|
|
57
|
-
{
|
|
58
|
-
from: '/somePath.exe',
|
|
59
|
-
to: '/somePath',
|
|
60
|
-
},
|
|
61
|
-
]);
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
test('should collect redirects to html/exe extension', () => {
|
|
65
|
-
expect(
|
|
66
|
-
collectRedirects(
|
|
67
|
-
createTestPluginContext(
|
|
68
|
-
{
|
|
69
|
-
toExtensions: ['html', 'exe'],
|
|
70
|
-
},
|
|
71
|
-
['/', '/somePath', '/otherPath.html'],
|
|
72
|
-
),
|
|
73
|
-
undefined,
|
|
74
|
-
),
|
|
75
|
-
).toEqual([
|
|
76
|
-
{
|
|
77
|
-
from: '/otherPath',
|
|
78
|
-
to: '/otherPath.html',
|
|
79
|
-
},
|
|
80
|
-
]);
|
|
81
|
-
});
|
|
82
|
-
|
|
83
|
-
test('should collect redirects from plugin option redirects', () => {
|
|
84
|
-
expect(
|
|
85
|
-
collectRedirects(
|
|
86
|
-
createTestPluginContext(
|
|
87
|
-
{
|
|
88
|
-
redirects: [
|
|
89
|
-
{
|
|
90
|
-
from: '/someLegacyPath',
|
|
91
|
-
to: '/somePath',
|
|
92
|
-
},
|
|
93
|
-
{
|
|
94
|
-
from: ['/someLegacyPathArray1', '/someLegacyPathArray2'],
|
|
95
|
-
to: '/',
|
|
96
|
-
},
|
|
97
|
-
],
|
|
98
|
-
},
|
|
99
|
-
['/', '/somePath'],
|
|
100
|
-
),
|
|
101
|
-
undefined,
|
|
102
|
-
),
|
|
103
|
-
).toEqual([
|
|
104
|
-
{
|
|
105
|
-
from: '/someLegacyPath',
|
|
106
|
-
to: '/somePath',
|
|
107
|
-
},
|
|
108
|
-
{
|
|
109
|
-
from: '/someLegacyPathArray1',
|
|
110
|
-
to: '/',
|
|
111
|
-
},
|
|
112
|
-
{
|
|
113
|
-
from: '/someLegacyPathArray2',
|
|
114
|
-
to: '/',
|
|
115
|
-
},
|
|
116
|
-
]);
|
|
117
|
-
});
|
|
118
|
-
|
|
119
|
-
test('should collect redirects from plugin option redirects with trailingSlash=true', () => {
|
|
120
|
-
expect(
|
|
121
|
-
collectRedirects(
|
|
122
|
-
createTestPluginContext(
|
|
123
|
-
{
|
|
124
|
-
redirects: [
|
|
125
|
-
{
|
|
126
|
-
from: '/someLegacyPath',
|
|
127
|
-
to: '/somePath',
|
|
128
|
-
},
|
|
129
|
-
{
|
|
130
|
-
from: ['/someLegacyPathArray1', '/someLegacyPathArray2'],
|
|
131
|
-
to: '/',
|
|
132
|
-
},
|
|
133
|
-
],
|
|
134
|
-
},
|
|
135
|
-
['/', '/somePath/'],
|
|
136
|
-
),
|
|
137
|
-
true,
|
|
138
|
-
),
|
|
139
|
-
).toEqual([
|
|
140
|
-
{
|
|
141
|
-
from: '/someLegacyPath',
|
|
142
|
-
to: '/somePath/',
|
|
143
|
-
},
|
|
144
|
-
{
|
|
145
|
-
from: '/someLegacyPathArray1',
|
|
146
|
-
to: '/',
|
|
147
|
-
},
|
|
148
|
-
{
|
|
149
|
-
from: '/someLegacyPathArray2',
|
|
150
|
-
to: '/',
|
|
151
|
-
},
|
|
152
|
-
]);
|
|
153
|
-
});
|
|
154
|
-
|
|
155
|
-
test('should collect redirects from plugin option redirects with trailingSlash=false', () => {
|
|
156
|
-
expect(
|
|
157
|
-
collectRedirects(
|
|
158
|
-
createTestPluginContext(
|
|
159
|
-
{
|
|
160
|
-
redirects: [
|
|
161
|
-
{
|
|
162
|
-
from: '/someLegacyPath',
|
|
163
|
-
to: '/somePath/',
|
|
164
|
-
},
|
|
165
|
-
{
|
|
166
|
-
from: ['/someLegacyPathArray1', '/someLegacyPathArray2'],
|
|
167
|
-
to: '/',
|
|
168
|
-
},
|
|
169
|
-
],
|
|
170
|
-
},
|
|
171
|
-
['/', '/somePath'],
|
|
172
|
-
),
|
|
173
|
-
false,
|
|
174
|
-
),
|
|
175
|
-
).toEqual([
|
|
176
|
-
{
|
|
177
|
-
from: '/someLegacyPath',
|
|
178
|
-
to: '/somePath',
|
|
179
|
-
},
|
|
180
|
-
{
|
|
181
|
-
from: '/someLegacyPathArray1',
|
|
182
|
-
to: '/',
|
|
183
|
-
},
|
|
184
|
-
{
|
|
185
|
-
from: '/someLegacyPathArray2',
|
|
186
|
-
to: '/',
|
|
187
|
-
},
|
|
188
|
-
]);
|
|
189
|
-
});
|
|
190
|
-
|
|
191
|
-
test('should throw if plugin option redirects contain invalid to paths', () => {
|
|
192
|
-
expect(() =>
|
|
193
|
-
collectRedirects(
|
|
194
|
-
createTestPluginContext(
|
|
195
|
-
{
|
|
196
|
-
redirects: [
|
|
197
|
-
{
|
|
198
|
-
from: '/someLegacyPath',
|
|
199
|
-
to: '/',
|
|
200
|
-
},
|
|
201
|
-
{
|
|
202
|
-
from: '/someLegacyPath',
|
|
203
|
-
to: '/this/path/does/not/exist2',
|
|
204
|
-
},
|
|
205
|
-
{
|
|
206
|
-
from: '/someLegacyPath',
|
|
207
|
-
to: '/this/path/does/not/exist2',
|
|
208
|
-
},
|
|
209
|
-
],
|
|
210
|
-
},
|
|
211
|
-
['/', '/someExistingPath', '/anotherExistingPath'],
|
|
212
|
-
),
|
|
213
|
-
undefined,
|
|
214
|
-
),
|
|
215
|
-
).toThrowErrorMatchingSnapshot();
|
|
216
|
-
});
|
|
217
|
-
|
|
218
|
-
test('should collect redirects with custom redirect creator', () => {
|
|
219
|
-
expect(
|
|
220
|
-
collectRedirects(
|
|
221
|
-
createTestPluginContext(
|
|
222
|
-
{
|
|
223
|
-
createRedirects: (routePath) => {
|
|
224
|
-
return [
|
|
225
|
-
`${removeTrailingSlash(routePath)}/some/path/suffix1`,
|
|
226
|
-
`${removeTrailingSlash(routePath)}/some/other/path/suffix2`,
|
|
227
|
-
];
|
|
228
|
-
},
|
|
229
|
-
},
|
|
230
|
-
['/', '/testpath', '/otherPath.html'],
|
|
231
|
-
),
|
|
232
|
-
undefined,
|
|
233
|
-
),
|
|
234
|
-
).toEqual([
|
|
235
|
-
{
|
|
236
|
-
from: '/some/path/suffix1',
|
|
237
|
-
to: '/',
|
|
238
|
-
},
|
|
239
|
-
{
|
|
240
|
-
from: '/some/other/path/suffix2',
|
|
241
|
-
to: '/',
|
|
242
|
-
},
|
|
243
|
-
|
|
244
|
-
{
|
|
245
|
-
from: '/testpath/some/path/suffix1',
|
|
246
|
-
to: '/testpath',
|
|
247
|
-
},
|
|
248
|
-
{
|
|
249
|
-
from: '/testpath/some/other/path/suffix2',
|
|
250
|
-
to: '/testpath',
|
|
251
|
-
},
|
|
252
|
-
|
|
253
|
-
{
|
|
254
|
-
from: '/otherPath.html/some/path/suffix1',
|
|
255
|
-
to: '/otherPath.html',
|
|
256
|
-
},
|
|
257
|
-
{
|
|
258
|
-
from: '/otherPath.html/some/other/path/suffix2',
|
|
259
|
-
to: '/otherPath.html',
|
|
260
|
-
},
|
|
261
|
-
]);
|
|
262
|
-
});
|
|
263
|
-
|
|
264
|
-
test('should throw if redirect creator creates invalid redirects', () => {
|
|
265
|
-
expect(() =>
|
|
266
|
-
collectRedirects(
|
|
267
|
-
createTestPluginContext(
|
|
268
|
-
{
|
|
269
|
-
createRedirects: (routePath) => {
|
|
270
|
-
if (routePath === '/') {
|
|
271
|
-
return [
|
|
272
|
-
`https://google.com/`,
|
|
273
|
-
`//abc`,
|
|
274
|
-
`/def?queryString=toto`,
|
|
275
|
-
];
|
|
276
|
-
}
|
|
277
|
-
return undefined;
|
|
278
|
-
},
|
|
279
|
-
},
|
|
280
|
-
['/'],
|
|
281
|
-
),
|
|
282
|
-
undefined,
|
|
283
|
-
),
|
|
284
|
-
).toThrowErrorMatchingSnapshot();
|
|
285
|
-
});
|
|
286
|
-
|
|
287
|
-
test('should throw if redirect creator creates array of array redirect', () => {
|
|
288
|
-
expect(() =>
|
|
289
|
-
collectRedirects(
|
|
290
|
-
createTestPluginContext(
|
|
291
|
-
{
|
|
292
|
-
createRedirects: (routePath) => {
|
|
293
|
-
if (routePath === '/') {
|
|
294
|
-
return ([[`/fromPath`]] as unknown) as string;
|
|
295
|
-
}
|
|
296
|
-
return undefined;
|
|
297
|
-
},
|
|
298
|
-
},
|
|
299
|
-
['/'],
|
|
300
|
-
),
|
|
301
|
-
undefined,
|
|
302
|
-
),
|
|
303
|
-
).toThrowErrorMatchingSnapshot();
|
|
304
|
-
});
|
|
305
|
-
|
|
306
|
-
test('should filter unwanted redirects', () => {
|
|
307
|
-
expect(
|
|
308
|
-
collectRedirects(
|
|
309
|
-
createTestPluginContext(
|
|
310
|
-
{
|
|
311
|
-
fromExtensions: ['html', 'exe'],
|
|
312
|
-
toExtensions: ['html', 'exe'],
|
|
313
|
-
},
|
|
314
|
-
[
|
|
315
|
-
'/',
|
|
316
|
-
'/somePath',
|
|
317
|
-
'/somePath.html',
|
|
318
|
-
'/somePath.exe',
|
|
319
|
-
'/fromShouldWork.html',
|
|
320
|
-
'/toShouldWork',
|
|
321
|
-
],
|
|
322
|
-
),
|
|
323
|
-
undefined,
|
|
324
|
-
),
|
|
325
|
-
).toEqual([
|
|
326
|
-
{
|
|
327
|
-
from: '/toShouldWork.html',
|
|
328
|
-
to: '/toShouldWork',
|
|
329
|
-
},
|
|
330
|
-
{
|
|
331
|
-
from: '/toShouldWork.exe',
|
|
332
|
-
to: '/toShouldWork',
|
|
333
|
-
},
|
|
334
|
-
{
|
|
335
|
-
from: '/fromShouldWork',
|
|
336
|
-
to: '/fromShouldWork.html',
|
|
337
|
-
},
|
|
338
|
-
]);
|
|
339
|
-
});
|
|
340
|
-
});
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
-
*
|
|
4
|
-
* This source code is licensed under the MIT license found in the
|
|
5
|
-
* LICENSE file in the root directory of this source tree.
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
import createRedirectPageContent from '../createRedirectPageContent';
|
|
9
|
-
|
|
10
|
-
describe('createRedirectPageContent', () => {
|
|
11
|
-
test('should match snapshot', () => {
|
|
12
|
-
expect(
|
|
13
|
-
createRedirectPageContent({toUrl: 'https://docusaurus.io/'}),
|
|
14
|
-
).toMatchSnapshot();
|
|
15
|
-
});
|
|
16
|
-
|
|
17
|
-
test('should encode uri special chars', () => {
|
|
18
|
-
const result = createRedirectPageContent({
|
|
19
|
-
toUrl: 'https://docusaurus.io/gr/σελιδας/',
|
|
20
|
-
});
|
|
21
|
-
expect(result).toContain(
|
|
22
|
-
'https://docusaurus.io/gr/%CF%83%CE%B5%CE%BB%CE%B9%CE%B4%CE%B1%CF%82/',
|
|
23
|
-
);
|
|
24
|
-
expect(result).toMatchSnapshot();
|
|
25
|
-
});
|
|
26
|
-
});
|
|
@@ -1,143 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
-
*
|
|
4
|
-
* This source code is licensed under the MIT license found in the
|
|
5
|
-
* LICENSE file in the root directory of this source tree.
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
import {
|
|
9
|
-
createFromExtensionsRedirects,
|
|
10
|
-
createToExtensionsRedirects,
|
|
11
|
-
} from '../extensionRedirects';
|
|
12
|
-
|
|
13
|
-
describe('createToExtensionsRedirects', () => {
|
|
14
|
-
test('should reject empty extensions', () => {
|
|
15
|
-
expect(() => {
|
|
16
|
-
createToExtensionsRedirects(['/'], ['.html']);
|
|
17
|
-
}).toThrowErrorMatchingInlineSnapshot(`
|
|
18
|
-
"Extension \\".html\\" contains a \\".\\" (dot) which is not allowed.
|
|
19
|
-
If the redirect extension system is not good enough for your usecase, you can create redirects yourself with the \\"createRedirects\\" plugin option."
|
|
20
|
-
`);
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
test('should reject extensions with .', () => {
|
|
24
|
-
expect(() => {
|
|
25
|
-
createToExtensionsRedirects(['/'], ['.html']);
|
|
26
|
-
}).toThrowErrorMatchingInlineSnapshot(`
|
|
27
|
-
"Extension \\".html\\" contains a \\".\\" (dot) which is not allowed.
|
|
28
|
-
If the redirect extension system is not good enough for your usecase, you can create redirects yourself with the \\"createRedirects\\" plugin option."
|
|
29
|
-
`);
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
test('should reject extensions with /', () => {
|
|
33
|
-
expect(() => {
|
|
34
|
-
createToExtensionsRedirects(['/'], ['ht/ml']);
|
|
35
|
-
}).toThrowErrorMatchingInlineSnapshot(`
|
|
36
|
-
"Extension \\"ht/ml\\" contains a \\"/\\" (slash) which is not allowed.
|
|
37
|
-
If the redirect extension system is not good enough for your usecase, you can create redirects yourself with the \\"createRedirects\\" plugin option."
|
|
38
|
-
`);
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
test('should reject extensions with illegal url char', () => {
|
|
42
|
-
expect(() => {
|
|
43
|
-
createToExtensionsRedirects(['/'], [',']);
|
|
44
|
-
}).toThrowErrorMatchingInlineSnapshot(`
|
|
45
|
-
"Extension \\",\\" contains invalid URI characters.
|
|
46
|
-
If the redirect extension system is not good enough for your usecase, you can create redirects yourself with the \\"createRedirects\\" plugin option."
|
|
47
|
-
`);
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
test('should create redirects from html/htm extensions', () => {
|
|
51
|
-
const ext = ['html', 'htm'];
|
|
52
|
-
expect(createToExtensionsRedirects([''], ext)).toEqual([]);
|
|
53
|
-
expect(createToExtensionsRedirects(['/'], ext)).toEqual([]);
|
|
54
|
-
expect(createToExtensionsRedirects(['/abc.html'], ext)).toEqual([
|
|
55
|
-
{from: '/abc', to: '/abc.html'},
|
|
56
|
-
]);
|
|
57
|
-
expect(createToExtensionsRedirects(['/abc.htm'], ext)).toEqual([
|
|
58
|
-
{from: '/abc', to: '/abc.htm'},
|
|
59
|
-
]);
|
|
60
|
-
expect(createToExtensionsRedirects(['/abc.xyz'], ext)).toEqual([]);
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
test('should create "to" redirects when relativeRoutesPath contains a prefix', () => {
|
|
64
|
-
expect(
|
|
65
|
-
createToExtensionsRedirects(['/prefix/file.html'], ['html']),
|
|
66
|
-
).toEqual([{from: '/prefix/file', to: '/prefix/file.html'}]);
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
test('should not create redirection for an empty extension array', () => {
|
|
70
|
-
const ext: string[] = [];
|
|
71
|
-
expect(createToExtensionsRedirects([''], ext)).toEqual([]);
|
|
72
|
-
expect(createToExtensionsRedirects(['/'], ext)).toEqual([]);
|
|
73
|
-
expect(createToExtensionsRedirects(['/abc.html'], ext)).toEqual([]);
|
|
74
|
-
});
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
describe('createFromExtensionsRedirects', () => {
|
|
78
|
-
test('should reject empty extensions', () => {
|
|
79
|
-
expect(() => {
|
|
80
|
-
createFromExtensionsRedirects(['/'], ['.html']);
|
|
81
|
-
}).toThrowErrorMatchingInlineSnapshot(`
|
|
82
|
-
"Extension \\".html\\" contains a \\".\\" (dot) which is not allowed.
|
|
83
|
-
If the redirect extension system is not good enough for your usecase, you can create redirects yourself with the \\"createRedirects\\" plugin option."
|
|
84
|
-
`);
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
test('should reject extensions with .', () => {
|
|
88
|
-
expect(() => {
|
|
89
|
-
createFromExtensionsRedirects(['/'], ['.html']);
|
|
90
|
-
}).toThrowErrorMatchingInlineSnapshot(`
|
|
91
|
-
"Extension \\".html\\" contains a \\".\\" (dot) which is not allowed.
|
|
92
|
-
If the redirect extension system is not good enough for your usecase, you can create redirects yourself with the \\"createRedirects\\" plugin option."
|
|
93
|
-
`);
|
|
94
|
-
});
|
|
95
|
-
|
|
96
|
-
test('should reject extensions with /', () => {
|
|
97
|
-
expect(() => {
|
|
98
|
-
createFromExtensionsRedirects(['/'], ['ht/ml']);
|
|
99
|
-
}).toThrowErrorMatchingInlineSnapshot(`
|
|
100
|
-
"Extension \\"ht/ml\\" contains a \\"/\\" (slash) which is not allowed.
|
|
101
|
-
If the redirect extension system is not good enough for your usecase, you can create redirects yourself with the \\"createRedirects\\" plugin option."
|
|
102
|
-
`);
|
|
103
|
-
});
|
|
104
|
-
|
|
105
|
-
test('should reject extensions with illegal url char', () => {
|
|
106
|
-
expect(() => {
|
|
107
|
-
createFromExtensionsRedirects(['/'], [',']);
|
|
108
|
-
}).toThrowErrorMatchingInlineSnapshot(`
|
|
109
|
-
"Extension \\",\\" contains invalid URI characters.
|
|
110
|
-
If the redirect extension system is not good enough for your usecase, you can create redirects yourself with the \\"createRedirects\\" plugin option."
|
|
111
|
-
`);
|
|
112
|
-
});
|
|
113
|
-
|
|
114
|
-
test('should create redirects from html/htm extensions', () => {
|
|
115
|
-
const ext = ['html', 'htm'];
|
|
116
|
-
expect(createFromExtensionsRedirects([''], ext)).toEqual([]);
|
|
117
|
-
expect(createFromExtensionsRedirects(['/'], ext)).toEqual([]);
|
|
118
|
-
expect(createFromExtensionsRedirects(['/abc'], ext)).toEqual([
|
|
119
|
-
{from: '/abc.html', to: '/abc'},
|
|
120
|
-
{from: '/abc.htm', to: '/abc'},
|
|
121
|
-
]);
|
|
122
|
-
expect(createFromExtensionsRedirects(['/def.html'], ext)).toEqual([]);
|
|
123
|
-
expect(createFromExtensionsRedirects(['/def/'], ext)).toEqual([
|
|
124
|
-
{from: '/def.html/', to: '/def/'},
|
|
125
|
-
{from: '/def.htm/', to: '/def/'},
|
|
126
|
-
]);
|
|
127
|
-
});
|
|
128
|
-
|
|
129
|
-
test('should create "from" redirects when relativeRoutesPath contains a prefix', () => {
|
|
130
|
-
expect(createFromExtensionsRedirects(['/prefix/file'], ['html'])).toEqual([
|
|
131
|
-
{from: '/prefix/file.html', to: '/prefix/file'},
|
|
132
|
-
]);
|
|
133
|
-
});
|
|
134
|
-
|
|
135
|
-
test('should not create redirection for an empty extension array', () => {
|
|
136
|
-
const ext: string[] = [];
|
|
137
|
-
expect(createFromExtensionsRedirects([''], ext)).toEqual([]);
|
|
138
|
-
expect(createFromExtensionsRedirects(['/'], ext)).toEqual([]);
|
|
139
|
-
expect(createFromExtensionsRedirects(['/abc'], ext)).toEqual([]);
|
|
140
|
-
expect(createFromExtensionsRedirects(['/def.html'], ext)).toEqual([]);
|
|
141
|
-
expect(createFromExtensionsRedirects(['/def/'], ext)).toEqual([]);
|
|
142
|
-
});
|
|
143
|
-
});
|
|
@@ -1,76 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
-
*
|
|
4
|
-
* This source code is licensed under the MIT license found in the
|
|
5
|
-
* LICENSE file in the root directory of this source tree.
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
import normalizePluginOptions, {
|
|
9
|
-
DefaultPluginOptions,
|
|
10
|
-
} from '../normalizePluginOptions';
|
|
11
|
-
import {CreateRedirectsFnOption} from '../types';
|
|
12
|
-
|
|
13
|
-
describe('normalizePluginOptions', () => {
|
|
14
|
-
test('should return default options for undefined user options', () => {
|
|
15
|
-
expect(normalizePluginOptions()).toEqual(DefaultPluginOptions);
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
test('should return default options for empty user options', () => {
|
|
19
|
-
expect(normalizePluginOptions()).toEqual(DefaultPluginOptions);
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
test('should override one default options with valid user options', () => {
|
|
23
|
-
expect(
|
|
24
|
-
normalizePluginOptions({
|
|
25
|
-
toExtensions: ['html'],
|
|
26
|
-
}),
|
|
27
|
-
).toEqual({...DefaultPluginOptions, toExtensions: ['html']});
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
test('should override all default options with valid user options', () => {
|
|
31
|
-
const createRedirects: CreateRedirectsFnOption = (_routePath: string) => {
|
|
32
|
-
return [];
|
|
33
|
-
};
|
|
34
|
-
expect(
|
|
35
|
-
normalizePluginOptions({
|
|
36
|
-
fromExtensions: ['exe', 'zip'],
|
|
37
|
-
toExtensions: ['html'],
|
|
38
|
-
createRedirects,
|
|
39
|
-
redirects: [{from: '/x', to: '/y'}],
|
|
40
|
-
}),
|
|
41
|
-
).toEqual({
|
|
42
|
-
id: 'default',
|
|
43
|
-
fromExtensions: ['exe', 'zip'],
|
|
44
|
-
toExtensions: ['html'],
|
|
45
|
-
createRedirects,
|
|
46
|
-
redirects: [{from: '/x', to: '/y'}],
|
|
47
|
-
});
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
test('should reject bad fromExtensions user inputs', () => {
|
|
51
|
-
expect(() =>
|
|
52
|
-
normalizePluginOptions({
|
|
53
|
-
fromExtensions: ([null, undefined, 123, true] as unknown) as string[],
|
|
54
|
-
}),
|
|
55
|
-
).toThrowErrorMatchingSnapshot();
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
test('should reject bad toExtensions user inputs', () => {
|
|
59
|
-
expect(() =>
|
|
60
|
-
normalizePluginOptions({
|
|
61
|
-
toExtensions: ([null, undefined, 123, true] as unknown) as string[],
|
|
62
|
-
}),
|
|
63
|
-
).toThrowErrorMatchingSnapshot();
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
test('should reject bad createRedirects user inputs', () => {
|
|
67
|
-
expect(() =>
|
|
68
|
-
normalizePluginOptions({
|
|
69
|
-
createRedirects: ([
|
|
70
|
-
'bad',
|
|
71
|
-
'value',
|
|
72
|
-
] as unknown) as CreateRedirectsFnOption,
|
|
73
|
-
}),
|
|
74
|
-
).toThrowErrorMatchingSnapshot();
|
|
75
|
-
});
|
|
76
|
-
});
|
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
-
*
|
|
4
|
-
* This source code is licensed under the MIT license found in the
|
|
5
|
-
* LICENSE file in the root directory of this source tree.
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
import {validateRedirect} from '../redirectValidation';
|
|
9
|
-
|
|
10
|
-
describe('validateRedirect', () => {
|
|
11
|
-
test('validate good redirects without throwing', () => {
|
|
12
|
-
validateRedirect({
|
|
13
|
-
from: '/fromSomePath',
|
|
14
|
-
to: '/toSomePath',
|
|
15
|
-
});
|
|
16
|
-
validateRedirect({
|
|
17
|
-
from: '/from/Some/Path',
|
|
18
|
-
to: '/toSomePath',
|
|
19
|
-
});
|
|
20
|
-
validateRedirect({
|
|
21
|
-
from: '/fromSomePath',
|
|
22
|
-
to: '/toSomePath',
|
|
23
|
-
});
|
|
24
|
-
validateRedirect({
|
|
25
|
-
from: '/fromSomePath',
|
|
26
|
-
to: '/to/Some/Path',
|
|
27
|
-
});
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
test('throw for bad redirects', () => {
|
|
31
|
-
expect(() =>
|
|
32
|
-
validateRedirect({
|
|
33
|
-
from: 'https://fb.com/fromSomePath',
|
|
34
|
-
to: '/toSomePath',
|
|
35
|
-
}),
|
|
36
|
-
).toThrowErrorMatchingSnapshot();
|
|
37
|
-
|
|
38
|
-
expect(() =>
|
|
39
|
-
validateRedirect({
|
|
40
|
-
from: '/fromSomePath',
|
|
41
|
-
to: 'https://fb.com/toSomePath',
|
|
42
|
-
}),
|
|
43
|
-
).toThrowErrorMatchingSnapshot();
|
|
44
|
-
|
|
45
|
-
expect(() =>
|
|
46
|
-
validateRedirect({
|
|
47
|
-
from: '/fromSomePath',
|
|
48
|
-
to: '/toSomePath?queryString=xyz',
|
|
49
|
-
}),
|
|
50
|
-
).toThrowErrorMatchingSnapshot();
|
|
51
|
-
|
|
52
|
-
expect(() =>
|
|
53
|
-
validateRedirect({
|
|
54
|
-
from: (null as unknown) as string,
|
|
55
|
-
to: '/toSomePath?queryString=xyz',
|
|
56
|
-
}),
|
|
57
|
-
).toThrowErrorMatchingSnapshot();
|
|
58
|
-
|
|
59
|
-
expect(() =>
|
|
60
|
-
validateRedirect({
|
|
61
|
-
from: (['heyho'] as unknown) as string,
|
|
62
|
-
to: '/toSomePath?queryString=xyz',
|
|
63
|
-
}),
|
|
64
|
-
).toThrowErrorMatchingSnapshot();
|
|
65
|
-
});
|
|
66
|
-
});
|