From 14673f41b4c6e1077c7f43a72446bdcff94cfd63 Mon Sep 17 00:00:00 2001 From: knihit Date: Fri, 9 Aug 2024 18:56:43 +0000 Subject: [PATCH] fix for custom websocket route not mapping to request template --- .../core/lib/websocket-api-helper.ts | 2 +- .../core/test/websocket-api-helper.test.ts | 83 ++++++++++++++++++- 2 files changed, 83 insertions(+), 2 deletions(-) diff --git a/source/patterns/@aws-solutions-constructs/core/lib/websocket-api-helper.ts b/source/patterns/@aws-solutions-constructs/core/lib/websocket-api-helper.ts index b710b5327..e35e32374 100644 --- a/source/patterns/@aws-solutions-constructs/core/lib/websocket-api-helper.ts +++ b/source/patterns/@aws-solutions-constructs/core/lib/websocket-api-helper.ts @@ -88,7 +88,7 @@ export function buildWebSocketQueueApi( if (props.customRouteName) { webSocketApi.addRoute( props.customRouteName, - buildWebSocketQueueRouteOptions(apiGatewayRole, props.queue, props.customRouteName) + buildWebSocketQueueRouteOptions(apiGatewayRole, props.queue, props.customRouteName, props.defaultRouteRequestTemplate) ); } diff --git a/source/patterns/@aws-solutions-constructs/core/test/websocket-api-helper.test.ts b/source/patterns/@aws-solutions-constructs/core/test/websocket-api-helper.test.ts index 92e6c7ec1..bef187d92 100644 --- a/source/patterns/@aws-solutions-constructs/core/test/websocket-api-helper.test.ts +++ b/source/patterns/@aws-solutions-constructs/core/test/websocket-api-helper.test.ts @@ -373,7 +373,7 @@ test("buildWebSocketQueueApi when passing a custom route name, it should add a c queue, createDefaultRoute: false, defaultIamAuthorization: false, - customRouteName + customRouteName, }); const template = Template.fromStack(stack); @@ -439,3 +439,84 @@ test("buildWebSocketQueueApi when passing a custom route name, it should add a c }, }); }); + +test("buildWebSocketQueueApi when passing a custom route name, with requestTemplate, should map the request template to the custom route", () => { + const app = new cdk.App(); + const stack = new cdk.Stack(app, "TestStack"); + const queue = new sqs.Queue(stack, "TestQueue"); + + const customRouteName = "fakeroutename"; + + buildWebSocketQueueApi(stack, "TestApi", { + queue, + createDefaultRoute: false, + defaultIamAuthorization: false, + customRouteName, + defaultRouteRequestTemplate: { + [customRouteName]: "a&fake&vtl", + }, + }); + + const template = Template.fromStack(stack); + template.resourceCountIs("AWS::ApiGatewayV2::Integration", 1); + template.hasResourceProperties("AWS::ApiGatewayV2::Integration", { + ApiId: { + Ref: Match.anyValue(), + }, + CredentialsArn: { + "Fn::GetAtt": [Match.anyValue(), "Arn"], + }, + IntegrationMethod: "POST", + IntegrationType: "AWS", + IntegrationUri: { + "Fn::Join": [ + "", + [ + "arn:", + { + Ref: "AWS::Partition", + }, + ":apigateway:", + { + Ref: "AWS::Region", + }, + ":sqs:path/", + { + Ref: "AWS::AccountId", + }, + "/", + { + "Fn::GetAtt": [Match.anyValue(), "QueueName"], + }, + ], + ], + }, + PassthroughBehavior: "NEVER", + RequestParameters: { + "integration.request.header.Content-Type": "'application/x-www-form-urlencoded'", + }, + RequestTemplates: { + [customRouteName]: "a&fake&vtl", + }, + TemplateSelectionExpression: customRouteName, + }); + + template.resourceCountIs("AWS::ApiGatewayV2::Route", 1); + template.hasResourceProperties("AWS::ApiGatewayV2::Route", { + ApiId: { + Ref: Match.anyValue(), + }, + RouteKey: customRouteName, + Target: { + "Fn::Join": [ + "", + [ + "integrations/", + { + Ref: Match.stringLikeRegexp("WebSocketApiTestApifakeroutenameRoutefakeroutename"), + }, + ], + ], + }, + }); +});