Skip to content

Commit

Permalink
fix(middleware-apply-body-checksum): fix request copying with clone
Browse files Browse the repository at this point in the history
  • Loading branch information
Steven Yuan committed Jul 2, 2024
1 parent 8654804 commit e225edb
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/thick-hats-explode.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@smithy/middleware-apply-body-checksum": patch
---

Fix request copying with `HttpRequest.clone()`.
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Check failure on line 33 in packages/middleware-apply-body-checksum/src/applyMd5BodyChecksumMiddleware.ts

View workflow job for this annotation

GitHub Actions / TypeScript Lint

'cloned' is never reassigned. Use 'const' instead
cloned.headers = {
...headers,
"content-md5": options.base64Encoder(await digest),
};
request = cloned;
}
}
return next({
Expand Down

0 comments on commit e225edb

Please sign in to comment.