mirror of
https://github.com/ading2210/libcurl.js.git
synced 2025-05-13 22:40:01 -04:00
fix bug with response body, add test for download hash
This commit is contained in:
parent
8e5a23adf2
commit
82249fe707
6 changed files with 31 additions and 4 deletions
|
@ -64,6 +64,9 @@ class JSTest(unittest.TestCase):
|
|||
|
||||
def test_post(self):
|
||||
self.run_test("test_post.js")
|
||||
|
||||
def test_download_hash(self):
|
||||
self.run_test("test_download_hash.js")
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
20
client/tests/scripts/test_download_hash.js
Normal file
20
client/tests/scripts/test_download_hash.js
Normal file
|
@ -0,0 +1,20 @@
|
|||
//https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest#examples
|
||||
async function digestMessage(msgUint8) {
|
||||
const hashBuffer = await window.crypto.subtle.digest("SHA-256", msgUint8); // hash the message
|
||||
const hashArray = Array.from(new Uint8Array(hashBuffer)); // convert buffer to byte array
|
||||
const hashHex = hashArray
|
||||
.map((b) => b.toString(16).padStart(2, "0"))
|
||||
.join(""); // convert bytes to hex string
|
||||
return hashHex;
|
||||
}
|
||||
|
||||
async function test() {
|
||||
let url = "https://curl.se/download/curl-8.11.0.tar.gz";
|
||||
let expected_hash = "264537d90e58d2b09dddc50944baf3c38e7089151c8986715e2aaeaaf2b8118f";
|
||||
|
||||
let r = await libcurl.fetch(url);
|
||||
let msg = new Uint8Array(await r.arrayBuffer());
|
||||
let hash = await digestMessage(msg);
|
||||
|
||||
assert(hash === expected_hash, "unexpected hash of downloaded data");
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue