From 8cfb801ae4bf44b0e27e51b84485cef102700e7d Mon Sep 17 00:00:00 2001 From: Lucas Vieira Date: Fri, 22 Mar 2024 10:31:54 -0300 Subject: [PATCH] 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 --- .changeset/short-eels-compare.md | 5 + .../open-next/src/adapters/event-mapper.ts | 11 ++ .../tests-unit/tests/event-mapper.test.ts | 114 ++++++++++++++++++ 3 files changed, 130 insertions(+) create mode 100644 .changeset/short-eels-compare.md create mode 100644 packages/tests-unit/tests/event-mapper.test.ts diff --git a/.changeset/short-eels-compare.md b/.changeset/short-eels-compare.md new file mode 100644 index 00000000..6c7ec352 --- /dev/null +++ b/.changeset/short-eels-compare.md @@ -0,0 +1,5 @@ +--- +"open-next": patch +--- + +fix(open-next): parse cookies when converting response to cloudfront diff --git a/packages/open-next/src/adapters/event-mapper.ts b/packages/open-next/src/adapters/event-mapper.ts index 15c632d7..3e0ef383 100644 --- a/packages/open-next/src/adapters/event-mapper.ts +++ b/packages/open-next/src/adapters/event-mapper.ts @@ -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) diff --git a/packages/tests-unit/tests/event-mapper.test.ts b/packages/tests-unit/tests/event-mapper.test.ts new file mode 100644 index 00000000..69bc2f8b --- /dev/null +++ b/packages/tests-unit/tests/event-mapper.test.ts @@ -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", + }, + ], + }); + }); + }); +});