Skip to content

Commit

Permalink
fix(open-next): parse cookies when converting response to cloudfront (#…
Browse files Browse the repository at this point in the history
…387)

* fix(open-next): parse cookies when converting response to cloudfront

* chore: add cloudfront event mapper unit tests

* Create short-eels-compare.md

---------

Co-authored-by: conico974 <[email protected]>
  • Loading branch information
lucasvieirasilva and conico974 authored Mar 22, 2024
1 parent a475e9d commit 8cfb801
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/short-eels-compare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"open-next": patch
---

fix(open-next): parse cookies when converting response to cloudfront
11 changes: 11 additions & 0 deletions packages/open-next/src/adapters/event-mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,17 @@ function convertToCloudFrontRequestResult(
Object.entries(result.headers)
.filter(([key]) => key.toLowerCase() !== "content-length")
.forEach(([key, value]) => {
if (key === "set-cookie") {
const cookies = parseCookies(value);
if (cookies) {
headers[key] = cookies.map((cookie) => ({
key,
value: cookie,
}));
}
return;
}

headers[key] = [
...(headers[key] || []),
...(Array.isArray(value)
Expand Down
114 changes: 114 additions & 0 deletions packages/tests-unit/tests/event-mapper.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import { CloudFrontRequestResult } from "aws-lambda";

import { convertTo } from "../../open-next/src/adapters/event-mapper";

describe("convertTo", () => {
describe("CloudFront Result", () => {
it("Should parse the headers", () => {
const response = convertTo({
body: "",
headers: {
"content-type": "application/json",
test: "test",
},
isBase64Encoded: false,
statusCode: 200,
type: "cf",
}) as CloudFrontRequestResult;

expect(response?.headers).toStrictEqual({
"content-type": [
{
key: "content-type",
value: "application/json",
},
],
test: [
{
key: "test",
value: "test",
},
],
});
});

it("Should parse the headers with arrays", () => {
const response = convertTo({
body: "",
headers: {
test: ["test1", "test2"],
},
isBase64Encoded: false,
statusCode: 200,
type: "cf",
}) as CloudFrontRequestResult;

expect(response?.headers).toStrictEqual({
test: [
{
key: "test",
value: "test1",
},
{
key: "test",
value: "test2",
},
],
});
});

it("Should parse the headers with cookies", () => {
const response = convertTo({
body: "",
headers: {
"set-cookie":
"test=1; Path=/; HttpOnly; Secure; SameSite=None, test=2; Path=/; HttpOnly; Secure; SameSite=None",
},
isBase64Encoded: false,
statusCode: 200,
type: "cf",
}) as CloudFrontRequestResult;

expect(response?.headers).toStrictEqual({
"set-cookie": [
{
key: "set-cookie",
value: "test=1; Path=/; HttpOnly; Secure; SameSite=None",
},
{
key: "set-cookie",
value: "test=2; Path=/; HttpOnly; Secure; SameSite=None",
},
],
});
});

it("Should parse the headers with cookies + expires", () => {
const response = convertTo({
body: "",
headers: {
"set-cookie":
"test=1; Path=/; Expires=Sun, 14 Apr 2024 22:19:07 GMT; HttpOnly; Secure; SameSite=None, test=2; Path=/; Expires=Sun, 14 Apr 2024 22:19:07 GMT; HttpOnly; Secure; SameSite=None",
},
isBase64Encoded: false,
statusCode: 200,
type: "cf",
}) as CloudFrontRequestResult;

expect(response?.headers).toStrictEqual({
"set-cookie": [
{
key: "set-cookie",
value:
"test=1; Path=/; Expires=Sun, 14 Apr 2024 22:19:07 GMT; HttpOnly; Secure; SameSite=None",
},
{
key: "set-cookie",
value:
"test=2; Path=/; Expires=Sun, 14 Apr 2024 22:19:07 GMT; HttpOnly; Secure; SameSite=None",
},
],
});
});
});
});

0 comments on commit 8cfb801

Please sign in to comment.