Converting mocks to common format
The structure of the captured URL varies depending on whether the request was captured from your IDE, or a dev cluster, or it was manually created. Once you have created the mocks using any combination of the three sources, you can edit the mocks to use a homogeneous request format, making it more convenient to utilize the mocks.
Understanding mock API paths
Let’s review the requests from earlier to outline the process. We had generated three example requests from 3 different sources in the previous sections.
API path for Mesh Dynamics mocks
Mesh Dynamics internally treats the captured urls as a combination of a service name followed by an API path. The service name is the text up to the first '/'. The rest of the captured url is the API path. Here are the service and API path names derived from the 3 captures above:
One option for using the mocks is to simply add the Mesh Dynamics mock server address before the captured URL.
While this is convenient, it may be hard to keep track of the source if you are using a mix of mocks created from different sources. Mesh Dynamics supports normalizing the API paths so that you can use a consistent format for your mock URLs when developing.
Normalize to common format
Let’s convert the urls to use a common URL format for the mock requests. We will go through the example using the url from the IDE. The process is the same for the url captured from the dev cluster.
- Retrieve the saved request from the collection. Open the collection, and click on the request to bring the request into API editor.If you already have the request open in a tab, then clicking on the request in the collection will not open a new tab. It will just shift the focus to the tab already open with the request.
- Change the service name to movieinfo. Click on the edit icon for the service name to edit it.
- Enter movieinfo in the box and press Enter to update the service name.
- In the request input box, delete the ‘http://localhost:8080/MIRest/’ (including the trailing ‘/’) from the url path. You should be left with minfo/listmovies in the request box.You can add or delete from the existing path. You just need to make sure that the consumer APIs use the correct URL.
- Now save the request back to the collection by clicking the Save button.
Your collection is updated with the service name and API path in the desired format.
Verify the mock
Now that the mock has been created using the desired API path format, let’s make a request and confirm that it works correctly.
- Open a new tab in API Studio, and create an API request with the API path and parameters for the API. However, to run the request you need to specify the host. The host in this case will the local API Studio proxy at port 9000 (which we had discussed earlier). You will need to append the API path to the mock server. The full request including the mock server address and the API path for your API will be:
- Add the query parameters in API editor.
- Click Run to send the request to the Mesh Dynamics mock server. You will get back the response in the right-hand pane.