|
1 | 1 | const test = require('ava')
|
2 | 2 | const { each } = require('test-each')
|
3 | 3 |
|
4 |
| -const { parseAllRedirects } = require('..') |
5 |
| - |
6 |
| -const { FIXTURES_DIR } = require('./helpers/main') |
7 |
| - |
8 |
| -const parseRedirects = async function ({ fileFixtureNames, configFixtureName, configRedirects, opts }) { |
9 |
| - const redirectsFiles = |
10 |
| - fileFixtureNames === undefined |
11 |
| - ? undefined |
12 |
| - : fileFixtureNames.map((fileFixtureName) => `${FIXTURES_DIR}/redirects_file/${fileFixtureName}`) |
13 |
| - const netlifyConfigPath = |
14 |
| - configFixtureName === undefined ? undefined : `${FIXTURES_DIR}/netlify_config/${configFixtureName}.toml` |
15 |
| - return await parseAllRedirects({ redirectsFiles, netlifyConfigPath, configRedirects, ...opts }) |
16 |
| -} |
| 4 | +const { validateSuccess, validateErrors } = require('./helpers/main') |
17 | 5 |
|
18 | 6 | each(
|
19 | 7 | [
|
20 | 8 | {
|
21 | 9 | title: 'empty',
|
| 10 | + input: {}, |
22 | 11 | output: [],
|
23 | 12 | },
|
24 | 13 | {
|
25 | 14 | title: 'only_config',
|
26 |
| - configFixtureName: 'from_simple', |
| 15 | + input: { |
| 16 | + netlifyConfigPath: 'from_simple', |
| 17 | + }, |
27 | 18 | output: [
|
28 | 19 | {
|
29 | 20 | from: '/old-path',
|
30 | 21 | path: '/old-path',
|
31 |
| - query: {}, |
32 | 22 | to: '/new-path',
|
33 |
| - force: false, |
34 |
| - conditions: {}, |
35 |
| - headers: {}, |
36 |
| - proxy: false, |
37 | 23 | },
|
38 | 24 | ],
|
39 | 25 | },
|
40 | 26 | {
|
41 | 27 | title: 'only_files',
|
42 |
| - fileFixtureNames: ['from_simple', 'from_absolute_uri'], |
| 28 | + input: { |
| 29 | + redirectsFiles: ['from_simple', 'from_absolute_uri'], |
| 30 | + }, |
43 | 31 | output: [
|
44 | 32 | {
|
45 | 33 | from: '/home',
|
46 | 34 | path: '/home',
|
47 |
| - query: {}, |
48 | 35 | to: '/',
|
49 |
| - force: false, |
50 |
| - conditions: {}, |
51 |
| - headers: {}, |
52 |
| - proxy: false, |
53 | 36 | },
|
54 | 37 | {
|
55 | 38 | from: 'http://hello.bitballoon.com/*',
|
56 | 39 | scheme: 'http',
|
57 | 40 | host: 'hello.bitballoon.com',
|
58 | 41 | path: '/*',
|
59 |
| - query: {}, |
60 | 42 | to: 'http://www.hello.com/:splat',
|
61 |
| - force: false, |
62 |
| - conditions: {}, |
63 |
| - headers: {}, |
64 |
| - proxy: false, |
65 | 43 | },
|
66 | 44 | ],
|
67 | 45 | },
|
68 | 46 | {
|
69 | 47 | title: 'both_config_files',
|
70 |
| - fileFixtureNames: ['from_simple', 'from_absolute_uri'], |
71 |
| - configFixtureName: 'from_simple', |
| 48 | + input: { |
| 49 | + redirectsFiles: ['from_simple', 'from_absolute_uri'], |
| 50 | + netlifyConfigPath: 'from_simple', |
| 51 | + }, |
72 | 52 | output: [
|
73 | 53 | {
|
74 | 54 | from: '/home',
|
75 | 55 | path: '/home',
|
76 |
| - query: {}, |
77 | 56 | to: '/',
|
78 |
| - force: false, |
79 |
| - conditions: {}, |
80 |
| - headers: {}, |
81 |
| - proxy: false, |
82 | 57 | },
|
83 | 58 | {
|
84 | 59 | from: 'http://hello.bitballoon.com/*',
|
85 | 60 | scheme: 'http',
|
86 | 61 | host: 'hello.bitballoon.com',
|
87 | 62 | path: '/*',
|
88 |
| - query: {}, |
89 | 63 | to: 'http://www.hello.com/:splat',
|
90 |
| - force: false, |
91 |
| - conditions: {}, |
92 |
| - headers: {}, |
93 |
| - proxy: false, |
94 | 64 | },
|
95 | 65 | {
|
96 | 66 | from: '/old-path',
|
97 | 67 | path: '/old-path',
|
98 |
| - query: {}, |
99 | 68 | to: '/new-path',
|
100 |
| - force: false, |
101 |
| - conditions: {}, |
102 |
| - headers: {}, |
103 |
| - proxy: false, |
104 | 69 | },
|
105 | 70 | ],
|
106 | 71 | },
|
107 | 72 | {
|
108 | 73 | title: 'config_redirects',
|
109 |
| - configFixtureName: 'from_simple', |
110 |
| - configRedirects: [ |
111 |
| - { |
112 |
| - from: '/home', |
113 |
| - to: '/', |
114 |
| - }, |
115 |
| - ], |
| 74 | + input: { |
| 75 | + netlifyConfigPath: 'from_simple', |
| 76 | + configRedirects: [ |
| 77 | + { |
| 78 | + from: '/home', |
| 79 | + to: '/', |
| 80 | + }, |
| 81 | + ], |
| 82 | + }, |
116 | 83 | output: [
|
117 | 84 | {
|
118 | 85 | from: '/old-path',
|
119 | 86 | path: '/old-path',
|
120 |
| - query: {}, |
121 | 87 | to: '/new-path',
|
122 |
| - force: false, |
123 |
| - conditions: {}, |
124 |
| - headers: {}, |
125 |
| - proxy: false, |
126 | 88 | },
|
127 | 89 | {
|
128 | 90 | from: '/home',
|
129 | 91 | path: '/home',
|
130 |
| - query: {}, |
131 | 92 | to: '/',
|
132 |
| - force: false, |
133 |
| - conditions: {}, |
134 |
| - headers: {}, |
135 |
| - proxy: false, |
136 | 93 | },
|
137 | 94 | ],
|
138 | 95 | },
|
139 | 96 | {
|
140 | 97 | title: 'minimal',
|
141 |
| - fileFixtureNames: ['from_simple', 'from_absolute_uri'], |
142 |
| - configFixtureName: 'from_simple', |
| 98 | + input: { |
| 99 | + redirectsFiles: ['from_simple', 'from_absolute_uri'], |
| 100 | + netlifyConfigPath: 'from_simple', |
| 101 | + minimal: true, |
| 102 | + }, |
143 | 103 | output: [
|
144 | 104 | {
|
145 | 105 | from: '/home',
|
146 |
| - query: {}, |
147 | 106 | to: '/',
|
148 |
| - force: false, |
149 |
| - conditions: {}, |
150 |
| - headers: {}, |
151 | 107 | },
|
152 | 108 | {
|
153 | 109 | from: 'http://hello.bitballoon.com/*',
|
154 |
| - query: {}, |
155 | 110 | to: 'http://www.hello.com/:splat',
|
156 |
| - force: false, |
157 |
| - conditions: {}, |
158 |
| - headers: {}, |
159 | 111 | },
|
160 | 112 | {
|
161 | 113 | from: '/old-path',
|
162 |
| - query: {}, |
163 | 114 | to: '/new-path',
|
164 |
| - force: false, |
165 |
| - conditions: {}, |
166 |
| - headers: {}, |
167 | 115 | },
|
168 | 116 | ],
|
169 |
| - opts: { minimal: true }, |
| 117 | + }, |
| 118 | + { |
| 119 | + title: 'valid_redirects_mixed', |
| 120 | + input: { |
| 121 | + redirectsFiles: ['from_simple'], |
| 122 | + configRedirects: {}, |
| 123 | + }, |
| 124 | + output: [ |
| 125 | + { |
| 126 | + from: '/home', |
| 127 | + path: '/home', |
| 128 | + to: '/', |
| 129 | + }, |
| 130 | + ], |
170 | 131 | },
|
171 | 132 | ],
|
172 |
| - ({ title }, { fileFixtureNames, configFixtureName, configRedirects, output, opts }) => { |
| 133 | + ({ title }, opts) => { |
173 | 134 | test(`Parses netlify.toml and _redirects | ${title}`, async (t) => {
|
174 |
| - const { redirects, errors } = await parseRedirects({ fileFixtureNames, configFixtureName, configRedirects, opts }) |
175 |
| - t.is(errors.length, 0) |
176 |
| - t.deepEqual(redirects, output) |
| 135 | + await validateSuccess(t, opts) |
| 136 | + }) |
| 137 | + }, |
| 138 | +) |
| 139 | + |
| 140 | +each( |
| 141 | + [ |
| 142 | + { |
| 143 | + title: 'invalid_redirects_array', |
| 144 | + input: { |
| 145 | + configRedirects: {}, |
| 146 | + }, |
| 147 | + errorMessage: /must be an array/, |
| 148 | + }, |
| 149 | + { |
| 150 | + title: 'invalid_redirects_mixed', |
| 151 | + input: { |
| 152 | + redirectsFiles: ['from_simple'], |
| 153 | + configRedirects: {}, |
| 154 | + }, |
| 155 | + errorMessage: /must be an array/, |
| 156 | + }, |
| 157 | + ], |
| 158 | + ({ title }, opts) => { |
| 159 | + test(`Validate syntax errors | ${title}`, async (t) => { |
| 160 | + await validateErrors(t, opts) |
177 | 161 | })
|
178 | 162 | },
|
179 | 163 | )
|
0 commit comments