|
| 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