MockIntegration

class aws_cdk.aws_apigateway.MockIntegration(*, cache_key_parameters=None, cache_namespace=None, connection_type=None, content_handling=None, credentials_passthrough=None, credentials_role=None, integration_responses=None, passthrough_behavior=None, request_parameters=None, request_templates=None, timeout=None, vpc_link=None)

Bases: 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.

ExampleMetadata:

lit=aws-apigateway/test/authorizers/integ.request-authorizer.lit.ts infused

Example:

from aws_cdk.aws_apigateway import IntegrationResponse, MethodResponse, IntegrationResponse, MethodResponse
import path as path
import aws_cdk.aws_lambda as lambda_
from aws_cdk import App, Stack
from aws_cdk.aws_apigateway import MockIntegration, PassthroughBehavior, RestApi, RequestAuthorizer, IdentitySource

# 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

app = App()
stack = Stack(app, "RequestAuthorizerInteg")

authorizer_fn = lambda_.Function(stack, "MyAuthorizerFunction",
    runtime=lambda_.Runtime.NODEJS_LATEST,
    handler="index.handler",
    code=lambda_.AssetCode.from_asset(path.join(__dirname, "integ.request-authorizer.handler"))
)

restapi = RestApi(stack, "MyRestApi", cloud_watch_role=True)

authorizer = RequestAuthorizer(stack, "MyAuthorizer",
    handler=authorizer_fn,
    identity_sources=[IdentitySource.header("Authorization"), IdentitySource.query_string("allow")]
)

second_authorizer = RequestAuthorizer(stack, "MySecondAuthorizer",
    handler=authorizer_fn,
    identity_sources=[IdentitySource.header("Authorization"), IdentitySource.query_string("allow")]
)

restapi.root.add_method("ANY", MockIntegration(
    integration_responses=[IntegrationResponse(status_code="200")
    ],
    passthrough_behavior=PassthroughBehavior.NEVER,
    request_templates={
        "application/json": "{ "statusCode": 200 }"
    }
),
    method_responses=[MethodResponse(status_code="200")
    ],
    authorizer=authorizer
)

restapi.root.resource_for_path("auth").add_method("ANY", MockIntegration(
    integration_responses=[IntegrationResponse(status_code="200")
    ],
    passthrough_behavior=PassthroughBehavior.NEVER,
    request_templates={
        "application/json": "{ "statusCode": 200 }"
    }
),
    method_responses=[MethodResponse(status_code="200")
    ],
    authorizer=second_authorizer
)
Parameters:
  • cache_key_parameters (Optional[Sequence[str]]) – A list of request parameters whose values are to be cached. It determines request parameters that will make it into the cache key.

  • cache_namespace (Optional[str]) – An API-specific tag group of related cached parameters.

  • connection_type (Optional[ConnectionType]) – The type of network connection to the integration endpoint. Default: - ConnectionType.VPC_LINK if vpcLink property is configured; ConnectionType.Internet otherwise.

  • content_handling (Optional[ContentHandling]) – Specifies how to handle request payload content type conversions. Default: none if this property isn’t defined, the request payload is passed through from the method request to the integration request without modification, provided that the passthroughBehaviors property is configured to support payload pass-through.

  • credentials_passthrough (Optional[bool]) – Requires that the caller’s identity be passed through from the request. Default: Caller identity is not passed through

  • credentials_role (Optional[IRole]) – An IAM role that API Gateway assumes. Mutually exclusive with credentialsPassThrough. Default: A role is not assumed

  • integration_responses (Optional[Sequence[Union[IntegrationResponse, Dict[str, Any]]]]) – The response that API Gateway provides after a method’s backend completes processing a request. API Gateway intercepts the response from the backend so that you can control how API Gateway surfaces backend responses. For example, you can map the backend status codes to codes that you define.

  • passthrough_behavior (Optional[PassthroughBehavior]) – Specifies the pass-through behavior for incoming requests based on the Content-Type header in the request, and the available mapping templates specified as the requestTemplates property on the Integration resource. There are three valid values: WHEN_NO_MATCH, WHEN_NO_TEMPLATES, and NEVER.

  • request_parameters (Optional[Mapping[str, str]]) – The request parameters that API Gateway sends with the backend request. Specify request parameters as key-value pairs (string-to-string mappings), with a destination as the key and a source as the value. Specify the destination by using the following pattern integration.request.location.name, where location is querystring, path, or header, and name is a valid, unique parameter name. The source must be an existing method request parameter or a static value. You must enclose static values in single quotation marks and pre-encode these values based on their destination in the request.

  • request_templates (Optional[Mapping[str, str]]) – A map of Apache Velocity templates that are applied on the request payload. The template that API Gateway uses is based on the value of the Content-Type header that’s sent by the client. The content type value is the key, and the template is the value (specified as a string), such as the following snippet:: { “application/json”: “{ "statusCode": 200 }” }

  • timeout (Optional[Duration]) – The maximum amount of time an integration will run before it returns without a response. Must be between 50 milliseconds and 29 seconds. Default: Duration.seconds(29)

  • vpc_link (Optional[IVpcLink]) – The VpcLink used for the integration. Required if connectionType is VPC_LINK

Methods

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.

Parameters:

_method (Method) –

Return type:

IntegrationConfig