Skip to content

Commit 8cfb801

Browse files
fix(open-next): parse cookies when converting response to cloudfront (#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 <nicodorseuil@yahoo.fr>
1 parent a475e9d commit 8cfb801

File tree

3 files changed

+130
-0
lines changed

3 files changed

+130
-0
lines changed

.changeset/short-eels-compare.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"open-next": patch
3+
---
4+
5+
fix(open-next): parse cookies when converting response to cloudfront

packages/open-next/src/adapters/event-mapper.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,17 @@ function convertToCloudFrontRequestResult(
218218
Object.entries(result.headers)
219219
.filter(([key]) => key.toLowerCase() !== "content-length")
220220
.forEach(([key, value]) => {
221+
if (key === "set-cookie") {
222+
const cookies = parseCookies(value);
223+
if (cookies) {
224+
headers[key] = cookies.map((cookie) => ({
225+
key,
226+
value: cookie,
227+
}));
228+
}
229+
return;
230+
}
231+
221232
headers[key] = [
222233
...(headers[key] || []),
223234
...(Array.isArray(value)
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import { CloudFrontRequestResult } from "aws-lambda";
2+
3+
import { convertTo } from "../../open-next/src/adapters/event-mapper";
4+
5+
describe("convertTo", () => {
6+
describe("CloudFront Result", () => {
7+
it("Should parse the headers", () => {
8+
const response = convertTo({
9+
body: "",
10+
headers: {
11+
"content-type": "application/json",
12+
test: "test",
13+
},
14+
isBase64Encoded: false,
15+
statusCode: 200,
16+
type: "cf",
17+
}) as CloudFrontRequestResult;
18+
19+
expect(response?.headers).toStrictEqual({
20+
"content-type": [
21+
{
22+
key: "content-type",
23+
value: "application/json",
24+
},
25+
],
26+
test: [
27+
{
28+
key: "test",
29+
value: "test",
30+
},
31+
],
32+
});
33+
});
34+
35+
it("Should parse the headers with arrays", () => {
36+
const response = convertTo({
37+
body: "",
38+
headers: {
39+
test: ["test1", "test2"],
40+
},
41+
isBase64Encoded: false,
42+
statusCode: 200,
43+
type: "cf",
44+
}) as CloudFrontRequestResult;
45+
46+
expect(response?.headers).toStrictEqual({
47+
test: [
48+
{
49+
key: "test",
50+
value: "test1",
51+
},
52+
{
53+
key: "test",
54+
value: "test2",
55+
},
56+
],
57+
});
58+
});
59+
60+
it("Should parse the headers with cookies", () => {
61+
const response = convertTo({
62+
body: "",
63+
headers: {
64+
"set-cookie":
65+
"test=1; Path=/; HttpOnly; Secure; SameSite=None, test=2; Path=/; HttpOnly; Secure; SameSite=None",
66+
},
67+
isBase64Encoded: false,
68+
statusCode: 200,
69+
type: "cf",
70+
}) as CloudFrontRequestResult;
71+
72+
expect(response?.headers).toStrictEqual({
73+
"set-cookie": [
74+
{
75+
key: "set-cookie",
76+
value: "test=1; Path=/; HttpOnly; Secure; SameSite=None",
77+
},
78+
{
79+
key: "set-cookie",
80+
value: "test=2; Path=/; HttpOnly; Secure; SameSite=None",
81+
},
82+
],
83+
});
84+
});
85+
86+
it("Should parse the headers with cookies + expires", () => {
87+
const response = convertTo({
88+
body: "",
89+
headers: {
90+
"set-cookie":
91+
"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",
92+
},
93+
isBase64Encoded: false,
94+
statusCode: 200,
95+
type: "cf",
96+
}) as CloudFrontRequestResult;
97+
98+
expect(response?.headers).toStrictEqual({
99+
"set-cookie": [
100+
{
101+
key: "set-cookie",
102+
value:
103+
"test=1; Path=/; Expires=Sun, 14 Apr 2024 22:19:07 GMT; HttpOnly; Secure; SameSite=None",
104+
},
105+
{
106+
key: "set-cookie",
107+
value:
108+
"test=2; Path=/; Expires=Sun, 14 Apr 2024 22:19:07 GMT; HttpOnly; Secure; SameSite=None",
109+
},
110+
],
111+
});
112+
});
113+
});
114+
});

0 commit comments

Comments
 (0)