API Gateway 10.15 | Getting Started with API Gateway | API Mocking
 
API Mocking
During API development, you might have the following concerns:
*How can I test my API while my native API is still under development?
*How can I continuously test my APIs on an ongoing basis, for example, CI-CD pipelines, without any external dependencies, especially, under circumstances where the backend is subjected to downtimes or requires access credentials that are changed quite frequently?
Mocking is the capability that helps you accelerate your development lifecycles by reducing the dependencies between your development teams, especially the API consumer and the backend. API Gateway helps mock an API. The mocked API provides realistic native API responses to requests. You can mock an API by simulating the native API, using the API mocking capability in API Gateway. For example, if you have an API without native implementation, you can mock that API. When the API is invoked, the mock API server returns the mocked response to the client that invoked the API.
API Gateway allows you to modify the default mocked response and configure it, as required, for each combination of resource, operation, status code, and content-type based on the examples and schemas specified in that API. In addition, you can add a condition to the operation in the resource.
API Gateway supports mocking ability for both REST and SOAP APIs. This example explains how you can use the mocking functionality for a REST API. In this example, you enable mocking for a REST API, PetStore API, modify the mocking details, and verify the mocked response as per the configured mocked response.
The following figure shows the high-level steps for mocking an API.
mock API high level steps
Step 1: Enable API Mocking
1. Log on to API Gateway.
2. Select APIs > PetStore.
3. Click API Details tab
4. Ensure that the API is in Deactivate state.
If the API is not in deactivated state, click Deactivate and click Yes to confirm.
5. Click the ellipsis icon ellipsis icon and select Enable mocking.
enable mocking
Step 2: Enrich Mocking
1. Select APIs > PetStore.
2. Click Edit > API Mocking.
3. Select /pet/findByStatus resource from the list of resources. (image - gs-enrich-mocking
4. To add a response of your own:
a. Click Add Response and select Status code. Select 200 from the drop-down menu.
b. Click Add to add it to the existing code list.
5. In certain instances, the mock API server sends mocked response based on certain conditions and parameters. You can add a condition to the operation by performing the following steps:
a. Click Add Conditions and specify the Condition name, for example, SuccessNoContent.
b. Select the type of Condition parameter to which the condition must be applied.
c. Specify the Key value, which can be a JSON path or XML path.
d. Add a value to your condition.
e. Select a status code, say 204, from the drop-down list.
add condition
Note:
To use the code 506, you must enable the property Send native provider fault in the section Administration > General > API fault. The query parameter must be set to expectStatusCode, while invoking an API, to have correct mock responses for status codes 100 and 202.
6. If you want to add a response header for the API, perform the following steps:
a. Select Add Response Header and specify the Header key as MockedResponse and Header value as True. These values are contained in the HTTP response.
b. Click Add to add the header value to the list of response headers.
7. Similarly, to add a content-type to the selected status code, perform the following steps:
a. Click Add content-type and select a content type value from the drop-down list.
b. Specify a mock response payload for the selected content-type value.
c. Select Add to save your selection. You can add multiple content-types.
8. Click Save to save your changes. There are multiple options to mock an API. For more information regarding different ways to mock an API, see API Mocking.
Step 3: Validate Mocked Response
1. Activate the Petstore API by clicking the Activate button.
2. Navigate to the Technical information tab and copy the link under Gateway localhost(s) or copy http:/env233186.apigw-aw-eu.webmethods.io/gateway/Petstore/2
validate mock API
3. Open Postman.
4. Open a new tab and paste this link under Untitled request.
5. Click Send to get the mocked response.
mocked response
6. To disable mocking, click Deactivate to deactivate the API.
7. Click ellipsis and select Disable mocking.
8. Click Activate to activate the API.
9. Go to Postman and click Send. You now see the actual response of the Petstore API.
actual response