From fc9e1428cef0d292d918c47a06f9131cd5a04f92 Mon Sep 17 00:00:00 2001 From: Iurii Kucherov Date: Tue, 25 Oct 2022 23:09:47 +0200 Subject: [PATCH] refactor(apigateway): Enclose getaway response parameters within single quotes (#22637) Hi team, Here is a follow up to https://github.com/aws/aws-cdk/pull/22633 Please let me know if this makes sense. Thanks ---- ### All Submissions: * [ ] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) ### Adding new Unconventional Dependencies: * [ ] This PR adds new unconventional dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md/#adding-new-unconventional-dependencies) ### New Features * [ ] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/main/INTEGRATION_TESTS.md)? * [ ] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)? *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license* --- packages/@aws-cdk/aws-apigateway/README.md | 5 ++--- .../aws-apigateway/lib/gateway-response.ts | 3 ++- packages/@aws-cdk/aws-apigateway/lib/util.ts | 16 ++++++++++++++++ .../aws-apigateway/test/gateway-response.test.ts | 6 ++++-- 4 files changed, 24 insertions(+), 6 deletions(-) diff --git a/packages/@aws-cdk/aws-apigateway/README.md b/packages/@aws-cdk/aws-apigateway/README.md index cfe75be5b0976..552f515d61488 100644 --- a/packages/@aws-cdk/aws-apigateway/README.md +++ b/packages/@aws-cdk/aws-apigateway/README.md @@ -1378,9 +1378,8 @@ api.addGatewayResponse('test-response', { type: apigateway.ResponseType.ACCESS_DENIED, statusCode: '500', responseHeaders: { - // Note that values must be enclosed within a pair of single quotes - 'Access-Control-Allow-Origin': "'test.com'", - 'test-key': "'test-value'", + 'Access-Control-Allow-Origin': 'test.com', + 'test-key': 'test-value', }, templates: { 'application/json': '{ "message": $context.error.messageString, "statusCode": "488", "type": "$context.error.responseType" }' diff --git a/packages/@aws-cdk/aws-apigateway/lib/gateway-response.ts b/packages/@aws-cdk/aws-apigateway/lib/gateway-response.ts index 0ab490a893051..137b2c66395b4 100644 --- a/packages/@aws-cdk/aws-apigateway/lib/gateway-response.ts +++ b/packages/@aws-cdk/aws-apigateway/lib/gateway-response.ts @@ -2,6 +2,7 @@ import { IResource, Resource } from '@aws-cdk/core'; import { Construct } from 'constructs'; import { CfnGatewayResponse, CfnGatewayResponseProps } from './apigateway.generated'; import { IRestApi } from './restapi'; +import { normalizeResponseParameterValue } from './util'; /** * Represents gateway response resource. @@ -88,7 +89,7 @@ export class GatewayResponse extends Resource implements IGatewayResponse { const responseParameters: { [key: string]: string } = {}; for (const [header, value] of Object.entries(responseHeaders)) { - responseParameters[`gatewayresponse.header.${header}`] = value; + responseParameters[`gatewayresponse.header.${header}`] = normalizeResponseParameterValue(value) ; } return responseParameters; } diff --git a/packages/@aws-cdk/aws-apigateway/lib/util.ts b/packages/@aws-cdk/aws-apigateway/lib/util.ts index e5df3afa246af..29513553d3cf8 100644 --- a/packages/@aws-cdk/aws-apigateway/lib/util.ts +++ b/packages/@aws-cdk/aws-apigateway/lib/util.ts @@ -11,6 +11,22 @@ export function validateHttpMethod(method: string, messagePrefix: string = '') { } } +/** + * Response header values need to be enclosed in single quotes. + */ +export function normalizeResponseParameterValue(value: string) { + if (!value) { + return value; + } + + // check if the value is already enclosed in single quotes + if (value.startsWith("'") && value.endsWith("'")) { + return value; + } + + return `'${value}'`; +} + export function parseMethodOptionsPath(originalPath: string): { resourcePath: string, httpMethod: string } { if (!originalPath.startsWith('/')) { throw new Error(`Method options path must start with '/': ${originalPath}`); diff --git a/packages/@aws-cdk/aws-apigateway/test/gateway-response.test.ts b/packages/@aws-cdk/aws-apigateway/test/gateway-response.test.ts index 80093aba04de1..438d824d2e9d5 100644 --- a/packages/@aws-cdk/aws-apigateway/test/gateway-response.test.ts +++ b/packages/@aws-cdk/aws-apigateway/test/gateway-response.test.ts @@ -45,6 +45,7 @@ describe('gateway response', () => { responseHeaders: { 'Access-Control-Allow-Origin': 'test.com', 'test-key': 'test-value', + 'another-test': "'test-value-enclosed-within-single-quotes'", }, }); @@ -54,8 +55,9 @@ describe('gateway response', () => { RestApiId: stack.resolve(api.restApiId), StatusCode: '500', ResponseParameters: { - 'gatewayresponse.header.Access-Control-Allow-Origin': 'test.com', - 'gatewayresponse.header.test-key': 'test-value', + 'gatewayresponse.header.Access-Control-Allow-Origin': "'test.com'", + 'gatewayresponse.header.test-key': "'test-value'", + 'gatewayresponse.header.another-test': "'test-value-enclosed-within-single-quotes'", }, ResponseTemplates: Match.absent(), });