Build API mocks with Amokjs
API contract mocking is often used within application development lifecycle where backend services are not yet available or not stable.
This totorial will guide you through building REST API mocks in order to decouple and speed up application development.
We will create 2 example API mocks by using the open source API mocking framework amokjs.
Let's get started.
Amokjs is a small Nodejs library which helps to serve example API responses from flat files.
It was created for quickly deploying API response mocks to unblock development of client applications and services.
Amokjs standalone example
First, we create the
package.json file to define Nodejs project. It must have the amokjs dependency listed.
Next, create the
app.js application file with the following
const amokjs = require('amokjs');
const port = process.env.PORT || 3000;
// start mock service
Above lines are importing amokjs package dependency and setting the incoming port on which app will listen to http requests.
What's left is to create the
responses directory and place the flat files with example responses we want to serve. Amokjs project provides few such example files as reference.
Your standalone API mock is created. Install npm dependencies and run the app
You API mock should be serving responses at the port you specified erlier, based on the flat file name.
curl -v localhost:3030/json
HTTP/1.1 200 OK
Content-Type: text/html; charset=utf-8
Amokjs has few helpers. For example, support for desired http response codes via request header
x-mock-filename header support for removing file names from request paths.
You can find more sample responses in the Amokjs example project and start creating your own.
Apigee Edge and Amokjs Example
Apigee Edge is API management product used for building enterprise level API platforms.
We will host simple API mock using Apigee Edge and Amokjs.
Next step is to take the standalone application we have create in the previous step of this tutorial and add it to
apiproxy/resources/hosted part of Apigee API bundle.
Finally, we have to add
app.yaml config file, which provided settings for Nodejs container within Apigee Edge.
- name: NODE_ENV
- name: LOG_LEVEL
Running Maven deploy command again, should deploy new version of Apigee Edge API with Amokjs mock application container running.
You can find example Apigee Edge bundle with mock endpoint application on Github.