description |
---|
This page provides the technical details of the Mock policy |
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:
{% 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="HTTP 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 %}
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 %}
The phases checked below are supported by the mock
policy:
v2 Phases | Compatible? | v4 Phases | Compatible? |
---|---|---|---|
onRequest | true | onRequest | false |
onResponse | false | onResponse | false |
onRequestContent | false | onMessageRequest | false |
onResponseContent | false | onMessageResponse | false |
The mock policy can be configured with the following options:
Property | Required | Description | Type | Default |
---|---|---|---|---|
status | true | HTTP Status Code | integer | |
headers | true | HTTP Headers | Array of HTTP headers | |
content | true | HTTP Body content | string |
The following is the compatibility matrix for APIM and the mock
policy:
Plugin Version | Supported APIM versions |
---|---|
Up to 1.x | All |
{% @github-files/github-code-block url="https://github.com/gravitee-io/gravitee-policy-mock/blob/master/CHANGELOG.md" %}