Discover our knowledge. Read our blogs!

Learn more

We build all our solutions with WSO2 and we are proud that we are Platinum Value-Added Reseller of WSO2.

Learn more

How to mock an API with WSO2 API Manager

5 min read

What is API Mocking?

How to mock an API with WSO2 API ManagerAPI Mocking is nothing but an impersonation of a real API that returns pre-defined response, response code, headers, et cetera, rather than real response and parameters.
 

How can API Mocking be useful?

  • Quick testing feedback and for early bug detection and fixing;
  • Get practical insights of an API into critical implementation and usability issues with little effort;
  • Parallel and non-blocking development with front-end;
  • Useful in supporting DevOps by reducing dependencies;

Mocking an API with WSO2 API Manager

With the new version of WSO2 API Manager, the issue for mocking an API has been addressed quite effectively. You can deploy a new API or new version of an existing API as a prototype. This allows subscribers to test an API without subscription or monetization. This will give the scope of improvement to the API publisher.

Previous scenario:

Users were presented with an inline script that had to be manually edited for each resource. Also, the default script had to be edited to allow the resource to return a response in the developer portal.

yenlo_blog_2020-05-22_api-mocking-feature_figure-1

New Scenario (with API Manager version 3.1.0):

Prototype implementation is equipped with mock payload generation.

Users can prototype an API using the built-in JavaScript engine without writing the JavaScript implementation for each resource manually. By that, the inline script will be generated automatically for each response code and payload type (JSON and / or XML) according to the response body example values or response schemas defined in the API Definition.

If the API definition contains mock responses for more than one response code per-resource, the payloads will be presented as multiple payloads but the “mc.setPayloadJson or mc.setPayloadXML” will only contain the lowest response code value. Furthermore, inline scripts of each resource can be further edited by users to suit the required outcome better.

Once the generated mock payload scripts have been saved, the API can be deployed as a prototype and tested in the developer portal.

There are two ways to deploy an API prototype.

1. Create a new prototype API :

The new API can be created by following the steps as mentioned in the WSO2 API Manager documentation.

Once created, the API can be deployed as prototype with 2 options:

  1. Prototype Endpoint: You need to provide an endpoint to a prototype API. Once deployed as prototype this API can be tested in Developer Portal from the WSO2 API Manager.
  2. Prototype Implementation: The inline script will be automatically generated for each response code and payload type (JSON and/or XML) according to the response body example values or response schemas defined in the API definition.

2. Create a prototype API as new version from an existing API:

The new version for a given API can be created by following the steps as mentioned in WSO2 API Manager documentation.

Once created, the API can be deployed in either way as mentioned here.

The prototype API created by any of the mentioned methods can be tested by logging into Developer Portal from WSO2 API Manager.

Note: the prototype API can be tested directly without subscription as subscriptions are not allowed for prototyped API.

yenlo_blog_2020-05-22_api-mocking-feature_figure-2

Once the API prototype is tested thoroughly and the final version from the prototyped API is ready for release in the publisher, it’s time for subscription and monetization. This can be achieved by publishing the prototyped API as in Step 2.

Conclusion

As more and more businesses are moving towards APIs (the age of API is upon us), the API mocking (prototyping) has made the life of a developer much easier.

The new version WSO2 API Manager has responded to this DevOps friendly requirement with quite a simple, effective and efficient manner by leveraging the use of built-in JavaScript engine.

Care to share?
   
Picture of Vivek Tiwari
Published May 22, 2020

Vivek Tiwari

Vivek is an Integration Consultant at Yenlo and has more than nine years experience as a software consultant.

Responses

Stay up to date with the latest articles