stkr89 / esme

Mock services by defining the APIs in json format

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

What is Embeddable Service Mocking Engine (ESME)?

ESME is a go library that allows you to mock a RESTful service by defining the configuration in json format. This service can then simply be consumed by any client to get the expected response.

Where can it be used?

  • while developing frontend
  • while consuming external APIs
  • to set up an API with static content

Usage

Here is a sample route-config.json file that can be processed by ESME

{
  "route_groups": [
    {
      "auth": {
        "basic": {
          "username": "user1",
          "password": "pass123"
        },
        "bearer_token": {
          "token": "abc123xyz"
        },
        "custom": {
          "custom_key": "custom_value"
        }
      },
      "endpoints": [
        {
          "url": "https://api.example.com/v1/resource",
          "method": "GET",
          "status_code": 200,
          "body": {
            "key1": "value1",
            "key2": "value2"
          },
          "response": {
            "success": true,
            "data": {
              "item1": "details1",
              "item2": "details2"
            }
          }
        },
        {
          "url": "https://api.example.com/v1/submit",
          "method": "POST",
          "status_code": 201,
          "body": {
            "submit_key": "submit_value"
          },
          "response": "Submission successful"
        }
      ]
    }
  ]
}

Start a mock server using above route-config.json file

package main

import (
	"github.com/stkr89/esme"
)

func main() {
	esme.Serve("8080", "route-config.json")
}

You can also provide multiple route configs as arguments to Serve method.

Let's break down this file to understand what each component means.

Route Groups

route_groups contains the list of route groups which need to be mocked. ESME supports adding routes to multiple files which can represent different services running simultaneously.

Auth

auth defines the authentication scheme required for each endpoint within the group. ESME supports following authentication schemes:

Basic

{
  "auth": {
    "basic": {
      "username": "username",
      "password": "password"
    }
  }
}

basic authentication checks for a header field in the form of Authorization: Basic <credentials>, where <credentials> is the Base64 encoding of username and password joined by a single colon :.

Bearer Token

{
  "auth": {
    "bearer_token": {
      "token": "token"
    }
  }
}

bearer_token authentication checks for a header field in the form of Authorization: Bearer <token>.

Custom

{
  "auth": {
    "custom": {
      "my_header_1": "value1",
      "my_header_2": "value2"
    }
  }
}

custom authentication checks for headers my_header_1 and my_header_2 with values value1 and value2 respectively.

Endpoints

endpoints contains the list of all the endpoints within a group.

URL

url defines the route that need to be mocked.

Method

method defines the http method associated with an url.

Status Code

status_code defines the http status code that needs to be returned from the endpoint.

Response

Array

{
  "response": [
    {
      "firstName": "jane",
      "lastName": "doe",
      "id": 1
    },
    {
      "firstName": "john",
      "lastName": "doe",
      "id": 2
    }
  ]
}

Object

{
  "response": {
    "firstName": "jane",
    "lastName": "doe",
    "id": 1
  }
}

String

{
  "response": "success"
}

response defines an array, object or string that the endpoint returns on success.

Auth

auth defines the authentication scheme required for an endpoint. Each url can have its own authentication scheme. ESME supports following authentication schemes:

Basic

{
  "auth": {
    "basic": {
      "username": "username",
      "password": "password"
    }
  }
}

basic authentication checks for a header field in the form of Authorization: Basic <credentials>, where <credentials> is the Base64 encoding of username and password joined by a single colon :.

Bearer Token

{
  "auth": {
    "bearer_token": {
      "token": "token"
    }
  }
}

bearer_token authentication checks for a header field in the form of Authorization: Bearer <token>.

Custom

{
  "auth": {
    "custom": {
      "my_header_1": "value1",
      "my_header_2": "value2"
    }
  }
}

custom authentication checks for headers my_header_1 and my_header_2 with values value1 and value2 respectively.

About

Mock services by defining the APIs in json format


Languages

Language:Go 100.0%