class MockIntegration
Language | Type name |
---|---|
![]() | Amazon.CDK.AWS.APIGateway.MockIntegration |
![]() | software.amazon.awscdk.services.apigateway.MockIntegration |
![]() | aws_cdk.aws_apigateway.MockIntegration |
![]() | @aws-cdk/aws-apigateway » MockIntegration |
Extends
Integration
This type of integration lets API Gateway return a response without sending the request further to the backend.
This is useful for API testing because it can be used to test the integration set up without incurring charges for using the backend and to enable collaborative development of an API. In collaborative development, a team can isolate their development effort by setting up simulations of API components owned by other teams by using the MOCK integrations. It is also used to return CORS-related headers to ensure that the API method permits CORS access. In fact, the API Gateway console integrates the OPTIONS method to support CORS with a mock integration. Gateway responses are other examples of mock integrations.
Example
import * as path from 'path';
import * as lambda from '@aws-cdk/aws-lambda';
import { App, Stack } from '@aws-cdk/core';
import { MockIntegration, PassthroughBehavior, RestApi } from '../../lib';
import { RequestAuthorizer } from '../../lib/authorizers';
import { IdentitySource } from '../../lib/authorizers/identity-source';
// Against the RestApi endpoint from the stack output, run
// `curl -s -o /dev/null -w "%{http_code}" <url>` should return 401
// `curl -s -o /dev/null -w "%{http_code}" -H 'Authorization: deny' <url>?allow=yes` should return 403
// `curl -s -o /dev/null -w "%{http_code}" -H 'Authorization: allow' <url>?allow=yes` should return 200
const app = new App();
const stack = new Stack(app, 'RequestAuthorizerInteg');
const authorizerFn = new lambda.Function(stack, 'MyAuthorizerFunction', {
runtime: lambda.Runtime.NODEJS_14_X,
handler: 'index.handler',
code: lambda.AssetCode.fromAsset(path.join(__dirname, 'integ.request-authorizer.handler')),
});
const restapi = new RestApi(stack, 'MyRestApi');
const authorizer = new RequestAuthorizer(stack, 'MyAuthorizer', {
handler: authorizerFn,
identitySources: [IdentitySource.header('Authorization'), IdentitySource.queryString('allow')],
});
restapi.root.addMethod('ANY', new MockIntegration({
integrationResponses: [
{ statusCode: '200' },
],
passthroughBehavior: PassthroughBehavior.NEVER,
requestTemplates: {
'application/json': '{ "statusCode": 200 }',
},
}), {
methodResponses: [
{ statusCode: '200' },
],
authorizer,
});
Initializer
new MockIntegration(options?: IntegrationOptions)
Parameters
- options
Integration
Options
Methods
Name | Description |
---|---|
bind(_method) | Can be overridden by subclasses to allow the integration to interact with the method being integrated, access the REST API object, method ARNs, etc. |
bind(_method)
public bind(_method: Method): IntegrationConfig
Parameters
- _method
Method
Returns
Can be overridden by subclasses to allow the integration to interact with the method being integrated, access the REST API object, method ARNs, etc.