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 = {