Skip to content

Commit

Permalink
test: update middleware-flexible-checksums.integ.spec.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr committed Dec 20, 2024
1 parent 8ad8496 commit d6af53c
Showing 1 changed file with 132 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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=="],
Expand All @@ -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<any> {
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<any> {
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",
Expand Down Expand Up @@ -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}(.*?)$`),
Expand Down

0 comments on commit d6af53c

Please sign in to comment.