forked from GoogleChrome/developer.chrome.com
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathredirect.js
More file actions
86 lines (69 loc) · 2.11 KB
/
redirect.js
File metadata and controls
86 lines (69 loc) · 2.11 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
/**
* @fileoverview Tests for the redirect-handler used by the server.
*/
const test = require('ava');
const {
buildCheckHandlerInternal,
} = require('../../server/handlers/redirects/redirect');
const YAML = require('js-yaml');
const sharedYamlSource = `
redirects:
- from: /subscribe
to: /newsletter
- from: /subscribe/all/...
to: /newsletter
- from: /foo/...
try: /bar/...
- from: /external/...
to: https://google.com/...
- from: /check_file_test
try:
- /does_not_exist.js
- /redirect.js # we expect this file to exist
else: /fallback.js
- from: /check_folder_test/...
try: /...
- from: /check_folder_test/...
to: /giveup.js
`;
const {redirects} = YAML.load(sharedYamlSource);
test('handles checking files', t => {
const h = buildCheckHandlerInternal(redirects, [
__dirname, // pretend to serve this directory
]);
t.is(h('/check_file_test'), '/redirect.js');
t.is(h('/check_folder_test/redirect.js'), '/redirect.js');
t.is(h('/check_folder_test/does_not_exist.js'), '/giveup.js');
});
test('handles simple redirects', t => {
const h = buildCheckHandlerInternal(redirects);
t.is(h('/subscribe'), '/newsletter');
t.is(
h('/subscribe/index.html'),
'/newsletter/index.html',
'trailing index.html is retained'
);
t.is(h('/subscribe/other.html'), null, 'unhandled URL returns null');
});
test('handles group redirects', t => {
const h = buildCheckHandlerInternal(redirects);
t.is(h('/subscribe/all/foo'), '/newsletter', 'group => non-group redirect');
t.is(
h('/foo/x'),
'/bar/x',
'group redirect functions, trailing slash unchanged'
);
t.is(h('/foo/x/index.html'), '/bar/x/index.html', 'index.html is retained');
t.is(
h('/external/hello'),
'https://google.com/hello',
'external redirect is also unchanged'
);
t.is(h('/external/'), 'https://google.com/');
t.is(h('/external'), 'https://google.com/', 'matches without trailing slash');
});
test('includes query strings', t => {
const h = buildCheckHandlerInternal(redirects);
t.is(h('/subscribe/?foo'), '/newsletter/?foo');
t.is(h('/subscribe/?foo=1&foo=2'), '/newsletter/?foo=1&foo=2');
});