Mock the API in Kong without Coding

1) Introduction

This documentation describe the steps to setup a mock REST API in Kong API gateway without writing a single line of code. We use few of the many available plugins in Kong to setup this API flow solely using configurations.

This can be useful to simulate and test the behavior any new API service before actually implementing it though a web service. So we can let the consumers experience the actual behavior of requests and responses though the API gateway via the mock backend while your development team builds the real service. But this has few limitations as we cannot simulate very complex response payloads using the available plugins.

2) Features and API Flow

Our mock API simulation will contain the below features.

  • Mocked API response payload and headers
  • Request and response logging to a file

Below are the required Kong plugins to produce the above behaviors.

  • Request Termination Plugin: This plugin terminates incoming requests flow and generate a response with a specified status code and message. This can also be used to (temporarily) stop traffic on a Service or a Route, or even block a Consumer.
  • File Log Plugin: Append request and response data in JSON format to a log file. You can also specify streams (for example, /dev/stdout and /dev/stderr), which is especially useful when running Kong in Kubernetes.
  • Response transfer advanced: Transform the response sent by the upstream server on the fly via Kong, before returning the response to the client.

3) Configuring the Kong Plugins

Request Termination Plugin

  • Below is the sample configurations to terminate a HTTP request with a response code of 200. You can use desired HTTP response code in the parameter status_code of the below configuration.
apiVersion: configuration.konghq.com/v1
kind: KongPlugin
metadata:
name: terminate-http-with-200
namespace: common-ingress
config:
status_code: 200
message: Success. Response from Kong is 200 !
plugin: request-termination

File Log Plugin

Below I have given two configurations. First config is for the Kong file-log plugin which actually logs the request to a file created on the folder path /filelog/client1-requests.log. If we apply this plugin to an Ingress, then it will logs all the requests to this Ingress.

config:
path: /filelog/client1-requests.log
kind: KongPlugin
metadata:
name: request-log-plugin
namespace: common-ingress
plugin: file-log
apiVersion: configuration.konghq.com/v1

But if you are using Kong’s authentication features with OIDC plugin and ACL plugins, then you can apply the file-log plugin to those specific clients. Then it will only log the requests from a specific client. For a such behavior, you can apply the plugin to the kongconsumer as given below.

apiVersion: configuration.konghq.com/v1
kind: KongConsumer
metadata:
name: client1
namespace: common-ingress
annotations:
konghq.com/plugins: request-log-plugin
username: client1
credentials:
- acl-client1
  • There is a know limitation we have with the file log plugin. That is only one Kong gateway pod can write to a log file at a given time. So we need to scale down the API gateway to one pod to get an accurate log all the requests to the log file.

Response Transformer Advanced Plugin

Using this plugin, we can further modify the payload and the headers of your API response returned by the request-termination plugin. Below is just a sample configuration of this plugin and I am not using this plugin for the simulated API response given later in this article.

apiVersion: configuration.konghq.com/v1
kind: KongPlugin
metadata:
name: response-transformer
config:
add:
if_status:
- "200"
json:
- new-json-key:some_value
- another-json-key:some_value
remove:
headers:
- x-toremove
json:
- json-key-toremove:json-key-toremove
replace:
if_status:
- "200"
json:
- message:kong.com
enabled: true
run_on: first
protocols:
- http
- https
plugin: response-transformer-advanced

Now you can expose the mock API similar to how you expose a normal API in Kong. Once we apply the above plugins to our API, it will behave like a simulated API backend.

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: myapi
annotations:
konghq.com/plugins: 'terminate-http-with-200, request-log-plugin'
konghq.com/preserve-host: 'false'
konghq.com/protocols: https
konghq.com/strip-path: 'true'
spec:
rules:
- http:
paths:
- path: /api/v1/mock/myapi
backend:
serviceName: mock-svc
servicePort: 8443

4) Sample API Requests

Below is a sample API request to test the above API and the expected output. This is the most simple response you can simulate with Kong. But you can generate more complex responses by changing the above plugins as per your requirement.

curl --location --request GET 'https://mykonggateway.com/api/v1/mock/myapi'{"message":"Success. Response from Kong is 200 !"}

Thank You!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Danuka Praneeth

Danuka Praneeth

Senior Software Engineer | BSc (Hons) Engineering | CIMA | Autodidact | Knowledge-Seeker