Skip to content

Latest commit

 

History

History
105 lines (75 loc) · 4.51 KB

File metadata and controls

105 lines (75 loc) · 4.51 KB
description
This page provides the technical details of the Mock policy

Mock

Overview

You can use the mock policy to create mock responses when a consumer calls one of your services. This means you do not have to provide a functional backend as soon as you create your API, giving you more time to think about your API contract.

You can think of the policy as a contract-first approach — you are able to create a fully-functional API without needing to write a single line of code to handle consumer calls.

Internally, this policy replaces the default HTTP invoker with a mock invoker. There are no more HTTP calls between the Gateway and a remote service or backend.

{% hint style="info" %} The mock policy will not cause the other policies to be skipped, regardless of its location in the flow. {% endhint %}

When defining the response body content, you can use Gravitee Expression Language (EL) to provide a dynamic mock response.

Functional and implementation information for the mock policy is organized into the following sections:

Examples

{% hint style="warning" %} This policy can only be applied to v2 APIs. It cannot be applied to v4 message APIs or v4 proxy APIs. {% endhint %}

{% tabs %} {% tab title="Proxy API example" %} Note that you don’t need to provide the Content-Type header, since the mock policy can automatically detect the content type.

Body content example (XML)

<user id="{#request.paths[3]}">
    <firstname>{#properties['firstname_' + #request.paths[3]]}</firstname>
	<lastname>{#properties['lastname_' + #request.paths[3]]}</lastname>
	<age>{(T(java.lang.Math).random() * 60).intValue()}</age>
	<createdAt>{(new java.util.Date()).getTime()}</createdAt>
</user>

Body content example (JSON)

{
    "id": "{#request.paths[3]}",
    "firstname": "{#properties['firstname_' + #request.paths[3]]}",
    "lastname": "{#properties['lastname_' + #request.paths[3]]}",
    "age": {(T(java.lang.Math).random() * 60).intValue()},
    "createdAt": {(new java.util.Date()).getTime()}
}

{% endtab %} {% endtabs %}

Configuration

Sample policy configuration is shown below:

{% code title="Sample Configuration" %}

"mock": {
    "status": "200",
    "headers": [
        {
            "name": "Content-Type",
            "value": "application/json"
        }, {
            "name": "Server",
            "value": "Gravitee.io"
        }
    ],
    "content": "<user id=\"{#request.paths[3]}\">\n\t<firstname>{#properties['firstname_' + #request.paths[3]]}</firstname>\n\t<lastname>{#properties['lastname_' + #request.paths[3]]}</lastname>\n\t<age>{(T(java.lang.Math).random() * 60).intValue()}</age>\n\t<createdAt>{(new java.util.Date()).getTime()}</createdAt>\n</user>"
}

{% endcode %}

Phases

The phases checked below are supported by the mock policy:

v2 PhasesCompatible?v4 PhasesCompatible?
onRequesttrueonRequestfalse
onResponsefalseonResponsefalse
onRequestContentfalseonMessageRequestfalse
onResponseContentfalseonMessageResponsefalse

Options

The mock policy can be configured with the following options:

PropertyRequiredDescriptionTypeDefault
statustrueHTTP Status Codeinteger
headerstrueHTTP HeadersArray of HTTP headers
contenttrueHTTP Body contentstring

Compatibility matrix

The following is the compatibility matrix for APIM and the mock policy:

Plugin VersionSupported APIM versions
Up to 1.xAll

Changelogs

{% @github-files/github-code-block url="https://github.com/gravitee-io/gravitee-policy-mock/blob/master/CHANGELOG.md" %}