diff --git a/.changeset/thick-hats-explode.md b/.changeset/thick-hats-explode.md new file mode 100644 index 00000000000..d41e16bf55a --- /dev/null +++ b/.changeset/thick-hats-explode.md @@ -0,0 +1,5 @@ +--- +"@smithy/middleware-apply-body-checksum": patch +--- + +Fix request copying with `HttpRequest.clone()`. diff --git a/packages/middleware-apply-body-checksum/src/applyMd5BodyChecksumMiddleware.spec.ts b/packages/middleware-apply-body-checksum/src/applyMd5BodyChecksumMiddleware.spec.ts index 31bf6a45111..f2f28732455 100644 --- a/packages/middleware-apply-body-checksum/src/applyMd5BodyChecksumMiddleware.spec.ts +++ b/packages/middleware-apply-body-checksum/src/applyMd5BodyChecksumMiddleware.spec.ts @@ -73,6 +73,27 @@ describe("applyMd5BodyChecksumMiddleware", () => { expect(mockHashDigest.mock.calls.length).toBe(0); expect(mockEncoder.mock.calls.length).toBe(0); }); + + it("should clone the request when applying the checksum", async () => { + const handler = applyMd5BodyChecksumMiddleware({ + md5: MockHash, + base64Encoder: mockEncoder, + // eslint-disable-next-line @typescript-eslint/no-unused-vars + streamHasher: async (stream: ExoticStream) => new Uint8Array(5), + })(next, {} as any); + + await handler({ + input: {}, + request: new HttpRequest({ + body: body + }), + }); + + expect(next.mock.calls.length).toBe(1); + const { request } = next.mock.calls[0][0]; + // Assert that non-enumerable properties like the method `clone()` are preserved. + expect(request.clone).toBeDefined(); + }); } it("should use the supplied stream hasher to calculate the hash of a streaming body", async () => { diff --git a/packages/middleware-apply-body-checksum/src/applyMd5BodyChecksumMiddleware.ts b/packages/middleware-apply-body-checksum/src/applyMd5BodyChecksumMiddleware.ts index 1abd166a10f..09c7307cb16 100644 --- a/packages/middleware-apply-body-checksum/src/applyMd5BodyChecksumMiddleware.ts +++ b/packages/middleware-apply-body-checksum/src/applyMd5BodyChecksumMiddleware.ts @@ -30,13 +30,12 @@ export const applyMd5BodyChecksumMiddleware = digest = options.streamHasher(options.md5, body); } - request = { - ...request, - headers: { - ...headers, - "content-md5": options.base64Encoder(await digest), - }, + let cloned = request.clone(); + cloned.headers = { + ...headers, + "content-md5": options.base64Encoder(await digest), }; + request = cloned; } } return next({