Class MockIntegration
This type of integration lets API Gateway return a response without sending the request further to the backend.
Inherited Members
Namespace: Amazon.CDK.AWS.APIGateway
Assembly: Amazon.CDK.Lib.dll
Syntax (csharp)
public class MockIntegration : Integration
Syntax (vb)
Public Class MockIntegration Inherits Integration
Remarks
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.
ExampleMetadata: lit=aws-apigateway/test/authorizers/integ.request-authorizer.lit.ts infused
Examples
using Path;
using Amazon.CDK.AWS.Lambda;
using Amazon.CDK;
using Amazon.CDK.AWS.APIGateway;
// 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
var app = new App();
var stack = new Stack(app, "RequestAuthorizerInteg");
var authorizerFn = new Function(stack, "MyAuthorizerFunction", new FunctionProps {
Runtime = Runtime.NODEJS_LATEST,
Handler = "index.handler",
Code = AssetCode.FromAsset(Join(__dirname, "integ.request-authorizer.handler"))
});
var restapi = new RestApi(stack, "MyRestApi", new RestApiProps { CloudWatchRole = true });
var authorizer = new RequestAuthorizer(stack, "MyAuthorizer", new RequestAuthorizerProps {
Handler = authorizerFn,
IdentitySources = new [] { IdentitySource.Header("Authorization"), IdentitySource.QueryString("allow") }
});
var secondAuthorizer = new RequestAuthorizer(stack, "MySecondAuthorizer", new RequestAuthorizerProps {
Handler = authorizerFn,
IdentitySources = new [] { IdentitySource.Header("Authorization"), IdentitySource.QueryString("allow") }
});
restapi.Root.AddMethod("ANY", new MockIntegration(new IntegrationOptions {
IntegrationResponses = new [] { new IntegrationResponse { StatusCode = "200" } },
PassthroughBehavior = PassthroughBehavior.NEVER,
RequestTemplates = new Dictionary<string, string> {
{ "application/json", "{ \"statusCode\": 200 }" }
}
}), new MethodOptions {
MethodResponses = new [] { new MethodResponse { StatusCode = "200" } },
Authorizer = authorizer
});
restapi.Root.ResourceForPath("auth").AddMethod("ANY", new MockIntegration(new IntegrationOptions {
IntegrationResponses = new [] { new IntegrationResponse { StatusCode = "200" } },
PassthroughBehavior = PassthroughBehavior.NEVER,
RequestTemplates = new Dictionary<string, string> {
{ "application/json", "{ \"statusCode\": 200 }" }
}
}), new MethodOptions {
MethodResponses = new [] { new MethodResponse { StatusCode = "200" } },
Authorizer = secondAuthorizer
});
Synopsis
Constructors
MockIntegration(IIntegrationOptions?) | This type of integration lets API Gateway return a response without sending the request further to the backend. |
Constructors
MockIntegration(IIntegrationOptions?)
This type of integration lets API Gateway return a response without sending the request further to the backend.
public MockIntegration(IIntegrationOptions? options = null)
Parameters
- options IIntegrationOptions
Remarks
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.
ExampleMetadata: lit=aws-apigateway/test/authorizers/integ.request-authorizer.lit.ts infused
Examples
using Path;
using Amazon.CDK.AWS.Lambda;
using Amazon.CDK;
using Amazon.CDK.AWS.APIGateway;
// 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
var app = new App();
var stack = new Stack(app, "RequestAuthorizerInteg");
var authorizerFn = new Function(stack, "MyAuthorizerFunction", new FunctionProps {
Runtime = Runtime.NODEJS_LATEST,
Handler = "index.handler",
Code = AssetCode.FromAsset(Join(__dirname, "integ.request-authorizer.handler"))
});
var restapi = new RestApi(stack, "MyRestApi", new RestApiProps { CloudWatchRole = true });
var authorizer = new RequestAuthorizer(stack, "MyAuthorizer", new RequestAuthorizerProps {
Handler = authorizerFn,
IdentitySources = new [] { IdentitySource.Header("Authorization"), IdentitySource.QueryString("allow") }
});
var secondAuthorizer = new RequestAuthorizer(stack, "MySecondAuthorizer", new RequestAuthorizerProps {
Handler = authorizerFn,
IdentitySources = new [] { IdentitySource.Header("Authorization"), IdentitySource.QueryString("allow") }
});
restapi.Root.AddMethod("ANY", new MockIntegration(new IntegrationOptions {
IntegrationResponses = new [] { new IntegrationResponse { StatusCode = "200" } },
PassthroughBehavior = PassthroughBehavior.NEVER,
RequestTemplates = new Dictionary<string, string> {
{ "application/json", "{ \"statusCode\": 200 }" }
}
}), new MethodOptions {
MethodResponses = new [] { new MethodResponse { StatusCode = "200" } },
Authorizer = authorizer
});
restapi.Root.ResourceForPath("auth").AddMethod("ANY", new MockIntegration(new IntegrationOptions {
IntegrationResponses = new [] { new IntegrationResponse { StatusCode = "200" } },
PassthroughBehavior = PassthroughBehavior.NEVER,
RequestTemplates = new Dictionary<string, string> {
{ "application/json", "{ \"statusCode\": 200 }" }
}
}), new MethodOptions {
MethodResponses = new [] { new MethodResponse { StatusCode = "200" } },
Authorizer = secondAuthorizer
});