Blog

- July 17, 2017

APIs defined in Restlet Studio now benefit from mocks which will simulate the API’s behavior. To activate your API mock, open Restlet Studio, navigate to your API’s settings page, and hit enable.

Why mock?

API mocks can bring several improvements to your development workflow:

  • They can be used during the design phase to prototype and validate an API design before implementing it.
  • They allow developers to work on different parts of a system at the same time by decoupling the backend (which implements the API) from the frontend (which consumes it)

Mocking has been around for a long time, and can take many forms, from testing libraries that mock API calls above the network layer, to online servers.

A common point is that they often require you to provide a model of the request / responses you wish to mock with a lot of detail, essentially making a stripped-down implementation of your API contract. For example, matching the right request path or method is often your responsibility.

Contract-based mocking in Restlet Studio

This is where mocking based on an API contract shines. Since the contract occupies a central position between the producer and consumer of the API, it makes a lot of sense to leverage it for mocking.

You get to reuse the information already present in the contract, such as your data types, resource paths, methods, responses and header definitions.

As an added benefit, it stands as single source of truth between your API producer and consumer. In Restlet Studio, this means that your mock always matches the most recent version of your API contract.

What will Studio leverage from your API contract?

Types Describe the data exchanged by your API, including types and a lot of other attributes.
Resources The skeleton of your API. Identified by their paths, resources are used to route the incoming requests.
Methods HTTP verbs that identify the actions you can perform.
Responses Contain the response code, headers, and bodies that can be returned.

From API contract to auto-generated response

When generating a response body, Restlet Studio mocks will give priority to user-provided data by looking for an example field, then a default value, then an enum field. If one is found, it will be used as an example, otherwise a JSON example will be generated based on the data type returned by the operation being called.

As an example, consider the Company data type definition shown below.

When mocking is activated, a GET call made to the /company/{companyId} resource will return an automatically generated JSON response based on the structure of the data type, as shown below using Restlet Client.

Note how the mock will do a number of interesting things for you including generating values for custom response headers (e.g. x-my-custom-header), identifying arrays and creating appropriate examples (e.g. the tags property), and also identifying nested structures and generating values for those too (e.g. the address property).

Content negotiation

In most API contract languages, a response body can be associated with one or more media types. With Restlet Studio’s mocks, you can use the Accept request header to select which body should be returned among several options (each having its own example), therefore changing the examples returned by the mock.

Making requests to your mock using Restlet Client

Once you have crafted your API contract, Restlet Studio makes it very easy to make requests to your API mock.
You can either try a single operation by using the “Try in Client” button in the operation page, or you can export the whole API as a project in Restlet Client via the main “Try in Client” button.

What about you?

What kind of mock would be ideal for your development workflow? Please share your thoughts with us at support@restlet.com.

For more information on how to configure and use API mocks, please refer to the Restlet Studio user guide.