Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(util-stream-node): getAwsChunkedEncodingStream #3364

Merged
merged 5 commits into from
Feb 28, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 123 additions & 0 deletions packages/util-stream-node/src/getAwsChunkedEncodingStream.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import { Readable } from "stream";

import { getAwsChunkedEncodingStream } from "./getAwsChunkedEncodingStream";

describe(getAwsChunkedEncodingStream.name, () => {
const mockBase64Encoder = jest.fn();
const mockBodyLengthChecker = jest.fn();
const mockChecksumAlgorithmFn = jest.fn();
const mockChecksumLocationName = "mockChecksumLocationName";
const mockStreamHasher = jest.fn();

const mockOptions = {
base64Encoder: mockBase64Encoder,
bodyLengthChecker: mockBodyLengthChecker,
checksumAlgorithmFn: mockChecksumAlgorithmFn,
checksumLocationName: mockChecksumLocationName,
streamHasher: mockStreamHasher,
};

const mockChecksum = "mockChecksum";
const mockRawChecksum = Buffer.from(mockChecksum);
const mockStreamChunks = ["Hello", "World"];
const mockBodyLength = 5;

const getMockReadableStream = () => {
const readableStream = new Readable();
mockStreamChunks.forEach((chunk) => {
readableStream.push(chunk);
});
readableStream.push(null);
return readableStream;
};

beforeEach(() => {
mockStreamHasher.mockResolvedValue(mockRawChecksum);
mockBase64Encoder.mockReturnValue(mockChecksum);
mockBodyLengthChecker.mockReturnValue(mockBodyLength);
});

afterEach(() => {
expect(mockBodyLengthChecker).toHaveBeenCalledTimes(mockStreamChunks.length);
mockStreamChunks.forEach((chunk, index) => {
expect(mockBodyLengthChecker).toHaveBeenNthCalledWith(index + 1, Buffer.from(chunk));
});
jest.clearAllMocks();
});

describe("skips checksum computation", () => {
const validateStreamWithoutChecksum = (awsChunkedEncodingStream: Readable, done: Function) => {
let dataEventCount = 0;
awsChunkedEncodingStream.on("data", (data) => {
if (dataEventCount === mockStreamChunks.length) {
expect(data.toString()).toStrictEqual(`0\r\n`);
} else {
expect(data.toString()).toStrictEqual(
`${mockBodyLength}\r\n${mockStreamChunks[dataEventCount].toString()}\r\n`
);
}
dataEventCount++;
});
awsChunkedEncodingStream.on("end", () => {
expect(mockStreamHasher).not.toHaveBeenCalled();
expect(mockBase64Encoder).not.toHaveBeenCalled();
expect(dataEventCount).toStrictEqual(mockStreamChunks.length + 1);
done();
});
};

it("if none of the required options are passed", (done) => {
const readableStream = getMockReadableStream();
const awsChunkedEncodingStream = getAwsChunkedEncodingStream(readableStream, {
bodyLengthChecker: mockBodyLengthChecker,
});
validateStreamWithoutChecksum(awsChunkedEncodingStream, done);
});

["base64Encoder", "checksumAlgorithmFn", "checksumLocationName", "streamHasher"].forEach((optionToRemove) => {
it(`if option '${optionToRemove}' is not passed`, (done) => {
const readableStream = getMockReadableStream();
const awsChunkedEncodingStream = getAwsChunkedEncodingStream(readableStream, {
...mockOptions,
[optionToRemove]: undefined,
});
validateStreamWithoutChecksum(awsChunkedEncodingStream, done);
});
});
});

it("computes checksum and adds it to the end event", (done) => {
const readableStream = getMockReadableStream();
const awsChunkedEncodingStream = getAwsChunkedEncodingStream(readableStream, mockOptions);

let dataEventCount = 0;
awsChunkedEncodingStream.on("data", (data) => {
if (dataEventCount >= mockStreamChunks.length) {
switch (dataEventCount) {
case mockStreamChunks.length:
expect(data.toString()).toStrictEqual(`0\r\n`);
break;
case mockStreamChunks.length + 1:
expect(data.toString()).toStrictEqual(`${mockChecksumLocationName}:${mockChecksum}\r\n`);
break;
case mockStreamChunks.length + 2:
expect(data.toString()).toStrictEqual(`\r\n`);
break;
default:
throw Error("Code shouldn't reach here");
}
} else {
expect(data.toString()).toStrictEqual(
`${mockBodyLength}\r\n${mockStreamChunks[dataEventCount].toString()}\r\n`
);
}
dataEventCount++;
});
awsChunkedEncodingStream.on("end", () => {
expect(mockStreamHasher).toHaveBeenCalledWith(mockChecksumAlgorithmFn, readableStream);
expect(mockBase64Encoder).toHaveBeenCalledWith(mockRawChecksum);
expect(dataEventCount).toStrictEqual(mockStreamChunks.length + 3);
done();
});
});
});