Skip to content

About

Gravitee Policy - Mock

Topics

Resources

Contributing

Stars

1 star

Watchers

23 watching

Forks

Repository files navigation

Mock Policy

Gravitee.io License Releases CircleCI

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.

Note: The Mock policy will not cause the other policies to be skipped, regardless of its location in the flow.

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

Usage

Expression Language support

The response body content and header values support Gravitee Expression Language (EL). This allows you to create dynamic mock responses based on request context.

Available EL variables include:

  • {#request.paths[n]} — Access path segments
  • {#request.headers['X-Custom']} — Access request headers
  • {#properties['key']} — Access API properties

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()}
}

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>

Automatic content-type detection

You don't need to provide the Content-Type header. The Mock policy automatically detects the content type of the response body (JSON, XML, or plain text).

Phases

The mock policy can be applied to the following API types and flow phases.

Compatible API types

  • PROXY
  • MCP PROXY

Supported flow phases:

  • Request

Compatibility matrix

Strikethrough text indicates that a version is deprecated.

Plugin version APIM Java version
2.0.0 and after 4.11 and after 21
1.14.0 to 1.15.x 4.1.24 and after 17
Up to 1.13.5 All supported versions 11

Configuration options

Name
json name
Type
constraint
Mandatory Description
Response body
content
string The body content of the mock response. Supports Expression Language for dynamic values.
Headers
headers
array Custom HTTP headers to include in the mock response. Header values support Expression Language.
See "Headers" section.
HTTP Status Code
status
enum (string) ✅ HTTP status code returned to the consumer in the mock response.
Values: 100 101 102 200 201 202 203 204 205 206 207 300 301 302 303 304 305 307 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 422 423 424 429 500 501 502 503 504 505 507

Headers (Array)

Name
json name
Type
constraint
Mandatory Description
Name
name
string Name of the header
Value
value
string Value of the header (support EL)

Examples

Mock a JSON response with Expression Language

{
  "api": {
    "definitionVersion": "V4",
    "type": "PROXY",
    "name": "Mock Policy example API",
    "flows": [
      {
        "name": "Common Flow",
        "enabled": true,
        "selectors": [
          {
            "type": "HTTP",
            "path": "/",
            "pathOperator": "STARTS_WITH"
          }
        ],
        "request": [
          {
            "name": "Mock Policy",
            "enabled": true,
            "policy": "mock",
            "configuration":
              {
                  "status": "200",
                  "headers": [
                      {
                          "name": "Content-Type",
                          "value": "application/json"
                      },
                      {
                          "name": "Server",
                          "value": "Gravitee.io"
                      }
                  ],
                  "content": "{\"id\": \"{#request.paths[3]}\", \"name\": \"{#properties['name']}\"}"
              }
          }
        ]
      }
    ]
  }
}

Mock a JSON response on MCP Proxy API

{
  "api": {
    "definitionVersion": "V4",
    "type": "MCP_PROXY",
    "name": "Mock Policy example API",
    "flows": [
      {
        "name": "All plans flow",
        "enabled": true,
        "selectors": [
          {
            "type": "MCP",
             "methods" : []
          }
        ],
        "request": [
          {
            "name": "Mock Policy",
            "enabled": true,
            "policy": "mock",
            "configuration":
              {
                  "status": "200",
                  "headers": [
                      {
                          "name": "Content-Type",
                          "value": "application/json"
                      },
                      {
                          "name": "Server",
                          "value": "Gravitee.io"
                      }
                  ],
                  "content": "{\"id\": \"{#request.paths[3]}\", \"name\": \"{#properties['name']}\"}"
              }
          }
        ]
      }
    ]
  }
}

Changelog

2.0.0 (2026-02-13)

Features
BREAKING CHANGES
  • compatible with APIM >= 4.11

1.15.0 (2025-12-11)

Features
  • enable for MCP Proxy API (d4c0957)

1.15.0-alpha.1 (2025-11-07)

Features
  • enable for MCP Proxy API (c58383b)

1.14.2 (2025-07-06)

Bug Fixes
  • ensure example values with 'string' + 'date-time' format are handled correctly (1c31dea)

1.14.1 (2025-03-11)

Bug Fixes
  • rework response example extractor from schema (9915115)

1.14.0 (2024-09-18)

Features
  • support mock policy on v4 proxy API request phase (8251445)
  • update schema-form for v4 policy studio (ce1db74)

1.13.5 (2023-10-31)

Bug Fixes
  • jsonSchema - config code editor mode for new policy studio display (d13ba38)

1.13.4 (2023-09-28)

Bug Fixes
  • handle array in response (1f97ed8)

1.13.3 (2023-09-27)

Bug Fixes
  • handle no components in OpenAPI descriptor (49904ac)

1.13.2 (2023-07-20)

Bug Fixes

1.13.1 (2023-07-20)

Bug Fixes
  • update policy description (77dae49)

1.13.0 (2022-01-21)

Features

About

Gravitee Policy - Mock

Topics

Resources

Contributing

Stars

1 star

Watchers

23 watching

Forks

Releases

Packages

Used by

Contributors

Languages