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: move flexible checksums integration tests #6752

Merged
merged 1 commit into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
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
170 changes: 0 additions & 170 deletions clients/client-s3/test/unit/flexibleChecksums.spec.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ChecksumAlgorithm, S3 } from "@aws-sdk/client-s3";
import { Transform } from "stream";
import { HttpHandler, HttpRequest, HttpResponse } from "@smithy/protocol-http";
import { Readable, Transform } from "stream";
import { describe, expect, test as it } from "vitest";

import { requireRequestsFrom } from "../../../private/aws-util-test/src";
Expand All @@ -13,87 +14,134 @@ describe("middleware-flexible-checksums", () => {
error() {},
};

const testCases: [string, ChecksumAlgorithm, string][] = [
["", ChecksumAlgorithm.CRC32, "AAAAAA=="],
["abc", ChecksumAlgorithm.CRC32, "NSRBwg=="],
["Hello world", ChecksumAlgorithm.CRC32, "i9aeUg=="],

["", ChecksumAlgorithm.CRC32C, "AAAAAA=="],
["abc", ChecksumAlgorithm.CRC32C, "Nks/tw=="],
["Hello world", ChecksumAlgorithm.CRC32C, "crUfeA=="],

["", ChecksumAlgorithm.SHA1, "2jmj7l5rSw0yVb/vlWAYkK/YBwk="],
["abc", ChecksumAlgorithm.SHA1, "qZk+NkcGgWq6PiVxeFDCbJzQ2J0="],
["Hello world", ChecksumAlgorithm.SHA1, "e1AsOh9IyGCa4hLN+2Od7jlnP14="],

["", ChecksumAlgorithm.SHA256, "47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU="],
["abc", ChecksumAlgorithm.SHA256, "ungWv48Bz+pBQUDeXa4iI7ADYaOWF3qctBD/YfIAFa0="],
["Hello world", ChecksumAlgorithm.SHA256, "ZOyIygCyaOW6GjVnihtTFtIS9PNmskdyMlNKiuyjfzw="],
];

describe(S3.name, () => {
it("should set flexible checksums (SHA256)", async () => {
const client = new S3({ region: "us-west-2", logger });
const client = new S3({ region: "us-west-2", logger });

requireRequestsFrom(client).toMatch({
method: "PUT",
hostname: "s3.us-west-2.amazonaws.com",
protocol: "https:",
path: "/b/k",
headers: {
"content-type": "application/octet-stream",
"x-amz-sdk-checksum-algorithm": "SHA256",
"content-length": "4",
Expect: "100-continue",
"x-amz-checksum-sha256": "iNQmb9TmM40TuEX88olXnSCciXgjuSF9o+Fhk28DFYk=",
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": "88d4266fd4e6338d13b845fcf289579d209c897823b9217da3e161936f031589",
authorization: /./,
},
query: {
"x-id": "PutObject",
},
});
describe("putObject", () => {
testCases.forEach(([body, checksumAlgorithm, checksumValue]) => {
const checksumHeader = `x-amz-checksum-${checksumAlgorithm.toLowerCase()}`;

await client.putObject({
Bucket: "b",
Key: "k",
Body: Buffer.from("abcd"),
ChecksumAlgorithm: "SHA256",
});
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",
},
});

expect.hasAssertions();
await client.putObject({
Bucket: "b",
Key: "k",
Body: body,
ChecksumAlgorithm: checksumAlgorithm,
});

expect.hasAssertions();
});
});
});

it("should set flexible checksums (SHA1)", async () => {
const client = new S3({ region: "us-west-2", logger });
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 {};
}
})(),
});

requireRequestsFrom(client).toMatch({
method: "PUT",
hostname: "s3.us-west-2.amazonaws.com",
protocol: "https:",
path: "/b/k",
headers: {
"content-type": "application/octet-stream",
"x-amz-sdk-checksum-algorithm": "SHA1",
"content-length": "4",
Expect: "100-continue",
"x-amz-checksum-sha1": "gf6L/odXbD7LIkJvjleEc4KRes8=",
host: "s3.us-west-2.amazonaws.com",
"x-amz-user-agent": /./,
"user-agent": /./,
"amz-sdk-invocation-id": /./,
"amz-sdk-request": /./,
"x-amz-date": /./,
authorization: /./,
// this is here even if algo is SHA1
"x-amz-content-sha256": "88d4266fd4e6338d13b845fcf289579d209c897823b9217da3e161936f031589",
},
query: {
"x-id": "PutObject",
},
});
const response = await client.getObject({
Bucket: "b",
Key: "k",
ChecksumMode: "ENABLED",
});

await client.putObject({
Bucket: "b",
Key: "k",
Body: Buffer.from("abcd"),
ChecksumAlgorithm: "SHA1",
await expect(response.Body?.transformToString()).resolves.toEqual(body);
});
});

expect.hasAssertions();
});

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 @@ -134,8 +182,6 @@ 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
Loading