CI

json-mocky

A customizable web server for mocking HTTP REST API responses.

json-mocky is written in Kotlin using Springboot as the underlying server.

Motivation

The motivation behind this software is to provide an easy way to mock external APIs during development phase without much configuration but also flexible enough.

Features

  • GET, POST, PUT, DELETE with NOT_FOUND, METHOD_NOT_ALLOWED, OK, CREATED and NO_CONTENT responses
  • List responses with arrays or wrapped in an envelope
  • Resource validation with BAD_REQUEST response

Limitations

  • PATCH NOT IMPLEMENTED
  • In Memory database (by design)
  • Numbers are all double (45.0 vs 45) (WIP)

⌨️ Usage

json-mocky requires a configuration file. The minimum requirements are that you define the routes you want to mock. The application will make some assumption on the resources you want and what ids to use.

ex:

{
  "routes": [
    "/users",
    "/users/{id}",
    "/users/{user_id}/orders",
    "/users/{user_id}/orders/{id}",
  ]
}

you can customize resources by providing additional configuration.

ex:

{
  "routes": [
    "/users",
    "/users/{userId}"
  ],
  "resources": [
    {
      "collectionName": "users",
      "envelope": "results",
      "idField": "userId",
      "resourceTemplate": {
        "userId": "String",
        "name": "String",
        "age": "Number"
      },
      "initialData": [
        {
          "userId": "ABCD",
          "name": "John",
          "age": 42
        },
        {
          "userId": "QWER",
          "name": "Maria",
          "age": 34
        }
      ]
    }
  ]
}

here we are configuring the users resource to use an envelope (results) when giving list responses instead of an array, setting the id field of the resource to userId (by default it is id), we also provide a resource template that allows validation on POST and PUT requests, and finally we set up some initial data.

ex. response for GET /users:

{
  "results": [
    {
      "userId": "ABCD",
      "name": "John",
      "age": 42.0
    },
    {
      "userId": "QWER",
      "name": "Maria",
      "age": 34.0
    }
  ]
}

Contributing

To get started, please fork the repo and checkout a new branch. You can then build the library with the Maven wrapper

./mnvw clean package

See more info in CONTRIBUTING.md

⚖️ License

This software is licensed under the MIT License

GitHub

View Github