aws / aws/aws-cdk

(aws-apigateway) MockIntegrations with defaulCorsPreflightOptions return statusCode 500

Open
#18,297 3 comments 0 reactions 1 assignee Claimed by @otaviomacedo View on GitHub
@aws-cdk/aws-apigateway bug needs-cfn p2
Dominant language
TypeScript
Stars
12.9k
Forks
4.6k
Avg merge
1d 19h
Merged PRs (30d)
71

Description

### What is the problem?

If an API Gateway Resource allow CORS with defaultCorsPreflightOptions in its options, hitting its child methods that are MockIntegrations will fail with a Internal Error message (statusCode: 500)

This can be manually fixed by setting PassthroughBehavior.NEVER for the Integration request in CDK or the console.

### Reproduction Steps

```typescript

import {
MockIntegration,
PassthroughBehavior,
RestApi,
} from "aws-cdk-lib/aws-apigateway";

const restApi = new RestApi(this, "testApi", {
defaultCorsPreflightOptions: {
allowOrigins: Cors.ALL_ORIGINS
}
})

const mockIntegration = new MockIntegration({
integrationResponses: [{
statusCode: '200',
responseTemplates: {
'application/json': '{"abc": "dfg"}'
}
}],
passthroughBehavior: PassthroughBehavior.NEVER,
requestTemplates: {
'application/json': '{ "statusCode": 200 }',
},
})

restApi.addMethod(
"GET",
mockIntegration,
{
methodResponses: [
{ statusCode: '200' }
]
});
```

### What did you expect to happen?

I expected MockIntegration methods to work beside the LambdaIntegration methods, even with CORS enabled by default.

### What actually happened?

Only the LambdaIntegration works, not the MockIntegration methods.
They work when testing through the API Gateway test option in the console, but not when hitting them with my browser, implying it's a problem with CORS/OPTIONS.

### CDK CLI Version

2.4.0 (build 993f14d)

### Framework Version

_No response_

### Node.js Version

v16.13.1

### OS

Ubuntu 21.10

### Language

Typescript

### Language Version

_No response_

### Other information

A workaround is to set defaultCorsPreflightOptions to undefined for the resources with MockIntegration and manually add the method OPTIONS with passthroughBehavior: PassthroughBehavior.NEVER.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.