Mocks from APIs

Updated 3 months ago by Rahul Lahiri

Mocks can be created from APIs that are already live. The API could be available in your IDE or in a dev cluster. In either case, it is easy to create mocks for the target APIs by sending API requests to the service from the API Studio.

Service in IDE

The API you want to create mocks for is running in your IDE. If the API in your IDE needs to use other services, then configure your setup (both API Studio and the service in the IDE) to use one of the configurations below. The configuration used makes no difference in the end result. The only difference is whether the trace shown in the API editor includes the egress requests or not. The full trace is not required to serve the mocks.

API Studio supports two configurations for the egress requests from the service in the IDE. You can use either configuration for creating the mocks -- no changes are needed from your current setup.

Egress request configuration: Send egress requests from the IDE directly to a dev cluster

API Studio sends the API request to the service in the IDE. The egress requests from the service in the IDE are sent directly to the dev cluster where the provider services are running.

Egress request configuration: Send the egress requests from the IDE to API Studio

API Studio sends the API request to the service in the IDE. The egress requests from the service in the IDE are sent back to the API Studio proxy. The API Studio records the egress requests, and then forwards the request to the dev cluster where the provider services are running.

For help with the egress request routing configuration, please refer to the API Studio quickstart guide.

Create a collection

Follow the steps below to create mocks for the APIs running in your IDE. For the example screenshots below, the egress requests from the API in the IDE are being routed back to API Studio before being forwarded to the dev cluster (the second configuration above).

  1. From the API editor, create a collection to store the requests. Select the Collections tab and then click on New collection. Enter a name for the collection and save it.
  1. In API editor, open a new tab, and create a request. The request should be sent to the localhost port where the API is listening. The request should include the localhost port, API path, query parameters, body, and headers as required.
  1. From the API editor, configure the egress request service configuration to use the appropriate source for servicing the egress requests. In the configuration below, we have set the configuration to use the dev cluster for all the upstream (provider) services.
  1. Run the request.
  1. The response comes back to the API Studio and is shown in the right-hand pane. Verify the response to confirm that the response matches the expected response.
You will see the trace of the egress requests only if the setup routes the egress requests through the API Studio. If the service configuration in the IDE is setup to send the egress requests directly to the dev cluster, then you will not see the trace for the egress requests shown in the screenshot. The egress request trace is not required for creating the API mocks.
  1. Set the response as the reference. This will copy the request and response over to the left pane.
  1. Save the request to the target collection.

The API path for the saved request is:

localhost:8080/MIRest/minfo/listmovies

Add as many requests as you need to the collection. Once created, the collection is immediately available for all your collaborators to use both as mocks and as sample requests.

Service in dev cluster

The process of creating the API mocks from a dev cluster is similar to the process described above for the service running locally in an IDE. The only difference is that the API request should be sent to the dev cluster instead of the IDE.

  1. From the API editor, create a collection to store the requests. Select the Collections tab and then click on New collection. Enter a name for the collection and save it.
  2. In the API editor, open a new tab. Create a request to be sent to the dev cluster. In this example, the target url is https://moviebook.meshdynamics.io/minfo/listmovies. Add query parameters, body, and headers as required.
  1. Run the request.
  2. The response comes back to the API Studio and is shown in the right-hand pane. Verify the response to confirm that the response matches the expected response.
You may not get the full trace when the request is sent to the dev cluster, depending on whether the listeners are setup in the dev cluster. You do not need the trace for this application.
  1. Set the response as the reference. This will copy the request and response over to the left pane.
  2. Save the request to the target collection.

The API path for the saved request is:

moviebook.meshdynamics.io/minfo/listmovies


How did we do?