From b46813b44dc2f09df7322a1e152aaa6520e836ad Mon Sep 17 00:00:00 2001 From: Steven 'Xaroth' Noorbergen Date: Mon, 26 Jul 2021 11:42:12 +0200 Subject: [PATCH 1/3] Add support for custom headers in custom origins --- .../origin-with-custom-headers.test.ts.snap | 0 .../origin-with-custom-headers.test.ts | 57 +++++++++++++++++++ .../aws-cloudfront/src/getOriginConfig.ts | 8 +++ 3 files changed, 65 insertions(+) create mode 100644 packages/serverless-components/aws-cloudfront/__tests__/__snapshots__/origin-with-custom-headers.test.ts.snap create mode 100644 packages/serverless-components/aws-cloudfront/__tests__/origin-with-custom-headers.test.ts diff --git a/packages/serverless-components/aws-cloudfront/__tests__/__snapshots__/origin-with-custom-headers.test.ts.snap b/packages/serverless-components/aws-cloudfront/__tests__/__snapshots__/origin-with-custom-headers.test.ts.snap new file mode 100644 index 0000000000..e69de29bb2 diff --git a/packages/serverless-components/aws-cloudfront/__tests__/origin-with-custom-headers.test.ts b/packages/serverless-components/aws-cloudfront/__tests__/origin-with-custom-headers.test.ts new file mode 100644 index 0000000000..c5590b7d1e --- /dev/null +++ b/packages/serverless-components/aws-cloudfront/__tests__/origin-with-custom-headers.test.ts @@ -0,0 +1,57 @@ +const { createComponent, assertHasCacheBehavior, assertHasOrigin } = require('../test-utils') + +import { + mockCreateDistribution, + mockCreateDistributionPromise + } from "../__mocks__/aws-sdk.mock"; + + +describe('Input origin with custom header', () => { + let component + + beforeEach(async () => { + mockCreateDistributionPromise.mockResolvedValueOnce({ + Distribution: { + Id: 'xyz' + } + }) + + component = await createComponent() + }) + + it('creates distribution with custom url origin', async () => { + await component.default({ + origins: [ + { + url: 'https://exampleorigin.com', + pathPatterns: { + '/some/path': { + ttl: 10, + allowedHttpMethods: ['GET', 'HEAD', 'POST'] + } + }, + headers: { + 'x-api-key': 'test' + } + } + ] + }) + + assertHasOrigin(mockCreateDistribution, { + Id: 'exampleorigin.com', + DomainName: 'exampleorigin.com', + CustomHeaders: { + Quantity: 1, + Items: [{ HeaderName: 'x-api-key', HeaderValue: 'test' }] + } + }) + + assertHasCacheBehavior(mockCreateDistribution, { + PathPattern: '/some/path', + MinTTL: 10, + TargetOriginId: 'exampleorigin.com' + }) + + expect(mockCreateDistribution.mock.calls[0][0]).toMatchSnapshot() + }) +}) diff --git a/packages/serverless-components/aws-cloudfront/src/getOriginConfig.ts b/packages/serverless-components/aws-cloudfront/src/getOriginConfig.ts index bfac027e12..df91ea7fef 100644 --- a/packages/serverless-components/aws-cloudfront/src/getOriginConfig.ts +++ b/packages/serverless-components/aws-cloudfront/src/getOriginConfig.ts @@ -25,6 +25,7 @@ export type Origin = protocolPolicy: string; url: string; pathPatterns: Record; + headers: Record; }; export const getOriginConfig = ( @@ -55,6 +56,13 @@ export const getOriginConfig = ( : "" }; } else { + if (typeof origin === "object" && origin.headers) { + originConfig.CustomHeaders.Quantity = Object.keys(origin.headers).length; + originConfig.CustomHeaders.Items = Object.keys(origin.headers).map((key) => ({ + HeaderName: key, + HeaderValue: origin.headers[key], + })) + } originConfig.CustomOriginConfig = { HTTPPort: 80, HTTPSPort: 443, From 0734207c1b4006d32b1adf5e7d6c9134a7d529c5 Mon Sep 17 00:00:00 2001 From: Steven 'Xaroth' Noorbergen Date: Tue, 27 Jul 2021 09:21:17 +0200 Subject: [PATCH 2/3] Fix eslint errors. --- .../aws-cloudfront/src/getOriginConfig.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/packages/serverless-components/aws-cloudfront/src/getOriginConfig.ts b/packages/serverless-components/aws-cloudfront/src/getOriginConfig.ts index df91ea7fef..b5506453cc 100644 --- a/packages/serverless-components/aws-cloudfront/src/getOriginConfig.ts +++ b/packages/serverless-components/aws-cloudfront/src/getOriginConfig.ts @@ -58,10 +58,12 @@ export const getOriginConfig = ( } else { if (typeof origin === "object" && origin.headers) { originConfig.CustomHeaders.Quantity = Object.keys(origin.headers).length; - originConfig.CustomHeaders.Items = Object.keys(origin.headers).map((key) => ({ - HeaderName: key, - HeaderValue: origin.headers[key], - })) + originConfig.CustomHeaders.Items = Object.keys(origin.headers).map( + (key) => ({ + HeaderName: key, + HeaderValue: origin.headers[key] + }) + ); } originConfig.CustomOriginConfig = { HTTPPort: 80, From c51c073d283652cc86be883173cedf5f25be06d4 Mon Sep 17 00:00:00 2001 From: Steven 'Xaroth' Noorbergen Date: Tue, 27 Jul 2021 09:30:13 +0200 Subject: [PATCH 3/3] Fix integration tests. --- .../origin-with-custom-headers.test.ts.snap | 153 ++++++++++++++++++ .../origin-with-custom-headers.test.ts | 63 ++++---- 2 files changed, 188 insertions(+), 28 deletions(-) diff --git a/packages/serverless-components/aws-cloudfront/__tests__/__snapshots__/origin-with-custom-headers.test.ts.snap b/packages/serverless-components/aws-cloudfront/__tests__/__snapshots__/origin-with-custom-headers.test.ts.snap index e69de29bb2..2ed6033acf 100644 --- a/packages/serverless-components/aws-cloudfront/__tests__/__snapshots__/origin-with-custom-headers.test.ts.snap +++ b/packages/serverless-components/aws-cloudfront/__tests__/__snapshots__/origin-with-custom-headers.test.ts.snap @@ -0,0 +1,153 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Input origin with custom header creates distribution with custom url origin 1`] = ` +Object { + "DistributionConfig": Object { + "Aliases": Object { + "Items": Array [], + "Quantity": 0, + }, + "CacheBehaviors": Object { + "Items": Array [ + Object { + "AllowedMethods": Object { + "CachedMethods": Object { + "Items": Array [ + "GET", + "HEAD", + ], + "Quantity": 2, + }, + "Items": Array [ + "GET", + "HEAD", + "POST", + ], + "Quantity": 3, + }, + "Compress": true, + "DefaultTTL": 10, + "FieldLevelEncryptionId": "", + "ForwardedValues": Object { + "Cookies": Object { + "Forward": "all", + }, + "Headers": Object { + "Items": Array [], + "Quantity": 0, + }, + "QueryString": true, + "QueryStringCacheKeys": Object { + "Items": Array [], + "Quantity": 0, + }, + }, + "LambdaFunctionAssociations": Object { + "Items": Array [], + "Quantity": 0, + }, + "MaxTTL": 10, + "MinTTL": 10, + "PathPattern": "/some/path", + "SmoothStreaming": false, + "TargetOriginId": "exampleorigin.com", + "TrustedSigners": Object { + "Enabled": false, + "Quantity": 0, + }, + "ViewerProtocolPolicy": "https-only", + }, + ], + "Quantity": 1, + }, + "CallerReference": "1566599541192", + "Comment": "", + "CustomErrorResponses": Object { + "Items": Array [], + "Quantity": 0, + }, + "DefaultCacheBehavior": Object { + "AllowedMethods": Object { + "CachedMethods": Object { + "Items": Array [ + "HEAD", + "GET", + ], + "Quantity": 2, + }, + "Items": Array [ + "HEAD", + "GET", + ], + "Quantity": 2, + }, + "Compress": false, + "DefaultTTL": 86400, + "FieldLevelEncryptionId": "", + "ForwardedValues": Object { + "Cookies": Object { + "Forward": "none", + }, + "Headers": Object { + "Items": Array [], + "Quantity": 0, + }, + "QueryString": false, + "QueryStringCacheKeys": Object { + "Items": Array [], + "Quantity": 0, + }, + }, + "LambdaFunctionAssociations": Object { + "Items": Array [], + "Quantity": 0, + }, + "MaxTTL": 31536000, + "MinTTL": 0, + "SmoothStreaming": false, + "TargetOriginId": "exampleorigin.com", + "TrustedSigners": Object { + "Enabled": false, + "Items": Array [], + "Quantity": 0, + }, + "ViewerProtocolPolicy": "redirect-to-https", + }, + "Enabled": true, + "HttpVersion": "http2", + "Origins": Object { + "Items": Array [ + Object { + "CustomHeaders": Object { + "Items": Array [ + Object { + "HeaderName": "x-api-key", + "HeaderValue": "test", + }, + ], + "Quantity": 1, + }, + "CustomOriginConfig": Object { + "HTTPPort": 80, + "HTTPSPort": 443, + "OriginKeepaliveTimeout": 5, + "OriginProtocolPolicy": "https-only", + "OriginReadTimeout": 30, + "OriginSslProtocols": Object { + "Items": Array [ + "TLSv1.2", + ], + "Quantity": 1, + }, + }, + "DomainName": "exampleorigin.com", + "Id": "exampleorigin.com", + "OriginPath": "", + }, + ], + "Quantity": 1, + }, + "PriceClass": "PriceClass_All", + }, +} +`; diff --git a/packages/serverless-components/aws-cloudfront/__tests__/origin-with-custom-headers.test.ts b/packages/serverless-components/aws-cloudfront/__tests__/origin-with-custom-headers.test.ts index c5590b7d1e..2e685c187b 100644 --- a/packages/serverless-components/aws-cloudfront/__tests__/origin-with-custom-headers.test.ts +++ b/packages/serverless-components/aws-cloudfront/__tests__/origin-with-custom-headers.test.ts @@ -1,57 +1,64 @@ -const { createComponent, assertHasCacheBehavior, assertHasOrigin } = require('../test-utils') +const { + createComponent, + assertHasCacheBehavior, + assertHasOrigin +} = require("../test-utils"); import { - mockCreateDistribution, - mockCreateDistributionPromise - } from "../__mocks__/aws-sdk.mock"; - + mockCreateDistribution, + mockCreateDistributionPromise +} from "../__mocks__/aws-sdk.mock"; -describe('Input origin with custom header', () => { - let component +jest.mock("aws-sdk", () => require("../__mocks__/aws-sdk.mock")); + +describe("Input origin with custom header", () => { + let component; beforeEach(async () => { mockCreateDistributionPromise.mockResolvedValueOnce({ Distribution: { - Id: 'xyz' + Id: "xyz" } - }) + }); - component = await createComponent() - }) + component = await createComponent(); + }); - it('creates distribution with custom url origin', async () => { + it("creates distribution with custom url origin", async () => { await component.default({ origins: [ { - url: 'https://exampleorigin.com', + url: "https://exampleorigin.com", pathPatterns: { - '/some/path': { - ttl: 10, - allowedHttpMethods: ['GET', 'HEAD', 'POST'] + "/some/path": { + minTTL: 10, + defaultTTL: 10, + maxTTL: 10, + allowedHttpMethods: ["GET", "HEAD", "POST"] } }, headers: { - 'x-api-key': 'test' + "x-api-key": "test" } } ] - }) + }); assertHasOrigin(mockCreateDistribution, { - Id: 'exampleorigin.com', - DomainName: 'exampleorigin.com', + Id: "exampleorigin.com", + DomainName: "exampleorigin.com", CustomHeaders: { Quantity: 1, - Items: [{ HeaderName: 'x-api-key', HeaderValue: 'test' }] + Items: [{ HeaderName: "x-api-key", HeaderValue: "test" }] } - }) + }); assertHasCacheBehavior(mockCreateDistribution, { - PathPattern: '/some/path', + PathPattern: "/some/path", MinTTL: 10, - TargetOriginId: 'exampleorigin.com' - }) + TargetOriginId: "exampleorigin.com" + }); - expect(mockCreateDistribution.mock.calls[0][0]).toMatchSnapshot() - }) -}) + expect(mockCreateDistribution.mock.calls[0][0]).toMatchSnapshot(); + }); +});