Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(apigateway): base path url cannot contain upper case characters #11799

Merged
merged 7 commits into from
Dec 9, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-apigateway/lib/base-path-mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export class BasePathMapping extends Resource {
super(scope, id);

if (props.basePath && !Token.isUnresolved(props.basePath)) {
if (!props.basePath.match(/^[a-z0-9$_.+!*'()-]+$/)) {
if (!props.basePath.match(/^[a-zA-Z0-9$_.+!*'()-]+$/)) {
throw new Error(`A base path may only contain letters, numbers, and one of "$-_.+!*'()", received: ${props.basePath}`);
}
}
Expand Down
107 changes: 107 additions & 0 deletions packages/@aws-cdk/aws-apigateway/test/base-path-mapping.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import '@aws-cdk/assert/jest';
import * as acm from '@aws-cdk/aws-certificatemanager';
import * as cdk from '@aws-cdk/core';
import * as apigw from '../lib';

describe('BasePathMapping', () => {
test('default setup', () => {
// GIVEN
const stack = new cdk.Stack();
const api = new apigw.RestApi(stack, 'MyApi');
api.root.addMethod('GET'); // api must have atleast one method.
const domain = new apigw.DomainName(stack, 'MyDomain', {
domainName: 'example.com',
certificate: acm.Certificate.fromCertificateArn(stack, 'cert', 'arn:aws:acm:us-east-1:1111111:certificate/11-3336f1-44483d-adc7-9cd375c5169d'),
endpointType: apigw.EndpointType.REGIONAL,
});

// WHEN
new apigw.BasePathMapping(stack, 'MyBasePath', {
restApi: api,
domainName: domain,
});

// THEN
expect(stack).toHaveResource('AWS::ApiGateway::BasePathMapping', {
DomainName: { Ref: 'MyDomainE4943FBC' },
RestApiId: { Ref: 'MyApi49610EDF' },
});
});

test('specify basePath property', () => {
nija-at marked this conversation as resolved.
Show resolved Hide resolved
// GIVEN
const stack = new cdk.Stack();
const api = new apigw.RestApi(stack, 'MyApi');
api.root.addMethod('GET'); // api must have atleast one method.
const domain = new apigw.DomainName(stack, 'MyDomain', {
domainName: 'example.com',
certificate: acm.Certificate.fromCertificateArn(stack, 'cert', 'arn:aws:acm:us-east-1:1111111:certificate/11-3336f1-44483d-adc7-9cd375c5169d'),
endpointType: apigw.EndpointType.REGIONAL,
});

// WHEN
new apigw.BasePathMapping(stack, 'MyBasePath', {
restApi: api,
domainName: domain,
basePath: 'My_B45E-P4th',
});

// THEN
expect(stack).toHaveResourceLike('AWS::ApiGateway::BasePathMapping', {
BasePath: 'My_B45E-P4th',
});
});

test('throw error for invalid basePath property', () => {
// GIVEN
const stack = new cdk.Stack();
const api = new apigw.RestApi(stack, 'MyApi');
api.root.addMethod('GET'); // api must have atleast one method.
const domain = new apigw.DomainName(stack, 'MyDomain', {
domainName: 'example.com',
certificate: acm.Certificate.fromCertificateArn(stack, 'cert', 'arn:aws:acm:us-east-1:1111111:certificate/11-3336f1-44483d-adc7-9cd375c5169d'),
endpointType: apigw.EndpointType.REGIONAL,
});

// WHEN
const invalidBasePath = '/invalid-/base-path';

// THEN
expect(() => {
new apigw.BasePathMapping(stack, 'MyBasePath', {
restApi: api,
domainName: domain,
basePath: invalidBasePath,
});
}).toThrowError(`A base path may only contain letters, numbers, and one of "$-_.+!*'()", received: ${invalidBasePath}`);
nija-at marked this conversation as resolved.
Show resolved Hide resolved
});

test('specify stage property', () => {
// GIVEN
const stack = new cdk.Stack();
const api = new apigw.RestApi(stack, 'MyApi');
api.root.addMethod('GET'); // api must have atleast one method.
const domain = new apigw.DomainName(stack, 'MyDomain', {
domainName: 'example.com',
certificate: acm.Certificate.fromCertificateArn(stack, 'cert', 'arn:aws:acm:us-east-1:1111111:certificate/11-3336f1-44483d-adc7-9cd375c5169d'),
endpointType: apigw.EndpointType.REGIONAL,
});
const stage = new apigw.Stage(stack, 'MyStage', {
deployment: new apigw.Deployment(stack, 'MyDeplouyment', {
api,
}),
});

// WHEN
new apigw.BasePathMapping(stack, 'MyBasePathMapping', {
restApi: api,
domainName: domain,
stage,
});

// THEN
expect(stack).toHaveResourceLike('AWS::ApiGateway::BasePathMapping', {
Stage: { Ref: 'MyStage572B0482' },
});
});
});