|
| 1 | +import { Checksum } from "@smithy/types"; |
| 2 | +import { toBase64 } from "@smithy/util-base64"; |
| 3 | +import { toUtf8 } from "@smithy/util-utf8"; |
| 4 | + |
| 5 | +import { headStream } from "../headStream.browser"; |
| 6 | +import { createChecksumStream } from "./createChecksumStream.browser"; |
| 7 | +import { ChecksumStream as ChecksumStreamWeb } from "./createChecksumStream.browser"; |
| 8 | + |
| 9 | +describe("Checksum streams", () => { |
| 10 | + /** |
| 11 | + * Hash "algorithm" that appends all data together. |
| 12 | + */ |
| 13 | + class Appender implements Checksum { |
| 14 | + public hash = ""; |
| 15 | + async digest(): Promise<Uint8Array> { |
| 16 | + return Buffer.from(this.hash); |
| 17 | + } |
| 18 | + reset(): void { |
| 19 | + throw new Error("Function not implemented."); |
| 20 | + } |
| 21 | + update(chunk: Uint8Array): void { |
| 22 | + this.hash += toUtf8(chunk); |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + const canonicalData = new Uint8Array("abcdefghijklmnopqrstuvwxyz".split("").map((_) => _.charCodeAt(0))); |
| 27 | + |
| 28 | + const canonicalUtf8 = toUtf8(canonicalData); |
| 29 | + const canonicalBase64 = toBase64(canonicalUtf8); |
| 30 | + |
| 31 | + describe(createChecksumStream.name + " webstreams API", () => { |
| 32 | + if (typeof ReadableStream !== "function") { |
| 33 | + // test not applicable to Node.js 16. |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + const makeStream = () => { |
| 38 | + return new ReadableStream({ |
| 39 | + start(controller) { |
| 40 | + canonicalData.forEach((byte) => { |
| 41 | + controller.enqueue(new Uint8Array([byte])); |
| 42 | + }); |
| 43 | + controller.close(); |
| 44 | + }, |
| 45 | + }); |
| 46 | + }; |
| 47 | + |
| 48 | + it("should extend a ReadableStream", async () => { |
| 49 | + const stream = makeStream(); |
| 50 | + const checksumStream = createChecksumStream({ |
| 51 | + expectedChecksum: canonicalBase64, |
| 52 | + checksum: new Appender(), |
| 53 | + checksumSourceLocation: "my-header", |
| 54 | + source: stream, |
| 55 | + }); |
| 56 | + |
| 57 | + expect(checksumStream).toBeInstanceOf(ReadableStream); |
| 58 | + expect(checksumStream).toBeInstanceOf(ChecksumStreamWeb); |
| 59 | + |
| 60 | + const collected = toUtf8(await headStream(checksumStream, Infinity)); |
| 61 | + expect(collected).toEqual(canonicalUtf8); |
| 62 | + expect(stream.locked).toEqual(true); |
| 63 | + |
| 64 | + // expectation is that it is resolved. |
| 65 | + expect(await checksumStream.getReader().closed); |
| 66 | + }); |
| 67 | + |
| 68 | + it("should throw during stream read if the checksum does not match", async () => { |
| 69 | + const stream = makeStream(); |
| 70 | + const checksumStream = createChecksumStream({ |
| 71 | + expectedChecksum: "different-expected-checksum", |
| 72 | + checksum: new Appender(), |
| 73 | + checksumSourceLocation: "my-header", |
| 74 | + source: stream, |
| 75 | + }); |
| 76 | + |
| 77 | + try { |
| 78 | + toUtf8(await headStream(checksumStream, Infinity)); |
| 79 | + throw new Error("stream was read successfully"); |
| 80 | + } catch (e: unknown) { |
| 81 | + expect(String(e)).toEqual( |
| 82 | + `Error: Checksum mismatch: expected "different-expected-checksum" but` + |
| 83 | + ` received "${canonicalBase64}"` + |
| 84 | + ` in response header "my-header".` |
| 85 | + ); |
| 86 | + } |
| 87 | + }); |
| 88 | + }); |
| 89 | +}); |
0 commit comments