Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: AUTO_WEBP should work if accept header is lowercase #490

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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