Skip to content

Commit e225edb

Browse files
author
Steven Yuan
committed
fix(middleware-apply-body-checksum): fix request copying with clone
1 parent 8654804 commit e225edb

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

.changeset/thick-hats-explode.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@smithy/middleware-apply-body-checksum": patch
3+
---
4+
5+
Fix request copying with `HttpRequest.clone()`.

packages/middleware-apply-body-checksum/src/applyMd5BodyChecksumMiddleware.spec.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,27 @@ describe("applyMd5BodyChecksumMiddleware", () => {
7373
expect(mockHashDigest.mock.calls.length).toBe(0);
7474
expect(mockEncoder.mock.calls.length).toBe(0);
7575
});
76+
77+
it("should clone the request when applying the checksum", async () => {
78+
const handler = applyMd5BodyChecksumMiddleware({
79+
md5: MockHash,
80+
base64Encoder: mockEncoder,
81+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
82+
streamHasher: async (stream: ExoticStream) => new Uint8Array(5),
83+
})(next, {} as any);
84+
85+
await handler({
86+
input: {},
87+
request: new HttpRequest({
88+
body: body
89+
}),
90+
});
91+
92+
expect(next.mock.calls.length).toBe(1);
93+
const { request } = next.mock.calls[0][0];
94+
// Assert that non-enumerable properties like the method `clone()` are preserved.
95+
expect(request.clone).toBeDefined();
96+
});
7697
}
7798

7899
it("should use the supplied stream hasher to calculate the hash of a streaming body", async () => {

packages/middleware-apply-body-checksum/src/applyMd5BodyChecksumMiddleware.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,12 @@ export const applyMd5BodyChecksumMiddleware =
3030
digest = options.streamHasher(options.md5, body);
3131
}
3232

33-
request = {
34-
...request,
35-
headers: {
36-
...headers,
37-
"content-md5": options.base64Encoder(await digest),
38-
},
33+
let cloned = request.clone();
34+
cloned.headers = {
35+
...headers,
36+
"content-md5": options.base64Encoder(await digest),
3937
};
38+
request = cloned;
4039
}
4140
}
4241
return next({

0 commit comments

Comments
 (0)