Skip to content

test(client-s3): use each() and jest APIs in flexibleChecksums #6684

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

Closed
wants to merge 1 commit into from
Closed
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
47 changes: 25 additions & 22 deletions clients/client-s3/test/unit/flexibleChecksums.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { describe, expect, test as it } from "vitest";
import { ChecksumAlgorithm as Algo, S3 } from "../../src/index";

describe("Flexible Checksums", () => {
const testCases = [
const testCases: [string, string, string][] = [
["", ChecksumAlgorithm.CRC32, "AAAAAA=="],
["abc", ChecksumAlgorithm.CRC32, "NSRBwg=="],
["Hello world", ChecksumAlgorithm.CRC32, "i9aeUg=="],
Expand All @@ -26,10 +26,10 @@ describe("Flexible Checksums", () => {
];

describe("putObject", () => {
testCases.forEach(([body, checksumAlgorithm, checksumValue]) => {
const checksumHeader = `x-amz-checksum-${checksumAlgorithm.toLowerCase()}`;

describe(`sets ${checksumHeader}="${checksumValue}"" for checksum="${checksumAlgorithm}"`, () => {
describe.each(testCases)(
`for body="%s" and checksumAlgorithm="%s", sets checksum="%s"`,
(body, checksumAlgorithm, checksumValue) => {
const checksumHeader = `x-amz-checksum-${checksumAlgorithm.toLowerCase()}`;
const getBodyAsReadableStream = (content: string) => {
const readableStream = new Readable();
const separator = " ";
Expand All @@ -44,13 +44,15 @@ describe("Flexible Checksums", () => {
return readableStream;
};

it(`when body is sent as a request`, async () => {
it(`when body is sent as a string`, async () => {
const requestChecksumValidator: BuildMiddleware<any, any> = (next) => async (args) => {
// middleware intercept the request and return it early
const request = args.request as HttpRequest;
const { headers } = request;
expect(headers["x-amz-sdk-checksum-algorithm"]).to.equal(checksumAlgorithm);
expect(headers[checksumHeader]).to.equal(checksumValue);

expect(headers["x-amz-sdk-checksum-algorithm"]).toEqual(checksumAlgorithm);
expect(headers[checksumHeader]).toEqual(checksumValue);

return { output: {} as any, response: {} as any };
};

Expand Down Expand Up @@ -79,16 +81,16 @@ describe("Flexible Checksums", () => {
// middleware intercept the request and return it early
const request = args.request as HttpRequest;
const { headers, body } = request;
expect(headers["content-length"]).to.be.undefined;
expect(headers["content-encoding"]).to.equal("aws-chunked");
expect(headers["transfer-encoding"]).to.equal("chunked");
expect(headers["x-amz-content-sha256"]).to.equal("STREAMING-UNSIGNED-PAYLOAD-TRAILER");
expect(headers["x-amz-trailer"]).to.equal(checksumHeader);
expect(headers["content-length"]).toBeUndefined();
expect(headers["content-encoding"]).toEqual("aws-chunked");
expect(headers["transfer-encoding"]).toEqual("chunked");
expect(headers["x-amz-content-sha256"]).toEqual("STREAMING-UNSIGNED-PAYLOAD-TRAILER");
expect(headers["x-amz-trailer"]).toEqual(checksumHeader);
body.on("data", (data: any) => {
const stringValue = data.toString();
if (stringValue.startsWith(checksumHeader)) {
const receivedChecksum = stringValue.replace("\r\n", "").split(":")[1];
expect(receivedChecksum).to.equal(checksumValue);
expect(receivedChecksum).toEqual(checksumValue);
}
});
return { output: {} as any, response: {} as any };
Expand All @@ -114,15 +116,16 @@ describe("Flexible Checksums", () => {
ChecksumAlgorithm: checksumAlgorithm as Algo,
});
});
});
});
}
);
});

describe("getObject", async () => {
testCases.forEach(([body, checksumAlgorithm, checksumValue]) => {
const checksumHeader = `x-amz-checksum-${checksumAlgorithm.toLowerCase()}`;
it.each(testCases)(
`for body="%s" and checksumAlgorithm="%s", validates ChecksumMode`,
async (body, checksumAlgorithm, checksumValue) => {
const checksumHeader = `x-amz-checksum-${checksumAlgorithm.toLowerCase()}`;

it(`validates ${checksumHeader}="${checksumValue}"" set for checksum="${checksumAlgorithm}"`, async () => {
const responseBody = new Readable();
responseBody.push(body);
responseBody.push(null);
Expand Down Expand Up @@ -162,9 +165,9 @@ describe("Flexible Checksums", () => {
ChecksumMode: "ENABLED",
});
(Body as Readable).on("data", (chunk) => {
expect(chunk.toString()).to.equal(body);
expect(chunk.toString()).toEqual(body);
});
});
});
}
);
});
});
Loading