Skip to content

Commit 93cb6e4

Browse files
authored
feat(apigateway): validate integrationHttpMethod with non-MOCK integration 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*
1 parent ac41730 commit 93cb6e4

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

packages/aws-cdk-lib/aws-apigateway/lib/integration.ts

+6
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@ export interface IntegrationConfig {
167167

168168
/**
169169
* The integration's HTTP method type.
170+
* Required unless you use a MOCK integration.
171+
*
170172
* @default - no integration method specified.
171173
*/
172174
readonly integrationHttpMethod?: string;
@@ -205,6 +207,10 @@ export class Integration {
205207
if (options.timeout && !options.timeout.isUnresolved() && (options.timeout.toMilliseconds() < 50 || options.timeout.toMilliseconds() > 29000)) {
206208
throw new Error('Integration timeout must be between 50 milliseconds and 29 seconds.');
207209
}
210+
211+
if (props.type !== IntegrationType.MOCK && !props.integrationHttpMethod) {
212+
throw new Error('integrationHttpMethod is required for non-mock integration types.');
213+
}
208214
}
209215

210216
/**

packages/aws-cdk-lib/aws-apigateway/test/integration.test.ts

+18
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ describe('integration', () => {
1414
// THEN
1515
expect(() => new apigw.Integration({
1616
type: apigw.IntegrationType.AWS_PROXY,
17+
integrationHttpMethod: 'ANY',
1718
options: {
1819
credentialsPassthrough: true,
1920
credentialsRole: role,
@@ -229,4 +230,21 @@ describe('integration', () => {
229230

230231
});
231232

233+
test('validates integrationHttpMethod is required for non-MOCK integration types', () => {
234+
expect(() => new apigw.Integration({
235+
type: apigw.IntegrationType.HTTP_PROXY,
236+
options: {
237+
timeout: cdk.Duration.seconds(15),
238+
},
239+
})).toThrow(/integrationHttpMethod is required for non-mock integration types/);
240+
});
241+
242+
test('integrationHttpMethod can be omitted for MOCK integration type', () => {
243+
expect(() => new apigw.Integration({
244+
type: apigw.IntegrationType.MOCK,
245+
options: {
246+
timeout: cdk.Duration.seconds(15),
247+
},
248+
})).not.toThrow();
249+
});
232250
});

packages/aws-cdk-lib/aws-apigateway/test/method.test.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,11 @@ describe('method', () => {
140140
test('use default integration from api', () => {
141141
// GIVEN
142142
const stack = new cdk.Stack();
143-
const defaultIntegration = new apigw.Integration({ type: apigw.IntegrationType.HTTP_PROXY, uri: 'https://amazon.com' });
143+
const defaultIntegration = new apigw.Integration({
144+
type: apigw.IntegrationType.HTTP_PROXY,
145+
integrationHttpMethod: 'POST',
146+
uri: 'https://amazon.com',
147+
});
144148
const api = new apigw.RestApi(stack, 'test-api', {
145149
cloudWatchRole: false,
146150
deploy: false,
@@ -309,6 +313,7 @@ describe('method', () => {
309313
// WHEN
310314
api.root.addMethod('GET', new apigw.Integration({
311315
type: apigw.IntegrationType.AWS_PROXY,
316+
integrationHttpMethod: 'GET',
312317
options: {
313318
credentialsRole: role,
314319
},
@@ -331,6 +336,7 @@ describe('method', () => {
331336
// WHEN
332337
api.root.addMethod('GET', new apigw.Integration({
333338
type: apigw.IntegrationType.AWS_PROXY,
339+
integrationHttpMethod: 'GET',
334340
options: {
335341
credentialsPassthrough: true,
336342
},

0 commit comments

Comments
 (0)