From 0b29652616c4c5b0fed896157538432d502e31a8 Mon Sep 17 00:00:00 2001 From: George Fu Date: Fri, 26 Jan 2024 15:15:12 -0500 Subject: [PATCH] fix(middleware-sdk-s3): do not warn when content-length is 0 (#5733) * fix(middleware-sdk-s3): do not warn when content-length is 0 * test(middleware-sdk-s3): test content-length 0 --- .../src/check-content-length-header.spec.ts | 18 ++++++++++++++++++ .../src/check-content-length-header.ts | 2 +- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/packages/middleware-sdk-s3/src/check-content-length-header.spec.ts b/packages/middleware-sdk-s3/src/check-content-length-header.spec.ts index 98f5703a2b0e..269b8e6cc59b 100644 --- a/packages/middleware-sdk-s3/src/check-content-length-header.spec.ts +++ b/packages/middleware-sdk-s3/src/check-content-length-header.spec.ts @@ -35,6 +35,24 @@ describe("checkContentLengthHeaderMiddleware", () => { ); }); + it("does not warn if uploading a payload of content-length 0", async () => { + const handler = checkContentLengthHeader()(mockNextHandler, {}); + + await handler({ + request: { + method: null, + protocol: null, + hostname: null, + path: null, + query: {}, + headers: { "content-length": 0 }, + }, + input: {}, + }); + + expect(spy).not.toHaveBeenCalled(); + }); + it("warns with console if logger is the default NoOpLogger", async () => { const handler = checkContentLengthHeader()(mockNextHandler, { logger: new NoOpLogger(), diff --git a/packages/middleware-sdk-s3/src/check-content-length-header.ts b/packages/middleware-sdk-s3/src/check-content-length-header.ts index 95cdb14e0e4d..ca45e78484bd 100644 --- a/packages/middleware-sdk-s3/src/check-content-length-header.ts +++ b/packages/middleware-sdk-s3/src/check-content-length-header.ts @@ -28,7 +28,7 @@ export function checkContentLengthHeader(): FinalizeRequestMiddleware const { request } = args; if (HttpRequest.isInstance(request)) { - if (!request.headers[CONTENT_LENGTH_HEADER]) { + if (!(CONTENT_LENGTH_HEADER in request.headers)) { const message = `Are you using a Stream of unknown length as the Body of a PutObject request? Consider using Upload instead from @aws-sdk/lib-storage.`; if (typeof context?.logger?.warn === "function" && !(context.logger instanceof NoOpLogger)) { context.logger.warn(message);