diff --git a/packages/aws-cdk-lib/aws-cloudfront/test/experimental/edge-function.test.ts b/packages/aws-cdk-lib/aws-cloudfront/test/experimental/edge-function.test.ts index 5b927a76ba2bb..bcf7a96ecd767 100644 --- a/packages/aws-cdk-lib/aws-cloudfront/test/experimental/edge-function.test.ts +++ b/packages/aws-cdk-lib/aws-cloudfront/test/experimental/edge-function.test.ts @@ -5,10 +5,40 @@ import * as iam from '../../../aws-iam'; import * as lambda from '../../../aws-lambda'; import * as cdk from '../../../core'; import * as cloudfront from '../../lib'; +import { handler } from '../../lib/experimental/edge-function/index'; let app: cdk.App; let stack: cdk.Stack; +type RequestType = 'Create' | 'Update' | 'Delete'; + +const mockSSM = { + getParameter: jest.fn().mockResolvedValue({ + Parameter: { Value: 'arn:aws:lambda:us-west-2:123456789012:function:edge-function' }, + }), +}; + +jest.mock('@aws-sdk/client-ssm', () => { + return { + SSM: jest.fn().mockImplementation(() => { + return mockSSM; + }), + }; +}); + +const eventCommon = { + ServiceToken: 'token', + ResponseURL: 'https://localhost', + StackId: 'stackId', + RequestId: 'requestId', + LogicalResourceId: 'logicalResourceId', + PhysicalResourceId: 'physicalResourceId', + ResourceProperties: { + Region: 'us-west-2', + ParameterName: 'edge-function-arn', + }, +}; + beforeEach(() => { app = new cdk.App(); stack = new cdk.Stack(app, 'Stack', { @@ -16,6 +46,8 @@ beforeEach(() => { }); }); +afterAll(() => { jest.resetAllMocks(); }); + describe('stacks', () => { test('creates a custom resource and supporting resources in main stack', () => { new cloudfront.experimental.EdgeFunction(stack, 'MyFn', defaultEdgeFunctionProps()); @@ -227,6 +259,58 @@ describe('stacks', () => { }); }); +describe('handler', () => { + afterEach(() => { + jest.restoreAllMocks(); + mockSSM.getParameter.mockClear(); + }); + + test('create event', async () => { + // GIVEN + const event = { + ...eventCommon, + RequestType: 'Create' as RequestType, + }; + + // WHEN + const response = await handler(event); + + // THEN + expect(mockSSM.getParameter).toBeCalledWith({ Name: 'edge-function-arn' }); + expect(response).toEqual({ Data: { FunctionArn: 'arn:aws:lambda:us-west-2:123456789012:function:edge-function' } }); + }); + + test('update event', async () => { + // GIVEN + const event = { + ...eventCommon, + RequestType: 'Update' as RequestType, + }; + + // WHEN + const response = await handler(event); + + // THEN + expect(mockSSM.getParameter).toBeCalledWith({ Name: 'edge-function-arn' }); + expect(response).toEqual({ Data: { FunctionArn: 'arn:aws:lambda:us-west-2:123456789012:function:edge-function' } }); + }); + + test('delete event', async () => { + // GIVEN + const event = { + ...eventCommon, + RequestType: 'Delete' as RequestType, + }; + + // WHEN + const response = await handler(event); + + // THEN + expect(mockSSM.getParameter).not.toHaveBeenCalled(); + expect(response).toBe(undefined); + }); +}); + test('addAlias() creates alias in function stack', () => { const fn = new cloudfront.experimental.EdgeFunction(stack, 'MyFn', defaultEdgeFunctionProps());