-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-redirects.js
More file actions
executable file
Β·282 lines (259 loc) Β· 8.79 KB
/
Copy pathtest-redirects.js
File metadata and controls
executable file
Β·282 lines (259 loc) Β· 8.79 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#!/usr/bin/env node
/**
* Redirect Testing Script
* Tests all documented URL redirects on a specified domain
*
* Usage: node test-redirects.js [URL]
* Example: node test-redirects.js http://localhost:3000
* Example: node test-redirects.js https://deploy-preview-238--absmartly-docs.netlify.app
*/
const https = require('https');
const http = require('http');
const { URL } = require('url');
// Get URL from command line argument or use default
const PREVIEW_DOMAIN = process.argv[2] || 'http://localhost:3000';
// Hardcoded redirect mappings from Google Sheets
const REDIRECTS = [
// Experiment Creation
{
from: '/docs/web-console-docs/creating-an-experiment',
to: '/docs/web-console-docs/experiments/creating-an-experiment'
},
{
from: '/docs/web-console-docs/creating-an-experiment#metadata',
to: '/docs/web-console-docs/experiments/creating-an-experiment#metadata'
},
{
from: '/docs/web-console-docs/creating-an-experiment#experiment-name',
to: '/docs/web-console-docs/experiments/creating-an-experiment#experiment-name'
},
{
from: '/docs/web-console-docs/creating-an-experiment#tracking-unit',
to: '/docs/web-console-docs/experiments/creating-an-experiment#tracking-unit'
},
{
from: '/docs/web-console-docs/creating-an-experiment#application',
to: '/docs/web-console-docs/experiments/creating-an-experiment#applications'
},
{
from: '/docs/web-console-docs/creating-an-experiment#targeting-audiences',
to: '/docs/web-console-docs/experiments/creating-an-experiment#audiences'
},
{
from: '/docs/web-console-docs/creating-an-experiment#variants',
to: '/docs/web-console-docs/experiments/creating-an-experiment#variants'
},
{
from: '/docs/web-console-docs/creating-an-experiment#metrics',
to: '/docs/web-console-docs/experiments/creating-an-experiment#metrics'
},
{
from: '/docs/web-console-docs/creating-an-experiment#error-control',
to: '/docs/web-console-docs/experiments/setting-up-a-gst-experiment#error-control'
},
{
from: '/docs/web-console-docs/creating-an-experiment#sample-size-calculation',
to: '/docs/web-console-docs/experiments/setting-up-a-gst-experiment#what-is-the-experiment-duration-based-on'
},
// Feature Creation
{
from: '/docs/web-console-docs/creating-a-feature#feature-name',
to: '/docs/web-console-docs/feature-flags/creating-a-feature#basics'
},
{
from: '/docs/web-console-docs/creating-a-feature#tracking-unit',
to: '/docs/web-console-docs/creating-a-feature#audiences'
},
{
from: '/docs/web-console-docs/creating-a-feature#application',
to: '/docs/web-console-docs/creating-a-feature#audiences'
},
{
from: '/docs/web-console-docs/creating-a-feature#targeting-audiences',
to: '/docs/web-console-docs/creating-a-feature#audiences'
},
{
from: '/docs/web-console-docs/creating-a-feature#variants',
to: '/docs/web-console-docs/feature-flags/creating-a-feature#variants'
},
{
from: '/docs/web-console-docs/creating-a-feature#metrics',
to: '/docs/web-console-docs/feature-flags/creating-a-feature#metrics'
},
// Experiment Reports
{
from: '/docs/web-console-docs/Experiment-reports#experiment-velocity-report',
to: '/docs/web-console-docs/experiments/Experiment-reports#experiment-velocity-report'
},
{
from: '/docs/web-console-docs/Experiment-reports#decisions-overview',
to: '/docs/web-console-docs/experiments/Experiment-reports#decisions-overview'
},
{
from: '/docs/web-console-docs/Experiment-reports#decisions-history',
to: '/docs/web-console-docs/experiments/Experiment-reports#decisions-history'
},
// Metrics & Settings
{
from: '/docs/web-console-docs/understanding-experimentation-metrics/',
to: '/docs/web-console-docs/goals-and-metrics/metrics/overview'
},
{
from: '/docs/web-console-docs/settings#custom-fields',
to: '/docs/web-console-docs/Configuration/settings#custom-fields'
},
{
from: '/docs/web-console-docs/settings#dialogs',
to: '/docs/web-console-docs/Configuration/settings#dialogs'
},
// Analysis Methods
{
from: '/docs/web-console-docs/creating-an-experiment#type-of-analysis',
to: '/docs/web-console-docs/experiments/overview#analysis-methods'
},
{
from: '/docs/web-console-docs/type-of-analysis',
to: '/docs/web-console-docs/experiments/overview#analysis-methods'
},
{
from: '/docs/web-console-docs/setting-up-a-gst-experiment',
to: '/docs/web-console-docs/experiments/setting-up-a-fixed-horizon-experiment'
}
];
function normalizeUrl(url) {
return url.replace(/#.*$/, '');
}
function checkRedirect(fromPath, expectedToPath) {
return new Promise((resolve) => {
const fullUrl = `${PREVIEW_DOMAIN}${fromPath}`;
const parsedUrl = new URL(fullUrl);
const client = parsedUrl.protocol === 'https:' ? https : http;
const options = {
method: 'GET',
hostname: parsedUrl.hostname,
port: parsedUrl.port,
path: parsedUrl.pathname + parsedUrl.search + parsedUrl.hash,
headers: {
'User-Agent': 'Redirect-Checker/1.0'
},
// Don't follow redirects automatically
followRedirect: false
};
const req = client.request(options, (res) => {
const statusCode = res.statusCode;
const location = res.headers.location;
if (statusCode >= 300 && statusCode < 400 && location) {
// Extract path from location (could be relative or absolute)
let redirectPath;
if (location.startsWith('http')) {
const redirectUrl = new URL(location);
redirectPath = redirectUrl.pathname + redirectUrl.search + redirectUrl.hash;
} else {
redirectPath = location;
}
const normalizedRedirect = normalizeUrl(redirectPath);
const normalizedExpected = normalizeUrl(expectedToPath);
const isMatch = normalizedRedirect === normalizedExpected || redirectPath === expectedToPath;
resolve({
success: true,
statusCode,
redirectsTo: redirectPath,
expected: expectedToPath,
match: isMatch,
fromPath,
fullUrl
});
} else if (statusCode === 200) {
// Check if we landed on the expected page (might be client-side redirect)
resolve({
success: true,
statusCode: 200,
redirectsTo: fromPath,
expected: expectedToPath,
match: normalizeUrl(fromPath) === normalizeUrl(expectedToPath),
warning: 'No redirect header (200 OK) - might be client-side redirect',
fromPath,
fullUrl
});
} else {
resolve({
success: false,
statusCode,
error: `Unexpected status code: ${statusCode}`,
expected: expectedToPath,
fromPath,
fullUrl
});
}
});
req.on('error', (error) => {
resolve({
success: false,
error: error.message,
expected: expectedToPath,
fromPath,
fullUrl
});
});
req.setTimeout(10000, () => {
req.destroy();
resolve({
success: false,
error: 'Request timeout',
expected: expectedToPath,
fromPath,
fullUrl
});
});
req.end();
});
}
async function testAllRedirects() {
console.log('π Testing redirects on:', PREVIEW_DOMAIN);
console.log('β'.repeat(80));
console.log('');
const results = [];
let passed = 0;
let failed = 0;
let warnings = 0;
for (const redirect of REDIRECTS) {
const result = await checkRedirect(redirect.from, redirect.to);
results.push(result);
if (result.match) {
passed++;
console.log(`β
PASS: ${redirect.from} β ${redirect.to}`);
} else if (result.warning) {
warnings++;
console.log(`β οΈ WARN: ${redirect.from} β ${redirect.to} (${result.warning})`);
} else {
failed++;
console.log(`β FAIL: ${redirect.from} β ${redirect.to} (${result.error || 'Status: ' + result.statusCode})`);
}
// Small delay to avoid overwhelming the server
await new Promise(resolve => setTimeout(resolve, 100));
}
console.log('β'.repeat(80));
console.log('');
console.log('π SUMMARY');
console.log('β'.repeat(80));
console.log(`Total tests: ${REDIRECTS.length}`);
console.log(`β
Passed: ${passed}`);
console.log(`β οΈ Warnings: ${warnings}`);
console.log(`β Failed: ${failed}`);
console.log('');
if (failed > 0) {
console.log('β Some redirects are not working correctly.');
process.exit(1);
} else if (warnings > 0) {
console.log('β οΈ All redirects passed, but some have warnings.');
process.exit(0);
} else {
console.log('β
All redirects are working correctly!');
process.exit(0);
}
}
// Run the tests
testAllRedirects().catch((error) => {
console.error('Fatal error:', error);
process.exit(1);
});