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..2ed6033acf --- /dev/null +++ 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 new file mode 100644 index 0000000000..2e685c187b --- /dev/null +++ b/packages/serverless-components/aws-cloudfront/__tests__/origin-with-custom-headers.test.ts @@ -0,0 +1,64 @@ +const { + createComponent, + assertHasCacheBehavior, + assertHasOrigin +} = require("../test-utils"); + +import { + mockCreateDistribution, + mockCreateDistributionPromise +} from "../__mocks__/aws-sdk.mock"; + +jest.mock("aws-sdk", () => require("../__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": { + minTTL: 10, + defaultTTL: 10, + maxTTL: 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..b5506453cc 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,15 @@ 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,