Skip to content

Commit

Permalink
feat(apigateway): validate integrationHttpMethod with non-MOCK integr…
Browse files Browse the repository at this point in the history
…ation types (#28316)

`integrationHttpMethod` must be specified for non-MOCK integration types.
This PR adds validation to prevent [build-time errors](#6404).

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
lpizzinidev committed Dec 15, 2023
1 parent ac41730 commit 93cb6e4
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
6 changes: 6 additions & 0 deletions packages/aws-cdk-lib/aws-apigateway/lib/integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ export interface IntegrationConfig {

/**
* The integration's HTTP method type.
* Required unless you use a MOCK integration.
*
* @default - no integration method specified.
*/
readonly integrationHttpMethod?: string;
Expand Down Expand Up @@ -205,6 +207,10 @@ export class Integration {
if (options.timeout && !options.timeout.isUnresolved() && (options.timeout.toMilliseconds() < 50 || options.timeout.toMilliseconds() > 29000)) {
throw new Error('Integration timeout must be between 50 milliseconds and 29 seconds.');
}

if (props.type !== IntegrationType.MOCK && !props.integrationHttpMethod) {
throw new Error('integrationHttpMethod is required for non-mock integration types.');
}
}

/**
Expand Down
18 changes: 18 additions & 0 deletions packages/aws-cdk-lib/aws-apigateway/test/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ describe('integration', () => {
// THEN
expect(() => new apigw.Integration({
type: apigw.IntegrationType.AWS_PROXY,
integrationHttpMethod: 'ANY',
options: {
credentialsPassthrough: true,
credentialsRole: role,
Expand Down Expand Up @@ -229,4 +230,21 @@ describe('integration', () => {

});

test('validates integrationHttpMethod is required for non-MOCK integration types', () => {
expect(() => new apigw.Integration({
type: apigw.IntegrationType.HTTP_PROXY,
options: {
timeout: cdk.Duration.seconds(15),
},
})).toThrow(/integrationHttpMethod is required for non-mock integration types/);
});

test('integrationHttpMethod can be omitted for MOCK integration type', () => {
expect(() => new apigw.Integration({
type: apigw.IntegrationType.MOCK,
options: {
timeout: cdk.Duration.seconds(15),
},
})).not.toThrow();
});
});
8 changes: 7 additions & 1 deletion packages/aws-cdk-lib/aws-apigateway/test/method.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,11 @@ describe('method', () => {
test('use default integration from api', () => {
// GIVEN
const stack = new cdk.Stack();
const defaultIntegration = new apigw.Integration({ type: apigw.IntegrationType.HTTP_PROXY, uri: 'https://amazon.com' });
const defaultIntegration = new apigw.Integration({
type: apigw.IntegrationType.HTTP_PROXY,
integrationHttpMethod: 'POST',
uri: 'https://amazon.com',
});
const api = new apigw.RestApi(stack, 'test-api', {
cloudWatchRole: false,
deploy: false,
Expand Down Expand Up @@ -309,6 +313,7 @@ describe('method', () => {
// WHEN
api.root.addMethod('GET', new apigw.Integration({
type: apigw.IntegrationType.AWS_PROXY,
integrationHttpMethod: 'GET',
options: {
credentialsRole: role,
},
Expand All @@ -331,6 +336,7 @@ describe('method', () => {
// WHEN
api.root.addMethod('GET', new apigw.Integration({
type: apigw.IntegrationType.AWS_PROXY,
integrationHttpMethod: 'GET',
options: {
credentialsPassthrough: true,
},
Expand Down

0 comments on commit 93cb6e4

Please sign in to comment.