Skip to content

test_function async

test_function(
    name: FunctionName,
    if_match: string,
    event_object: FunctionEventObject,
    *,
    config_overrides: Optional[AsyncCloudFrontClientConfig] = None,
    stage: Optional[FunctionStage] = None,
) -> TestFunctionResult

Tests a CloudFront function.

To test a function, you provide an event object that represents an HTTP request or response that your CloudFront distribution could receive in production. CloudFront runs the function, passing it the event object that you provided, and returns the function's result (the modified event object) in the response. The response also contains function logs and error messages, if any exist. For more information about testing functions, see Testing functions in the Amazon CloudFront Developer Guide.

To test a function, you provide the function's name and version (ETag value) along with the event object. To get the function's name and version, you can use ListFunctions and DescribeFunction.

Parameters:

  • name (FunctionName) –

    The name of the function that you are testing.

  • if_match (string) –

    The current version (ETag value) of the function that you are testing, which you can get using DescribeFunction.

  • stage (Optional[FunctionStage], default: None ) –

    The stage of the function that you are testing, either DEVELOPMENT or LIVE.

  • event_object (FunctionEventObject) –

    The event object to test the function with. For more information about the structure of the event object, see Testing functions in the Amazon CloudFront Developer Guide.

Raises:

Source code in docs_src/capo_cloudfront/_services/async_cloud_front.py
async def test_function(
    self,
    name: "capo_cloudfront.types.function_name.FunctionName",
    if_match: "capo_cloudfront.types.string.string",
    event_object: "capo_cloudfront.types.function_event_object.FunctionEventObject",
    *,
    config_overrides: Optional[AsyncCloudFrontClientConfig] = None,
    stage: Optional["capo_cloudfront.types.function_stage.FunctionStage"] = None,
) -> "capo_cloudfront.types.test_function_result.TestFunctionResult":
    """<p>Tests a CloudFront function.</p> <p>To test a function, you provide an <i>event object</i> that represents an HTTP request or response that your CloudFront distribution could receive in production. CloudFront runs the function, passing it the event object that you provided, and returns the function's result (the modified event object) in the response. The response also contains function logs and error messages, if any exist. For more information about testing functions, see <a href="https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/managing-functions.html#test-function">Testing functions</a> in the <i>Amazon CloudFront Developer Guide</i>.</p> <p>To test a function, you provide the function's name and version (<code>ETag</code> value) along with the event object. To get the function's name and version, you can use <code>ListFunctions</code> and <code>DescribeFunction</code>.</p>

    Args:
        name: <p>The name of the function that you are testing.</p>
        if_match: <p>The current version (<code>ETag</code> value) of the function that you are testing, which you can get using <code>DescribeFunction</code>.</p>
        stage: <p>The stage of the function that you are testing, either <code>DEVELOPMENT</code> or <code>LIVE</code>.</p>
        event_object: <p>The event object to test the function with. For more information about the structure of the event object, see <a href="https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/managing-functions.html#test-function">Testing functions</a> in the <i>Amazon CloudFront Developer Guide</i>.</p>

    Raises:
        capo_cloudfront.errors.invalid_argument.InvalidArgument: <p>An argument is invalid.</p>
        capo_cloudfront.errors.invalid_if_match_version.InvalidIfMatchVersion: <p>The <code>If-Match</code> version is missing or not valid.</p>
        capo_cloudfront.errors.no_such_function_exists.NoSuchFunctionExists: <p>The function does not exist.</p>
        capo_cloudfront.errors.test_function_failed.TestFunctionFailed: <p>The CloudFront function failed.</p>
        capo_cloudfront.errors.unsupported_operation.UnsupportedOperation: <p>This operation is not supported in this Amazon Web Services Region.</p>
        capo_cloudfront.errors.UnknownServiceError: The service returned an error code this client does not model.
    """

    async def _handler(
        req: "AsyncOperationRequest[capo_cloudfront.types.test_function_request.TestFunctionRequest]",
    ) -> AsyncOperationResponse[
        "capo_cloudfront.types.test_function_result.TestFunctionResult"
    ]:
        import capo_cloudfront._operations.cloudfront2020_05_31.test_function

        (
            output,
            http_response,
        ) = await capo_cloudfront._operations.cloudfront2020_05_31.test_function.async_test_function(
            req.options, req.input
        )
        return AsyncOperationResponse(output=output, response=http_response)

    def _redact_input(
        input_: "capo_cloudfront.types.test_function_request.TestFunctionRequest",
    ) -> dict[str, Any]:
        import capo_cloudfront.types.test_function_request

        return capo_cloudfront.types.test_function_request.redact_sensitive(input_)

    def _redact_output(
        output: "capo_cloudfront.types.test_function_result.TestFunctionResult",
    ) -> dict[str, Any]:
        import capo_cloudfront.types.test_function_result

        return capo_cloudfront.types.test_function_result.redact_sensitive(output)

    interceptors_, options_ = self.operation_options(
        config_overrides,
        OperationLogging(
            "TestFunction", redact_input=_redact_input, redact_output=_redact_output
        ),
    )
    input_: capo_cloudfront.types.test_function_request.TestFunctionRequest = {
        "name": name,
        "if_match": if_match,
        "event_object": event_object,
    }
    if stage is not None:
        input_["stage"] = stage

    response = await aexecute_pipeline(
        AsyncOperationRequest(input=input_, options=options_),
        handler=_handler,
        interceptors=list(interceptors_),
    )
    await response.response.aclose()
    return response.output