From d6af53c6f56968e87e0d3f91bb63de283d4522ea Mon Sep 17 00:00:00 2001 From: Trivikram Kamat <16024985+trivikr@users.noreply.github.com> Date: Fri, 20 Dec 2024 02:51:45 +0000 Subject: [PATCH] test: update middleware-flexible-checksums.integ.spec.ts --- ...iddleware-flexible-checksums.integ.spec.ts | 233 ++++++++++-------- 1 file changed, 132 insertions(+), 101 deletions(-) diff --git a/packages/middleware-flexible-checksums/src/middleware-flexible-checksums.integ.spec.ts b/packages/middleware-flexible-checksums/src/middleware-flexible-checksums.integ.spec.ts index 1512a76b53f5..ba9a9ef4ceb9 100644 --- a/packages/middleware-flexible-checksums/src/middleware-flexible-checksums.integ.spec.ts +++ b/packages/middleware-flexible-checksums/src/middleware-flexible-checksums.integ.spec.ts @@ -4,6 +4,7 @@ import { Readable, Transform } from "stream"; import { describe, expect, test as it } from "vitest"; import { requireRequestsFrom } from "../../../private/aws-util-test/src"; +import { DEFAULT_CHECKSUM_ALGORITHM, RequestChecksumCalculation, ResponseChecksumValidation } from "./constants"; describe("middleware-flexible-checksums", () => { const logger = { @@ -14,7 +15,7 @@ describe("middleware-flexible-checksums", () => { error() {}, }; - const testCases: [string, ChecksumAlgorithm, string][] = [ + const testCases: [string, ChecksumAlgorithm | undefined, string][] = [ ["", ChecksumAlgorithm.CRC32, "AAAAAA=="], ["abc", ChecksumAlgorithm.CRC32, "NSRBwg=="], ["Hello world", ChecksumAlgorithm.CRC32, "i9aeUg=="], @@ -30,118 +31,146 @@ describe("middleware-flexible-checksums", () => { ["", ChecksumAlgorithm.SHA256, "47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU="], ["abc", ChecksumAlgorithm.SHA256, "ungWv48Bz+pBQUDeXa4iI7ADYaOWF3qctBD/YfIAFa0="], ["Hello world", ChecksumAlgorithm.SHA256, "ZOyIygCyaOW6GjVnihtTFtIS9PNmskdyMlNKiuyjfzw="], + + // Choose default checksum algorithm when explicily not provided. + ["", undefined, "AAAAAA=="], + ["abc", undefined, "NSRBwg=="], + ["Hello world", undefined, "i9aeUg=="], ]; describe(S3.name, () => { - const client = new S3({ region: "us-west-2", logger }); - describe("putObject", () => { - testCases.forEach(([body, checksumAlgorithm, checksumValue]) => { - const checksumHeader = `x-amz-checksum-${checksumAlgorithm.toLowerCase()}`; - - it(`sets ${checksumHeader}="${checksumValue}"" for checksum="${checksumAlgorithm}"`, async () => { - requireRequestsFrom(client).toMatch({ - method: "PUT", - hostname: "s3.us-west-2.amazonaws.com", - protocol: "https:", - path: "/b/k", - headers: { - "content-type": "application/octet-stream", - ...(body.length - ? { - "content-length": body.length.toString(), - Expect: "100-continue", - } - : {}), - "x-amz-sdk-checksum-algorithm": checksumAlgorithm, - [checksumHeader]: checksumValue, - host: "s3.us-west-2.amazonaws.com", - "x-amz-user-agent": /./, - "user-agent": /./, - "amz-sdk-invocation-id": /./, - "amz-sdk-request": /./, - "x-amz-date": /./, - "x-amz-content-sha256": /./, - authorization: /./, - }, - query: { - "x-id": "PutObject", - }, + describe.each([undefined, RequestChecksumCalculation.WHEN_SUPPORTED, RequestChecksumCalculation.WHEN_REQUIRED])( + `when requestChecksumCalculation='%s'`, + (requestChecksumCalculation) => { + testCases.forEach(([body, checksumAlgorithm, checksumValue]) => { + const client = new S3({ region: "us-west-2", logger, requestChecksumCalculation }); + const checksumHeader = `x-amz-checksum-${(checksumAlgorithm ?? DEFAULT_CHECKSUM_ALGORITHM).toLowerCase()}`; + + it(`tests ${checksumHeader}="${checksumValue}"" for checksum="${checksumAlgorithm}"`, async () => { + requireRequestsFrom(client).toMatch({ + method: "PUT", + hostname: "s3.us-west-2.amazonaws.com", + protocol: "https:", + path: "/b/k", + headers: { + "content-type": "application/octet-stream", + ...(body.length + ? { + "content-length": body.length.toString(), + Expect: "100-continue", + } + : {}), + ...(requestChecksumCalculation === RequestChecksumCalculation.WHEN_REQUIRED && + checksumAlgorithm === undefined + ? {} + : { + "x-amz-sdk-checksum-algorithm": checksumAlgorithm, + [checksumHeader]: checksumValue, + }), + host: "s3.us-west-2.amazonaws.com", + "x-amz-user-agent": /./, + "user-agent": /./, + "amz-sdk-invocation-id": /./, + "amz-sdk-request": /./, + "x-amz-date": /./, + "x-amz-content-sha256": /./, + authorization: /./, + }, + query: { + "x-id": "PutObject", + }, + }); + + await client.putObject({ + Bucket: "b", + Key: "k", + Body: body, + ChecksumAlgorithm: checksumAlgorithm as ChecksumAlgorithm, + }); + + expect.hasAssertions(); + }); }); - - await client.putObject({ - Bucket: "b", - Key: "k", - Body: body, - ChecksumAlgorithm: checksumAlgorithm, - }); - - expect.hasAssertions(); - }); - }); + } + ); }); describe("getObject", () => { - testCases.forEach(([body, checksumAlgorithm, checksumValue]) => { - const checksumHeader = `x-amz-checksum-${checksumAlgorithm.toLowerCase()}`; - - it(`validates ${checksumHeader}="${checksumValue}"" set for checksum="${checksumAlgorithm}"`, async () => { - const client = new S3({ - region: "us-west-2", - logger, - requestHandler: new (class implements HttpHandler { - async handle(request: HttpRequest): Promise { - expect(request).toMatchObject({ - method: "GET", - hostname: "s3.us-west-2.amazonaws.com", - protocol: "https:", - path: "/b/k", - headers: { - "x-amz-checksum-mode": "ENABLED", - host: "s3.us-west-2.amazonaws.com", - "x-amz-user-agent": /./, - "user-agent": /./, - "amz-sdk-invocation-id": /./, - "amz-sdk-request": /./, - "x-amz-date": /./, - "x-amz-content-sha256": /./, - authorization: /./, - }, - query: { - "x-id": "GetObject", - }, - }); - return { - response: new HttpResponse({ - statusCode: 200, - headers: { - "content-type": "application/octet-stream", - "content-length": body.length.toString(), - [checksumHeader]: checksumValue, - }, - body: Readable.from([body]), - }), - }; - } - updateHttpClientConfig(key: never, value: never): void {} - httpHandlerConfigs() { - return {}; - } - })(), - }); - - const response = await client.getObject({ - Bucket: "b", - Key: "k", - ChecksumMode: "ENABLED", + describe.each([undefined, ResponseChecksumValidation.WHEN_SUPPORTED, ResponseChecksumValidation.WHEN_REQUIRED])( + `when responseChecksumValidation='%s'`, + (responseChecksumValidation) => { + testCases.forEach(([body, checksumAlgorithm, checksumValue]) => { + const checksumHeader = `x-amz-checksum-${(checksumAlgorithm ?? DEFAULT_CHECKSUM_ALGORITHM).toLowerCase()}`; + + it(`validates ${checksumHeader}="${checksumValue}"" for checksum="${checksumAlgorithm}"`, async () => { + const client = new S3({ + region: "us-west-2", + logger, + requestHandler: new (class implements HttpHandler { + async handle(request: HttpRequest): Promise { + expect(request).toMatchObject({ + method: "GET", + hostname: "s3.us-west-2.amazonaws.com", + protocol: "https:", + path: "/b/k", + headers: { + ...(responseChecksumValidation === ResponseChecksumValidation.WHEN_REQUIRED && + !checksumAlgorithm + ? {} + : { + "x-amz-checksum-mode": "ENABLED", + }), + host: "s3.us-west-2.amazonaws.com", + "x-amz-user-agent": /./, + "user-agent": /./, + "amz-sdk-invocation-id": /./, + "amz-sdk-request": /./, + "x-amz-date": /./, + "x-amz-content-sha256": /./, + authorization: /./, + }, + query: { + "x-id": "GetObject", + }, + }); + return { + response: new HttpResponse({ + statusCode: 200, + headers: { + "content-type": "application/octet-stream", + "content-length": body.length.toString(), + [checksumHeader]: checksumValue, + }, + body: Readable.from([body]), + }), + }; + } + updateHttpClientConfig(key: never, value: never): void {} + httpHandlerConfigs() { + return {}; + } + })(), + responseChecksumValidation, + }); + + const response = await client.getObject({ + Bucket: "b", + Key: "k", + // Do not pass ChecksumMode if algorithm is not explicitly defined. It'll be set by SDK. + ChecksumMode: checksumAlgorithm ? "ENABLED" : undefined, + }); + + await expect(response.Body?.transformToString()).resolves.toEqual(body); + }); }); - - await expect(response.Body?.transformToString()).resolves.toEqual(body); - }); - }); + } + ); }); it("should not set binary file content length", async () => { + const client = new S3({ region: "us-west-2", logger }); + requireRequestsFrom(client).toMatch({ method: "PUT", hostname: "s3.us-west-2.amazonaws.com", @@ -182,6 +211,8 @@ describe("middleware-flexible-checksums", () => { ["CRC32C", "V"], ].forEach(([algo, id]) => { it(`should feature-detect checksum ${algo}=${id}`, async () => { + const client = new S3({ region: "us-west-2", logger }); + requireRequestsFrom(client).toMatch({ headers: { "user-agent": new RegExp(`(.*?) m\/(.*?)${id}(.*?)$`),