From a7f111d1494bcdd1820f36352b1fa34acfafc714 Mon Sep 17 00:00:00 2001 From: Chris Chapman Date: Wed, 5 Jul 2023 11:12:52 -0700 Subject: [PATCH] fix: AUTO_WEBP should work if `accepts` header is lowercase Fixes https://github.com/aws-solutions/serverless-image-handler/issues/467 Refs: https://github.com/aws/aws-sam-cli/issues/3083#issuecomment-887632702 --- source/image-handler/image-request.ts | 3 ++- .../image-request/get-output-format.spec.ts | 20 ++++++++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/source/image-handler/image-request.ts b/source/image-handler/image-request.ts index ad4fae161..4bfb72c9c 100644 --- a/source/image-handler/image-request.ts +++ b/source/image-handler/image-request.ts @@ -427,8 +427,9 @@ export class ImageRequest { */ public getOutputFormat(event: ImageHandlerEvent, requestType: RequestTypes = undefined): ImageFormatTypes { const { AUTO_WEBP } = process.env; + const accept = event.headers.Accept || event.headers.accept; - if (AUTO_WEBP === "Yes" && event.headers.Accept && event.headers.Accept.includes(ContentTypes.WEBP)) { + if (AUTO_WEBP === "Yes" && accept && accept.includes(ContentTypes.WEBP)) { return ImageFormatTypes.WEBP; } else if (requestType === RequestTypes.DEFAULT) { const decoded = this.decodeRequest(event); diff --git a/source/image-handler/test/image-request/get-output-format.spec.ts b/source/image-handler/test/image-request/get-output-format.spec.ts index 0063d8f56..664121559 100644 --- a/source/image-handler/test/image-request/get-output-format.spec.ts +++ b/source/image-handler/test/image-request/get-output-format.spec.ts @@ -21,7 +21,7 @@ describe("getOutputFormat", () => { process.env = OLD_ENV; }); - it('Should pass if it returns "webp" for an accepts header which includes webp', () => { + it('Should pass if it returns "webp" for a capitalized accepts header which includes webp', () => { // Arrange const event = { headers: { @@ -39,6 +39,24 @@ describe("getOutputFormat", () => { expect(result).toEqual("webp"); }); + it('Should pass if it returns "webp" for a lowercase accepts header which includes webp', () => { + // Arrange + const event = { + headers: { + accept: + "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3", + }, + }; + process.env.AUTO_WEBP = "Yes"; + + // Act + const imageRequest = new ImageRequest(s3Client, secretProvider); + const result = imageRequest.getOutputFormat(event); + + // Assert + expect(result).toEqual("webp"); + }); + it("Should pass if it returns null for an accepts header which does not include webp", () => { // Arrange const event = {