Converting mocks to common format

Updated 3 months ago by Rahul Lahiri

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.

Source

API path for Mesh Dynamics mocks

IDE

localhost:8080/MIRest/minfo/listmovies

Dev cluster

moviebook.meshdynamics.io/minfo/listmovies

Manually created

movieinfo/minfo/listmovies

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:

Source

Service name

API path

IDE

localhost:8080

/MIRest/minfo/listmovies

Dev cluster

moviebook.meshdynamics.io

/minfo/listmovies

Manually created

movieinfo

/minfo/listmovies

One option for using the mocks is to simply add the Mesh Dynamics mock server address before the captured URL.

Source

Host

Original URL

Mock URL

IDE

N/A

localhost:8080/MIRest/minfo/listmovies

localhost:9000/localhost:8080/MIRest/minfo/listmovies

Dev cluster

N/A

moviebook.meshdynamics.io/minfo/listmovies

localhost:9000/moviebook.meshdynamics.io/minfo/listmovies

Manually created

N/A

movieinfo/minfo/listmovies

localhost:9000/movieinfo/minfo/listmovies

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.

You do not need to convert the mocks to a common format. However if you are using a variety of sources for the mocks, it may get challenging to track the source. Normalizing the mock URLs to a common format will make the maintenance process easier.
  1. 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.
  2. Change the service name to movieinfo. Click on the edit icon for the service name to edit it.
  3. Enter movieinfo in the box and press Enter to update the service name.
  4. 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.
  5. 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.

  1. 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:
    http://localhost:9000/movieinfo/minfo/listmovies
  2. Add the query parameters in API editor.

    Name

    Value

    filmName

    ali forever

  3. Click Run to send the request to the Mesh Dynamics mock server. You will get back the response in the right-hand pane.


How did we do?