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.
- while developing frontend
- while consuming external APIs
- to set up an API with static content
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
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
defines the authentication scheme required for each endpoint within the group. ESME supports following
authentication schemes:
{
"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 :
.
{
"auth": {
"bearer_token": {
"token": "token"
}
}
}
bearer_token
authentication checks for a header field in the form of
Authorization: Bearer <token>
.
{
"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
contains the list of all the endpoints within a group.
url
defines the route that need to be mocked.
method
defines the http method associated with an url.
status_code
defines the http status code that needs to be returned from the endpoint.
{
"response": [
{
"firstName": "jane",
"lastName": "doe",
"id": 1
},
{
"firstName": "john",
"lastName": "doe",
"id": 2
}
]
}
{
"response": {
"firstName": "jane",
"lastName": "doe",
"id": 1
}
}
{
"response": "success"
}
response
defines an array, object or string that the endpoint returns on success.
auth
defines the authentication scheme required for an endpoint. Each url
can have its own authentication scheme.
ESME supports following authentication schemes:
{
"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 :
.
{
"auth": {
"bearer_token": {
"token": "token"
}
}
}
bearer_token
authentication checks for a header field in the form of
Authorization: Bearer <token>
.
{
"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.