Build API mocks with Amokjs

api mocking with nodejs and amokjs

Overview

API mocking is often used within application development lifecycle.

In this tutorial, we will create 2 example mocks by using one of the API mocking frameworks – Amokjs.

One of the mocks will be lightweight Nodejs application and the second one will be API mock which can be deployed to Apigee Edge platform.

Lets 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 Example

Lets build standalone Nodejs application which serves example API responses from flat files.

First, create the package.json file to define your Nodejs project. (you should have Nodejs and npm installed). It must have the amokjs dependency listed (example of such file is here).

"dependencies": {
   "amokjs":"2.0.1"
}

Next, create the app.js file with the following content

const amokjs = require('amokjs');
amokjs.setPort('3030');

// start mock service
amokjs.start();

Above lines are importing amokjs package dependency and setting the incoming port on which app will listen to http requests.

Whats left is to create the responses directory and place the flat files with example responses. Amokjs project provides few such example files.

Install npm dependencies and run the application.

npm install
node app.js

Nodejs application should be running on the specified port, and serving example API responses based on the flat file name.

curl -v localhost:3030/json

HTTP/1.1 200 OK
X-Powered-By: Express
Content-Type: text/html; charset=utf-8
Content-Length: 180
Connection: keep-alive

{
  "array": [1,2,3],
  "boolean": true,
  "null": null,
  "number": 123
},
"string": "Hello World"
}

Amokjs has few helpers. For example, support for desired http response codes via request header x-mock-response-code and 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.

This part of tutorial will show you how to host simple API mocks using Apigee Edge and Amokjs.

For quick setup of Apigee Edge based API, we are going to use skeleton Apigee API bootstrap project. It has all the setup we need to get started.

Clone the project.

git clone git@github.com:popularowl/apigeebootstrap.git

Change the project settings and do a deployment to Apigee Edge as described in readme instructions.

You should see the API endpoint deployed and running in Apigee Edge.

Next step is to take the application we have create in the previous step of this tutorial and add it to apiproxy/resources/hosted part of Apigee API bundle.

We need to introduce one change to the app

const amokjs = require('amokjs');
const port = process.env.PORT || 3000;

amokjs.setPort(port);

amokjs.start();

Capturing process.env.PORT allows us to use the port which Nodejs app container hosted in Apigee Edge will be exposing.

Finally, we have to add app.yaml config file, which configures how Nodejs container will be run in Apigee Edge.

runtime: node
runtimeVersion: 8
env:
   - name: NODE_ENV
     value: production
   - name: LOG_LEVEL
     value: 3

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 as open source project.

As always, feel free to post any questions you have in the comments.

Did you like this post?
Subscribe to our API first monthly newsletter!

No Responses

Post Your Comment

Your email address will not be published.