Skip to content

Commit

Permalink
fix: AUTO_WEBP should work if accepts header is lowercase
Browse files Browse the repository at this point in the history
  • Loading branch information
ccchapman committed Jul 5, 2023
1 parent 5c79a44 commit a7f111d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
3 changes: 2 additions & 1 deletion source/image-handler/image-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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 = {
Expand Down

0 comments on commit a7f111d

Please sign in to comment.