Skip to content

A lightweight, user-friendly, and powerful tool for mocking API endpoints. Useful for testing API client integrations, or performance testing your application without sending traffic to your API dependencies.

License

Notifications You must be signed in to change notification settings

gegana/supermockapi

Repository files navigation

supermockapi

Build Status This image on DockerHub npm version

A lightweight, user-friendly, and powerful tool for mocking API endpoints. Useful for testing API client integrations, or performance testing your application without sending traffic to your API dependencies.

Running in Docker

Runs in port 3000 by default.

docker run -d --name supermockapi -p 80:3000 dsumiskum/supermockapi

Configure a different port.

docker run -d --name supermockapi -p 80:5000 -e "PORT:5000" dsumiskum/supermockapi

Specify initial mocked api routes specification through volume mapping a folder with a routes.json or the file itself to the container workdir (app is the workdir). The application will look for a routes.json file in the workdir when booting up.

docker run -d --name supermockapi -p 3000:3000 -v "${pwd}/folder/routes.json:/app/routes.json" dsumiskum/supermockapi

Route specifications format (routes.json):

[
  {
    "path": "/product/:sku", 
    "method": "GET",
    "status": 200, 
    "body": {
      "message": "Server received your request for sku {sku}" 
    },
    "responseDelay": 5000 
  },
  {
    "path": "/product/:sku", 
    "method": "POST",
    "status": 500
  }
]

Features

supermockapi is meant to be lightweight, easy to deploy, and works for your Continous Integration needs. A big problem with performance testing services is isolating the external dependencies to get more predictable metrics, which would be useful if you are benchmarking your metrics overtime.

With supermockapi, you can deploy it with your application as a stack, with specified routes.json file. And/or you can use the super friendly UI to manually add mock api endpoints. The UI comes with a console log stream, so that you can monitor incoming requests all in one place.

Create mocked API endpoints using Express JS pattern matching

You can do this via the UI or by configuring a routes.json file. Json file example where you take a parameter sku in the base path:

{
  "path": "/product/:sku"
}

Specify default response code

You can do this via the UI or by configuring a routes.json file. Json file example for returning a 200:

{
  "path": "/product/:sku",
  "status": 200
}

Specify default response body in json

You can do this via the UI or by configuring a routes.json file. Json file example for returning a simple message:

{
  "path": "/product/:sku",
  "status": 200,
  "body": {
    "message": "Hello world!"
  }
}

You can include values from the tokens bag in your response body. The syntax for this is {token}.

{
  "path": "/product/:sku",
  "status": 200,
  "body": {
    "sku": "{sku}"
  }
}

What is included in tokens:

  1. Any parameters in the base URL
  2. Any parameters in the querystring
  3. The request body {body}
  4. The number of calls made to the base URL {calls}
  5. A random number between 1 and 100 {random}
  6. The current timestamp {timestamp}

Simulate response delay

You can do this via the UI or by configuring a routes.json file. Json file example for delaying response by 5 seconds:

{
  "responseDelay": 5000
}

Specify conditional behaviors

You can do this via the UI or by configuring a routes.json file. Json file example of a weighted response scenario using conditional behaviors:

{
    "path": "/random",
    "method": "GET",
    "status": 200,
    "conditionalBehaviors": [{
        "condition": "{random} <= 30",
        "status": 500,
        "body": {
          "message": "[Condition Triggered] Random = {random}"
        }
      },
      {
        "condition": "{random} <= 60",
        "status": 404,
        "body": {
          "message": "[Condition Triggered] Random = {random}"
        }
      },
      {
        "condition": "{random} <= 90",
        "status": 200,
        "body": {
          "message": "[Condition Triggered] Random = {random}"
        }
      },
      {
        "condition": "{random} <= 100",
        "status": 206,
        "body": {
          "message": "[Condition Triggered] Random = {random}"
        }
      }
    ],
    "body": {
      "message": "Number of calls = {calls}"
    }
  }

How it works

Built using express js, the mock api endpoints have 3 priority levels:

  1. Routes hard coded in the application
  2. Routes you inject via routes.json
  3. Routes added dynamically via the API (/mockapi/route) that the UI uses

All routes are stored in memory. When you create routes.json file, make sure to include the required fields: path, method, and status.

Contributing

If you want to contribute, the application is fairly standard node js app albeit it is using 7.0 features for async/await operators. So you will need:

  1. Node 7.10.0
  2. IDE like Visual Studio Code
  3. Docker engine

About

A lightweight, user-friendly, and powerful tool for mocking API endpoints. Useful for testing API client integrations, or performance testing your application without sending traffic to your API dependencies.

Resources

License

Stars

Watchers

Forks

Packages

No packages published