add handshake redirect test

This commit is contained in:
Karol Wypchlo 2021-01-27 15:11:03 +01:00
parent 203ff0fd84
commit a002b21dac
1 changed files with 32 additions and 7 deletions

View File

@ -1024,6 +1024,20 @@ function fileEndpointCheck(done) {
skylinkVerification(done, linkInfo); skylinkVerification(done, linkInfo);
} }
// check whether hns/note-to-self would properly redirect to note-to-self/
function hnsEndpointDirectoryRedirect(done) {
const expected = {
name: "hns endpoint directory redirect",
skylink: "hns/note-to-self",
statusCode: 307,
headers: {
location: "note-to-self/",
},
};
skylinkVerification(done, expected, { redirects: 0 });
}
function parseHeaderString(header) { function parseHeaderString(header) {
try { try {
return JSON.parse(header); return JSON.parse(header);
@ -1033,26 +1047,36 @@ function parseHeaderString(header) {
} }
// skylinkVerification verifies a skylink against provided information. // skylinkVerification verifies a skylink against provided information.
function skylinkVerification(done, { name, skylink, bodyHash, headers }) { function skylinkVerification(done, { name, skylink, bodyHash, headers, statusCode }, { redirects } = {}) {
const time = process.hrtime(); const time = process.hrtime();
// Create the query for the skylink // Create the query for the skylink
const query = `${process.env.PORTAL_URL}/${skylink}?nocache=true`; const query = `${process.env.PORTAL_URL}/${skylink}`;
// Get the Skylink // Get the Skylink
superagent superagent
.get(query) .get(query)
.set("cookie", "nocache=true")
.redirects(redirects)
.ok((res) => (redirects === undefined ? res.ok : res.status < 400))
.responseType("blob") .responseType("blob")
.then( .then(
(response) => { (response) => {
const entry = { name, up: true, statusCode: response.statusCode, time: calculateElapsedTime(time) }; const entry = { name, up: true, statusCode: response.statusCode, time: calculateElapsedTime(time) };
const info = {}; const info = {};
// Check if the response body is valid by checking against the known hash if (statusCode && statusCode !== response.statusCode) {
const currentBodyHash = hash(response.body);
if (currentBodyHash !== bodyHash) {
entry.up = false; entry.up = false;
info.bodyHash = { expected: bodyHash, current: currentBodyHash }; info.statusCode = { expected: statusCode, current: response.statusCode };
}
// Check if the response body is valid by checking against the known hash
if (bodyHash) {
const currentBodyHash = hash(response.body);
if (currentBodyHash !== bodyHash) {
entry.up = false;
info.bodyHash = { expected: bodyHash, current: currentBodyHash };
}
} }
if (headers) { if (headers) {
@ -1065,7 +1089,7 @@ function skylinkVerification(done, { name, skylink, bodyHash, headers }) {
if (typeof currentHeader === "object") { if (typeof currentHeader === "object") {
info.headers[headerName] = ensureValidJSON(detailedDiff(expectedHeader, currentHeader)); info.headers[headerName] = ensureValidJSON(detailedDiff(expectedHeader, currentHeader));
} else { } else {
info.headers[headerName] = currentHeader; info.headers[headerName] = { expected: expectedHeader, current: currentHeader };
} }
} }
}); });
@ -1118,4 +1142,5 @@ module.exports = [
// uniswapHNSRedirectCheck, // uniswapHNSRedirectCheck,
uniswapHNSResolverCheck, uniswapHNSResolverCheck,
uniswapHNSResolverRedirectCheck, uniswapHNSResolverRedirectCheck,
hnsEndpointDirectoryRedirect,
]; ];