Using the Mock Response middleware with Tyk Classic APIs
Last updated: 5 minutes read.
The Mock Response middleware allows you to configure Tyk to return a response for an API endpoint without requiring an upstream service. This can be useful when creating a new API or making a development API available to an external team.
When working with Tyk Classic APIs, this middleware is executed at the start of the request processing chain. Thus an endpoint with the mock response middleware will not be authenticated, will not process other middleware configured for the API (neither API nor endpoint level) and will have no analytics created. It will simply return the configured response for any request made to that endpoint.
The middleware is configured in the Tyk Classic API Definition. You can do this via the Tyk Dashboard API, the API Designer or in Tyk Operator.
If you’re using the newer Tyk OAS APIs, then check out the Tyk OAS page.
Configuring the middleware in the Tyk Classic API Definition
If you’re using Tyk Operator then check out the configuring the middleware in Tyk Operator section below.
To enable mock response, you must first add the endpoint to a list - one of allow list, block list or ignore authentication. This will add a new object to the extended_paths
section of your API definition - white_list
, black_list
or ignored
. The mock response can then be configured within the method_actions
element within the new object.
The white_list
, black_list
and ignored
objects all have the same structure and configuration as follows:
path
: the endpoint pathmethod
: this should be blankignore_case
: if set totrue
then the path matching will be case insensitivemethod_actions
: the configuration of the mock response
The method_actions
object should be configured as follows, with an entry created for each method on the path for which you wish to configure the mock response:
action
: this should be set toreply
code
: the HTTP status code to be provided with the responseheaders
: the headers to inject with the responsedata
: the payload to be returned as the body of the response
For example:
|
|
In this example the mock response middleware has been configured for requests to the GET /anything
endpoint. The allow list middleware has been enabled for this endpoint and is case sensitive, so calls to GET /Anything
will not return the mock response.
A call to GET /anything
would return:
HTTP/1.1 200 OK
X-Example-Header: foobar
Date: Wed, 31 Jan 2024 16:21:05 GMT
Content-Length: 30
Content-Type: text/plain; charset=utf-8
This is the mock response body
Configuring the middleware in the API Designer
You can use the API Designer in the Tyk Dashboard to configure the Mock Response middleware for your Tyk Classic API by following these steps.
Step 1: Add an endpoint for the path and configure a list plugin
For the mock response to be enabled, the endpoint must also be in a list. We recommend adding the path to an allow list by selecting the Whitelist plugin.
Step 2: Add the mock response plugin
Now select the Mock response plugin.
Step 3: Configure the middleware
Once you have selected the Mock response middleware for the endpoint, you can configure the HTTP status code, headers and body to be included in the response. Remember to click ADD, to add each header to the response.
Step 4: Save the API
Use the save or create buttons to save the changes and activate the middleware.
Note
For the mock response to be enabled, the endpoint must also be in a list. We recommend adding the path to an allow list. If this isn’t done, then the mock will not be saved when you save your API in the designer.
Configuring the middleware in Tyk Operator
The process of configuring a mock response is similar to that defined in the configuring the middleware in Tyk Classic API definition section.
To configure a mock response, you must first add a mock response configuration object to the extended_paths
section, e.g. one of allow list (white_list
), block list (black_list
) or ignore authentication (ignore
). The mock response configuration object has the following properties:
- path: the path of the endpoint, e.g
/foo
. - ignore_case: when set to true the path matching is case insensitive.
- method_actions: a configuration object that allows the mock response to be configured for a given method, including the response body, response headers and status code. This object should also contain an
action
field with a value set toreply
.
In the example below we can see that a mock response is configured to ignore authentication (ignore
) for the GET /foo
endpoint. When a request is made to the endpoint then authentication will be ignored and a mock response is returned with status code 200
and a response body payload of {"foo": "bar"}
. The middleware has been configured to be case sensitive, so calls to GET /Foo
will not ignore authentication.
|
|