-
-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathtest_status_codes.js
More file actions
89 lines (71 loc) · 2.94 KB
/
test_status_codes.js
File metadata and controls
89 lines (71 loc) · 2.94 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
const fs = require("fs-extra");
const fetch = (...args) => import('node-fetch').then(({default: fetch}) => fetch(...args));
const convert = require('xml-js');
const base_url = "http://127.0.0.1:8080";
const output_dir = `static/html`;
let failures = 0;
let passes = 0;
const test_status_code = async (page, status, location) => {
if (location == undefined) {
location = null;
}
try {
// Don't follow redirects
const options = {
redirect: 'manual'
};
const response = await fetch(base_url + page, options);
if (response.status === status && response.headers.get('location') === location) {
//console.log('Success - expected:', status, 'got:',response.status, 'for page:', page);
passes++;
if (status === 200 && response.headers.get('content-type').startsWith('text/html')) {
if (page.slice(-1) === '/') page = page + 'index';
page = page + '.html';
const body = await response.text();
await fs.outputFile(output_dir + page, body, 'utf8');
}
} else {
console.error('Failed - expected:', status, 'got:', response.status, 'for page:', page, 'location:', location);
failures++;
}
} catch (error) {
console.error('Error - expected:', status, 'for page:', page);
console.log(error);
failures++;
}
};
const test_sitemap_pages = async () => {
const xml = await fs.readFile(`templates/sitemap.xml`, 'utf-8');
const sitemap = JSON.parse(convert.xml2json(xml, {compact: true}));
const urls = sitemap['urlset']['url'];
for ( var url in urls ) {
let page = urls[url]['loc']['_text'].trim();
page = page.replace('https://httparchive.org', '');
await test_status_code(page, 200);
}
}
const test_status_codes = async () => {
// Test success pages
await test_sitemap_pages();
await test_status_code('/reports/state-of-the-web?lens=drupal&start=2018_04_15&end=latest&view=list', 200);
await test_status_code('/reports/state-of-the-web?lens=drupal', 200);
await test_status_code('/reports/state-of-the-web?start=2018_04_15&end=latest', 200);
await test_status_code('/reports/state-of-the-web?view=grid', 200);
await test_status_code('/reports/techreport/landing', 200);
await test_status_code('/reports/techreport/drilldown', 200);
await test_status_code('/reports/techreport/comparison', 200);
await test_status_code('/reports/techreport/tech?tech=WordPress&geo=ALL&rank=ALL&start=2024-01-01&end=2024-03-01', 200);
await test_status_code('/reports/techreport/tech?tech=jQuery%2CWordPress&geo=ALL&rank=ALL&start=2024-01-01&end=2024-03-01', 200);
// Test non-sitemap pages
await test_status_code('/sitemap.xml', 200);
await test_status_code('/robots.txt', 200);
await test_status_code('/favicon.ico', 200);
//Test 404s
await test_status_code('/zz/', 404);
await test_status_code('/base/', 404);
console.log('Passes:', passes, "Failures:", failures);
process.exitCode = failures;
}
module.exports = {
test_status_codes
};