-
Notifications
You must be signed in to change notification settings - Fork 1
/
extended.js
115 lines (99 loc) · 4.25 KB
/
extended.js
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
import got from "got";
import hasha from "hasha";
import { detailedDiff } from "deep-object-diff";
import { isEqual } from "lodash-es";
import { calculateElapsedTime, ensureValidJSON, getResponseContent, parseHeaderString } from "../utils.js";
import extendedChecks from "../fixtures/extendedChecks.json" assert { type: "json" };
/**
*
* @param {string} name check names
* @param {object} expected expected data (metadata, headers, etc.)
* @param {object} config config object containing optional request parameters
*/
async function executeExtendedCheck(name, expected, config = {}) {
const time = process.hrtime();
const details = { name, skylink: expected.skylink };
try {
const query = `https://${process.env.PORTAL_DOMAIN}/${expected.skylink}`;
const response = await got[config.method ?? "get"](query, {
followRedirect: config.followRedirect ?? true,
headers: { "Skynet-Api-Key": process.env.ACCOUNTS_TEST_USER_API_KEY },
});
// prepare entry report object
const entry = { ...details, up: true, statusCode: response.statusCode, time: calculateElapsedTime(time) };
// prepare additional info object for any mismatch reposts
const info = {};
// compare status codes if defined in the expected response object
if (expected.statusCode && expected.statusCode !== response.statusCode) {
entry.up = false;
info.statusCode = { expected: expected.statusCode, current: response.statusCode };
}
// compare body hash if defined in the expected response object
if (expected.bodyHash) {
const currentBodyHash = hasha(response.rawBody, { algorithm: "sha1" });
if (currentBodyHash !== expected.bodyHash) {
entry.up = false;
info.bodyHash = { expected: expected.bodyHash, current: currentBodyHash };
}
}
// compare headers if defined in the expected response object
if (expected.headers) {
Object.entries(expected.headers).forEach(([headerName, expectedHeader]) => {
const currentHeader = parseHeaderString(response.headers[headerName]);
if (!isEqual(currentHeader, expectedHeader)) {
entry.up = false;
info.headers = info.headers ?? {};
// special deep diff mode report for headers containing valid json objects
if (typeof currentHeader === "object") {
info.headers[headerName] = ensureValidJSON(detailedDiff(expectedHeader, currentHeader));
} else {
info.headers[headerName] = { expected: expectedHeader, current: currentHeader };
}
}
});
}
// if metadata comparison is expected and skylink is provided, fetch metadata
// in separate request and compare it with expected metadata
if (expected.metadata && expected.skylink) {
const url = `https://${process.env.PORTAL_DOMAIN}/skynet/metadata/${expected.skylink}`;
try {
const metadata = await got(url, {
headers: { "Skynet-Api-Key": process.env.ACCOUNTS_TEST_USER_API_KEY },
}).json();
// deep compare requested metadata with expected metadata
if (!isEqual(expected.metadata, metadata)) {
entry.up = false;
// report metadata diff on mismatch
info.metadata = { url, diff: ensureValidJSON(detailedDiff(expected.metadata, metadata)) };
}
} catch (error) {
entry.up = false;
info.metadata = {
url,
ip: error?.response?.ip ?? null,
statusCode: error?.response?.statusCode || error.statusCode || error.status,
errorMessage: error.message,
errorResponseContent: getResponseContent(error.response),
};
}
}
// attach info only if it exists
if (Object.keys(info).length) {
entry.info = info;
}
return entry; // return the entry information
} catch (error) {
return {
...details,
up: false,
ip: error?.response?.ip ?? null,
statusCode: error?.response?.statusCode || error.statusCode || error.status,
errorMessage: error.message,
errorResponseContent: getResponseContent(error.response),
time: calculateElapsedTime(time),
};
}
}
export default extendedChecks.map((extendedCheck) => {
return () => executeExtendedCheck(extendedCheck.name, extendedCheck.data, extendedCheck.config);
});