Skip to content

Commit c83395b

Browse files
committed
test(client-s3): add requestChecksumCalculation for stream
1 parent ef71b11 commit c83395b

File tree

1 file changed

+24
-10
lines changed

1 file changed

+24
-10
lines changed

clients/client-s3/test/unit/flexibleChecksums.spec.ts

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ describe("Flexible Checksums", () => {
5555
return readableStream;
5656
};
5757

58-
it(`when body is sent as a request`, async () => {
58+
it(`when body is sent as a string`, async () => {
5959
const requestChecksumValidator: BuildMiddleware<any, any> = (next) => async (args) => {
6060
// middleware intercept the request and return it early
6161
const request = args.request as HttpRequest;
@@ -70,10 +70,10 @@ describe("Flexible Checksums", () => {
7070
expect(headers["x-amz-sdk-checksum-algorithm"]).toBeUndefined();
7171
expect(headers[checksumHeader]).toBeUndefined();
7272
} else {
73-
expect(headers["x-amz-sdk-checksum-algorithm"]).to.equal(
73+
expect(headers["x-amz-sdk-checksum-algorithm"]).toEqual(
7474
checksumAlgorithm ?? DEFAULT_CHECKSUM_ALGORITHM
7575
);
76-
expect(headers[checksumHeader]).to.equal(checksumValue);
76+
expect(headers[checksumHeader]).toEqual(checksumValue);
7777
}
7878

7979
return { output: {} as any, response: {} as any };
@@ -105,16 +105,29 @@ describe("Flexible Checksums", () => {
105105
// middleware intercept the request and return it early
106106
const request = args.request as HttpRequest;
107107
const { headers, body } = request;
108-
expect(headers["content-length"]).to.be.undefined;
109-
expect(headers["content-encoding"]).to.equal("aws-chunked");
110-
expect(headers["transfer-encoding"]).to.equal("chunked");
111-
expect(headers["x-amz-content-sha256"]).to.equal("STREAMING-UNSIGNED-PAYLOAD-TRAILER");
112-
expect(headers["x-amz-trailer"]).to.equal(checksumHeader);
108+
expect(headers["content-length"]).toBeUndefined();
109+
110+
// Headers are not set when checksumAlgorithm is not provided,
111+
// and requestChecksumCalculation is explicitly set to WHEN_SUPPORTED.
112+
if (
113+
checksumAlgorithm === undefined &&
114+
requestChecksumCalculation === RequestChecksumCalculation.WHEN_REQUIRED
115+
) {
116+
expect(headers["content-encoding"]).toBeUndefined();
117+
expect(headers["transfer-encoding"]).toBeUndefined();
118+
expect(headers["x-amz-content-sha256"]).toBeUndefined();
119+
expect(headers["x-amz-trailer"]).toBeUndefined();
120+
} else {
121+
expect(headers["content-encoding"]).toEqual("aws-chunked");
122+
expect(headers["transfer-encoding"]).toEqual("chunked");
123+
expect(headers["x-amz-content-sha256"]).toEqual("STREAMING-UNSIGNED-PAYLOAD-TRAILER");
124+
expect(headers["x-amz-trailer"]).toEqual(checksumHeader);
125+
}
113126
body.on("data", (data: any) => {
114127
const stringValue = data.toString();
115128
if (stringValue.startsWith(checksumHeader)) {
116129
const receivedChecksum = stringValue.replace("\r\n", "").split(":")[1];
117-
expect(receivedChecksum).to.equal(checksumValue);
130+
expect(receivedChecksum).toEqual(checksumValue);
118131
}
119132
});
120133
return { output: {} as any, response: {} as any };
@@ -126,6 +139,7 @@ describe("Flexible Checksums", () => {
126139
accessKeyId: "CLIENT_TEST",
127140
secretAccessKey: "CLIENT_TEST",
128141
},
142+
requestChecksumCalculation,
129143
});
130144
client.middlewareStack.addRelativeTo(requestChecksumValidator, {
131145
relation: "after",
@@ -207,7 +221,7 @@ describe("Flexible Checksums", () => {
207221
ChecksumMode: checksumAlgorithm ? "ENABLED" : undefined,
208222
});
209223
(Body as Readable).on("data", (chunk) => {
210-
expect(chunk.toString()).to.equal(body);
224+
expect(chunk.toString()).toEqual(body);
211225
});
212226
}
213227
);

0 commit comments

Comments
 (0)