Skip to content

Instantly share code, notes, and snippets.

@leonelgalan
Created January 21, 2022 19:01
Show Gist options
  • Save leonelgalan/b91020e4d0105e3f09b59cd1adb5dd92 to your computer and use it in GitHub Desktop.
Save leonelgalan/b91020e4d0105e3f09b59cd1adb5dd92 to your computer and use it in GitHub Desktop.
AWS API Gateway Mock Integration that responds with the JSON: `{"message": "Hello world!"}` to all requests for JSON or missing the Content-Type header.
import { Stack, StackProps } from 'aws-cdk-lib';
import * as apigateway from 'aws-cdk-lib/aws-apigateway';
import { Construct } from 'constructs';
export class MyStack extends Stack {
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props);
const mockIntegration = new apigateway.MockIntegration({
passthroughBehavior: apigateway.PassthroughBehavior.WHEN_NO_TEMPLATES,
requestTemplates: {
'application/json': JSON.stringify({ statusCode: 200 }),
},
integrationResponses: [
{
statusCode: '200',
responseTemplates: {
'application/json': JSON.stringify({ message: 'Hello world!' }),
},
},
],
});
const restApi = new apigateway.RestApi(this, 'RestApi', {});
const method = restApi.root.addMethod('ANY', mockIntegration, {
methodResponses: [{ statusCode: '200' }],
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment