Skip to content

Commit c450f57

Browse files
committed
test: update middleware-flexible-checksums.integ.spec.ts
1 parent 4c0514b commit c450f57

File tree

1 file changed

+132
-101
lines changed

1 file changed

+132
-101
lines changed

packages/middleware-flexible-checksums/src/middleware-flexible-checksums.integ.spec.ts

Lines changed: 132 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Readable, Transform } from "stream";
44
import { describe, expect, test as it } from "vitest";
55

66
import { requireRequestsFrom } from "../../../private/aws-util-test/src";
7+
import { DEFAULT_CHECKSUM_ALGORITHM, RequestChecksumCalculation, ResponseChecksumValidation } from "./constants";
78

89
describe("middleware-flexible-checksums", () => {
910
const logger = {
@@ -14,7 +15,7 @@ describe("middleware-flexible-checksums", () => {
1415
error() {},
1516
};
1617

17-
const testCases: [string, ChecksumAlgorithm, string][] = [
18+
const testCases: [string, ChecksumAlgorithm | undefined, string][] = [
1819
["", ChecksumAlgorithm.CRC32, "AAAAAA=="],
1920
["abc", ChecksumAlgorithm.CRC32, "NSRBwg=="],
2021
["Hello world", ChecksumAlgorithm.CRC32, "i9aeUg=="],
@@ -30,118 +31,146 @@ describe("middleware-flexible-checksums", () => {
3031
["", ChecksumAlgorithm.SHA256, "47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU="],
3132
["abc", ChecksumAlgorithm.SHA256, "ungWv48Bz+pBQUDeXa4iI7ADYaOWF3qctBD/YfIAFa0="],
3233
["Hello world", ChecksumAlgorithm.SHA256, "ZOyIygCyaOW6GjVnihtTFtIS9PNmskdyMlNKiuyjfzw="],
34+
35+
// Choose default checksum algorithm when explicily not provided.
36+
["", undefined, "AAAAAA=="],
37+
["abc", undefined, "NSRBwg=="],
38+
["Hello world", undefined, "i9aeUg=="],
3339
];
3440

3541
describe(S3.name, () => {
36-
const client = new S3({ region: "us-west-2", logger });
37-
3842
describe("putObject", () => {
39-
testCases.forEach(([body, checksumAlgorithm, checksumValue]) => {
40-
const checksumHeader = `x-amz-checksum-${checksumAlgorithm.toLowerCase()}`;
41-
42-
it(`sets ${checksumHeader}="${checksumValue}"" for checksum="${checksumAlgorithm}"`, async () => {
43-
requireRequestsFrom(client).toMatch({
44-
method: "PUT",
45-
hostname: "s3.us-west-2.amazonaws.com",
46-
protocol: "https:",
47-
path: "/b/k",
48-
headers: {
49-
"content-type": "application/octet-stream",
50-
...(body.length
51-
? {
52-
"content-length": body.length.toString(),
53-
Expect: "100-continue",
54-
}
55-
: {}),
56-
"x-amz-sdk-checksum-algorithm": checksumAlgorithm,
57-
[checksumHeader]: checksumValue,
58-
host: "s3.us-west-2.amazonaws.com",
59-
"x-amz-user-agent": /./,
60-
"user-agent": /./,
61-
"amz-sdk-invocation-id": /./,
62-
"amz-sdk-request": /./,
63-
"x-amz-date": /./,
64-
"x-amz-content-sha256": /./,
65-
authorization: /./,
66-
},
67-
query: {
68-
"x-id": "PutObject",
69-
},
43+
describe.each([undefined, RequestChecksumCalculation.WHEN_SUPPORTED, RequestChecksumCalculation.WHEN_REQUIRED])(
44+
`when requestChecksumCalculation='%s'`,
45+
(requestChecksumCalculation) => {
46+
testCases.forEach(([body, checksumAlgorithm, checksumValue]) => {
47+
const client = new S3({ region: "us-west-2", logger, requestChecksumCalculation });
48+
const checksumHeader = `x-amz-checksum-${(checksumAlgorithm ?? DEFAULT_CHECKSUM_ALGORITHM).toLowerCase()}`;
49+
50+
it(`tests ${checksumHeader}="${checksumValue}"" for checksum="${checksumAlgorithm}"`, async () => {
51+
requireRequestsFrom(client).toMatch({
52+
method: "PUT",
53+
hostname: "s3.us-west-2.amazonaws.com",
54+
protocol: "https:",
55+
path: "/b/k",
56+
headers: {
57+
"content-type": "application/octet-stream",
58+
...(body.length
59+
? {
60+
"content-length": body.length.toString(),
61+
Expect: "100-continue",
62+
}
63+
: {}),
64+
...(requestChecksumCalculation === RequestChecksumCalculation.WHEN_REQUIRED &&
65+
checksumAlgorithm === undefined
66+
? {}
67+
: {
68+
"x-amz-sdk-checksum-algorithm": checksumAlgorithm,
69+
[checksumHeader]: checksumValue,
70+
}),
71+
host: "s3.us-west-2.amazonaws.com",
72+
"x-amz-user-agent": /./,
73+
"user-agent": /./,
74+
"amz-sdk-invocation-id": /./,
75+
"amz-sdk-request": /./,
76+
"x-amz-date": /./,
77+
"x-amz-content-sha256": /./,
78+
authorization: /./,
79+
},
80+
query: {
81+
"x-id": "PutObject",
82+
},
83+
});
84+
85+
await client.putObject({
86+
Bucket: "b",
87+
Key: "k",
88+
Body: body,
89+
ChecksumAlgorithm: checksumAlgorithm as ChecksumAlgorithm,
90+
});
91+
92+
expect.hasAssertions();
93+
});
7094
});
71-
72-
await client.putObject({
73-
Bucket: "b",
74-
Key: "k",
75-
Body: body,
76-
ChecksumAlgorithm: checksumAlgorithm,
77-
});
78-
79-
expect.hasAssertions();
80-
});
81-
});
95+
}
96+
);
8297
});
8398

8499
describe("getObject", () => {
85-
testCases.forEach(([body, checksumAlgorithm, checksumValue]) => {
86-
const checksumHeader = `x-amz-checksum-${checksumAlgorithm.toLowerCase()}`;
87-
88-
it(`validates ${checksumHeader}="${checksumValue}"" set for checksum="${checksumAlgorithm}"`, async () => {
89-
const client = new S3({
90-
region: "us-west-2",
91-
logger,
92-
requestHandler: new (class implements HttpHandler {
93-
async handle(request: HttpRequest): Promise<any> {
94-
expect(request).toMatchObject({
95-
method: "GET",
96-
hostname: "s3.us-west-2.amazonaws.com",
97-
protocol: "https:",
98-
path: "/b/k",
99-
headers: {
100-
"x-amz-checksum-mode": "ENABLED",
101-
host: "s3.us-west-2.amazonaws.com",
102-
"x-amz-user-agent": /./,
103-
"user-agent": /./,
104-
"amz-sdk-invocation-id": /./,
105-
"amz-sdk-request": /./,
106-
"x-amz-date": /./,
107-
"x-amz-content-sha256": /./,
108-
authorization: /./,
109-
},
110-
query: {
111-
"x-id": "GetObject",
112-
},
113-
});
114-
return {
115-
response: new HttpResponse({
116-
statusCode: 200,
117-
headers: {
118-
"content-type": "application/octet-stream",
119-
"content-length": body.length.toString(),
120-
[checksumHeader]: checksumValue,
121-
},
122-
body: Readable.from([body]),
123-
}),
124-
};
125-
}
126-
updateHttpClientConfig(key: never, value: never): void {}
127-
httpHandlerConfigs() {
128-
return {};
129-
}
130-
})(),
131-
});
132-
133-
const response = await client.getObject({
134-
Bucket: "b",
135-
Key: "k",
136-
ChecksumMode: "ENABLED",
100+
describe.each([undefined, ResponseChecksumValidation.WHEN_SUPPORTED, ResponseChecksumValidation.WHEN_REQUIRED])(
101+
`when responseChecksumValidation='%s'`,
102+
(responseChecksumValidation) => {
103+
testCases.forEach(([body, checksumAlgorithm, checksumValue]) => {
104+
const checksumHeader = `x-amz-checksum-${(checksumAlgorithm ?? DEFAULT_CHECKSUM_ALGORITHM).toLowerCase()}`;
105+
106+
it(`validates ${checksumHeader}="${checksumValue}"" for checksum="${checksumAlgorithm}"`, async () => {
107+
const client = new S3({
108+
region: "us-west-2",
109+
logger,
110+
requestHandler: new (class implements HttpHandler {
111+
async handle(request: HttpRequest): Promise<any> {
112+
expect(request).toMatchObject({
113+
method: "GET",
114+
hostname: "s3.us-west-2.amazonaws.com",
115+
protocol: "https:",
116+
path: "/b/k",
117+
headers: {
118+
...(responseChecksumValidation === ResponseChecksumValidation.WHEN_REQUIRED &&
119+
!checksumAlgorithm
120+
? {}
121+
: {
122+
"x-amz-checksum-mode": "ENABLED",
123+
}),
124+
host: "s3.us-west-2.amazonaws.com",
125+
"x-amz-user-agent": /./,
126+
"user-agent": /./,
127+
"amz-sdk-invocation-id": /./,
128+
"amz-sdk-request": /./,
129+
"x-amz-date": /./,
130+
"x-amz-content-sha256": /./,
131+
authorization: /./,
132+
},
133+
query: {
134+
"x-id": "GetObject",
135+
},
136+
});
137+
return {
138+
response: new HttpResponse({
139+
statusCode: 200,
140+
headers: {
141+
"content-type": "application/octet-stream",
142+
"content-length": body.length.toString(),
143+
[checksumHeader]: checksumValue,
144+
},
145+
body: Readable.from([body]),
146+
}),
147+
};
148+
}
149+
updateHttpClientConfig(key: never, value: never): void {}
150+
httpHandlerConfigs() {
151+
return {};
152+
}
153+
})(),
154+
responseChecksumValidation,
155+
});
156+
157+
const response = await client.getObject({
158+
Bucket: "b",
159+
Key: "k",
160+
// Do not pass ChecksumMode if algorithm is not explicitly defined. It'll be set by SDK.
161+
ChecksumMode: checksumAlgorithm ? "ENABLED" : undefined,
162+
});
163+
164+
await expect(response.Body?.transformToString()).resolves.toEqual(body);
165+
});
137166
});
138-
139-
await expect(response.Body?.transformToString()).resolves.toEqual(body);
140-
});
141-
});
167+
}
168+
);
142169
});
143170

144171
it("should not set binary file content length", async () => {
172+
const client = new S3({ region: "us-west-2", logger });
173+
145174
requireRequestsFrom(client).toMatch({
146175
method: "PUT",
147176
hostname: "s3.us-west-2.amazonaws.com",
@@ -182,6 +211,8 @@ describe("middleware-flexible-checksums", () => {
182211
["CRC32C", "V"],
183212
].forEach(([algo, id]) => {
184213
it(`should feature-detect checksum ${algo}=${id}`, async () => {
214+
const client = new S3({ region: "us-west-2", logger });
215+
185216
requireRequestsFrom(client).toMatch({
186217
headers: {
187218
"user-agent": new RegExp(`(.*?) m\/(.*?)${id}(.*?)$`),

0 commit comments

Comments
 (0)