API Mocking

Generating a Mocked Response using API Mocking

What is API Mocking?

During API development, you might have the following concerns:

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.

Mocking an API

The following figure shows the high-level steps for mocking an API.

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 on Deactivate and click Yes to confirm.

  5. Click the ellipsis icon icon and select Enable Mocking.

Enrich Mocking

  1. Select APIs > PetStore.

  2. Click Edit > API mocking.

  3. Select /pet/findByStatus resource from the list of resources.

  4. To add a response of your own:

    • Click Add Response and select Status code. Choose 200 from the drop-down menu.
    • 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:

    • Click Add Conditions and specify the Condition name, for example, SuccessNoContent.
    • Select the type of Condition parameter to which the condition must be applied. You can choose one of the following options: Body, Header or Query Parameter.
    • Specify the Key value, which can be a JSON path or XML path.
    • Add a value to your condition.
    • Select a status code, say 204, from the drop-down list.

    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:

    • Select Add Response Header and specify the Header key as MockedResponse and Header value as True. These values are contained in the HTTP response.
    • 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:

    • Click Add content-type and select a content type value from the drop-down list.
    • Specify a mock response payload for the selected content-type value.
    • 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.

Validate the 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

  3. Open Postman.

  4. Open a new tab and paste this link under Untitled request.

  5. Click Send to get the 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.