Skip to content

Commit

Permalink
test(util-stream-node): getAwsChunkedEncodingStream (#3364)
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr authored Feb 28, 2022
1 parent 15ed586 commit c013190
Showing 1 changed file with 123 additions and 0 deletions.
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();
});
});
});

0 comments on commit c013190

Please sign in to comment.