Skip to content

Commit

Permalink
chore(middleware-apply-body-checksum): request copying with clone met…
Browse files Browse the repository at this point in the history
…hod (#1324)
  • Loading branch information
syall authored Jul 18, 2024
1 parent 9d22987 commit 9624938
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 11 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 @@ -17,7 +17,7 @@ export const applyMd5BodyChecksumMiddleware =
(options: Md5BodyChecksumResolvedConfig): BuildMiddleware<any, any> =>
<Output extends MetadataBearer>(next: BuildHandler<any, Output>): BuildHandler<any, Output> =>
async (args: BuildHandlerArguments<any>): Promise<BuildHandlerOutput<Output>> => {
let { request } = args;
const { request } = args;
if (HttpRequest.isInstance(request)) {
const { body, headers } = request;
if (!hasHeader("content-md5", headers)) {
Expand All @@ -30,19 +30,18 @@ export const applyMd5BodyChecksumMiddleware =
digest = options.streamHasher(options.md5, body);
}

request = {
...request,
headers: {
...headers,
"content-md5": options.base64Encoder(await digest),
},
const cloned = HttpRequest.clone(request);
cloned.headers = {
...headers,
"content-md5": options.base64Encoder(await digest),
};
return next({
...args,
request: cloned,
});
}
}
return next({
...args,
request,
});
return next(args);
};

export const applyMd5BodyChecksumMiddlewareOptions: BuildHandlerOptions = {
Expand Down

0 comments on commit 9624938

Please # to comment.