From 639253d97193edd1c472e37bec250264c57be1ed Mon Sep 17 00:00:00 2001 From: Pieter Vander Vennet Date: Thu, 11 Jan 2024 05:37:25 +0100 Subject: [PATCH] Fix tests --- test/CodeQuality.spec.ts | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/test/CodeQuality.spec.ts b/test/CodeQuality.spec.ts index 205701af5..522d99b5b 100644 --- a/test/CodeQuality.spec.ts +++ b/test/CodeQuality.spec.ts @@ -81,6 +81,8 @@ async function validateScriptIntegrityOf(path: string): Promise { const doc = parse_html(htmlContents) // @ts-ignore const scripts = Array.from(doc.getElementsByTagName("script")) + // Maps source URL onto hash + const cachedHashes: Record = {} for (const script of scripts) { let src = script.getAttribute("src") if (src === undefined) { @@ -102,13 +104,15 @@ async function validateScriptIntegrityOf(path: string): Promise { if (src.startsWith("//")) { src = "https:" + src } - // Using 'scriptUtils' actually fetches data from the internet, it is not prohibited by the testHooks - const data: string = (await ScriptUtils.Download(src))["content"] - const hashed = await webcrypto.subtle.digest("SHA-384", new TextEncoder().encode(data)) - const hashedStr = _arrayBufferToBase64(hashed) - console.log(src, hashedStr, integrity) + if (cachedHashes[src] === undefined) { + // Using 'scriptUtils' actually fetches data from the internet, it is not prohibited by the testHooks + const data: string = (await ScriptUtils.Download(src))["content"] + const hashed = await webcrypto.subtle.digest("SHA-384", new TextEncoder().encode(data)) + cachedHashes[src] = _arrayBufferToBase64(hashed) + } + console.log(src, cachedHashes[src], integrity) expect(integrity).to.equal( - "sha384-" + hashedStr, + "sha384-" + cachedHashes[src], "Loading a script from '" + src + "' in the file " + path + " has a mismatched checksum" ) }