mock-server

command module
v0.0.0-...-a7707e3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 1, 2019 License: MIT Imports: 11 Imported by: 0

README

mock-server

mock-server is a tiny (<10 MB) Web Server that allows to emulate RESTful and WebSocket. It could be useful for a quick mocking Back-End endpoints during UI prototype implementation, integration or system tests. It could be configured by config file (YAML or JSON) and/or with API.

Table of Contents

1. Prerequisites
1.1. Install
1.2. Example files
2. Hello World
2.1. YAML
2.2. JSON
2.3. API
3. Examples
3.1. Files
3.2. CRUD
3.3. Entities
4. Config
4.1. Config YAML
4.2. Config JSON
5. API
5.1. Rest Endpoint API
5.1.1. Rest Endpoint API Description
5.1.2. Rest Endpoint API Examples
5.1.2.1. GET /_api/rest/endpoints
5.1.2.2. POST /_api/rest/endpoints
5.1.2.3. GET /_api/rest/endpoints/:id
5.1.2.4. PUT /_api/rest/endpoints/:id
5.2. Rest Global API
5.2.1. Rest Global API Description
5.2.2. Rest Global API Examples
5.2.2.1. GET /_api/rest/global
5.2.2.2. POST /_api/rest/global
5.3. Rest Entity API
5.3.1. Rest Entity API Description
5.3.2. Rest Entity API Examples
5.3.2.1. GET /_api/rest/entities
5.3.2.2. POST /_api/rest/entities
5.3.2.3. GET /_api/rest/entities/:id
5.3.2.4. PUT /_api/rest/entities/:id
5.4. Files API
5.4.1. Files API Description
5.4.2. Files API Examples
5.4.2.1. GET /_api/files
5.4.2.2. GET /_api/files/:id
6. Models
6.1. Rest Endpoint Model
6.1.1. Rest Endpoint Request Model
6.1.2. Rest Endpoint Response Model
6.2. File Model
6.3. Rest Entity Model

1. Prerequisites

1.1. Install

1.2. Example files

It is possible just to clone current repositiory with examples instead of creating files manually:

git clone https://github.com/abproject/mock-server.git
cd mock-server

and then run docker commands with examples from repository (no path changes needed).

2. Hello World

2.1. YAML

Create file examples/hello/config.yaml with content:

rest:
  endpoints:
    - request:
        method: GET
        path: hello
      response:
        status: 200
        body: Hello, World!
        headers:
          Content-Type: text/html

Run in terminal:

docker run -p 4242:8000 -v ${PWD}/examples:/examples abezpalov/mock-server -file=/examples/hello/config.yaml

Check by opening in browser http://localhost:4242/hello or making GET request, e.g., with curl:

curl -v http://localhost:4242/hello

### Response
...
< HTTP/1.1 200 OK
< Content-Type: text/html
...
Hello, World!

2.2. JSON

Create file examples/hello/config.json with content:

{
  "rest": {
    "endpoints": [
      {
        "request": {
          "method": "GET",
          "path": "hello"
        },
        "response": {
          "status": 200,
          "body": "Hello, World!",
          "headers": {
            "Content-Type": "text/html"
          }
        }
      }
    ]
  }
}

Run in terminal:

docker run -p 4242:8000 -v ${PWD}/examples:/examples abezpalov/mock-server -file=/examples/hello/config.json

Check by opening in browser http://localhost:4242/hello or making GET request, e.g., with curl:

curl -v http://localhost:4242/hello

### Response
...
< HTTP/1.1 200 OK
< Content-Type: text/html
...
Hello, World!

2.3. API

Another way to get the same Hello World configuration without config file but by using API requests only.

Run in terminal:

docker run -p 4242:8000 abezpalov/mock-server

Make POST request to URL http://localhost:4242/_api/rest/endpoints with body:

{
  "request": {
    "method": "GET",
    "path": "hello"
  },
  "response": {
    "status": 200,
    "body": "Hello, World!",
    "headers": {
      "Content-Type": "text/html"
    }
  }
}

e.g., with curl (copy all 3 code blocks below and paste in terminal):

curl -X POST http://localhost:4242/_api/rest/endpoints \
-H "Content-Type: application/json" \
-d @- << EOF
{
  "request": {
    "method": "GET",
    "path": "hello"
  },
  "response": {
    "status": 200,
    "body": "Hello, World!",
    "headers": {
      "Content-Type": "text/html"
    }
  }
}

EOF

Check by opening in browser http://localhost:4242/hello or making GET request, e.g., with curl:

curl -v http://localhost:4242/hello

### Response
...
< HTTP/1.1 200 OK
< Content-Type: text/html
...
Hello, World!

3. Examples

3.1. Files

3.2. CRUD

3.3. Entities

4. Config

4.1. Config YAML
rest:
  global:
    request:
      method: method name
      path: url path
    response:
      body: response body
      bodyFile: response body as file
      status: http status
      headers: # Map
        header-key: header-value
  endpoints: # List of Endpoint Models
    - request:
        method: method name
        path: url path
      response:
        body: response body
        bodyFile: response body as file
        status: http status
        headers: # Map
          header-key: header-value
  entities: # List of Entity Models
    - name: entity name (used as path)
      dataAll: data of all entities (Array of objects)
      dataNew: object for POST request
      id: id property in object

Details of model description:

Path Model
rest.global Endpoint Model
rest.endpoints List of Endpoint Model

4.2. Config JSON
{
	"rest": {
		"global": {
			"request": {
				"method": "method name",
				"path": "url path"
			},
			"response": {
				"body": "response body",
				"bodyFile": "response body as file",
				"status": "http status",
				"headers": {
					"header-key": "header-value"
				}
			}
		},
		"endpoints": [
			{
				"request": {
					"method": "method name",
					"path": "url path"
				},
				"response": {
					"body": "response body",
					"bodyFile": "response body as file",
					"status": "http status",
					"headers": {
						"header-key": "header-value"
					}
				}
			}
		],
		"entities": [
			{
				"name": "entity name (used as path)",
				"dataAll": "data of all entities (Array of objects)",
				"dataNew": "object for POST request",
				"id": "id property in object"
			}
		]
	}
}

Details of model description:

Path Model
rest.global Endpoint Model
rest.endpoints List of Endpoint Model

5. API

5.1. Rest Endpoint API

URL: /_api/rest/endpoints

5.1.1. Rest Endpoint API Description
Path Method Description Request Body Response Body Success Status Failed Status
/_api/rest/endpoints GET Returns the list of all endpoints configurations - List of Endpoint Model 200 -
/_api/rest/endpoints POST Creates new endpoint entity Endpoint Model Endpoint Model 201 -
/_api/rest/endpoints DELETE Deletes all endpoints configuration - - 204 -
/_api/rest/endpoints/:id GET Returns endpoint by id or error if not found - Endpoint Model 200 404
/_api/rest/endpoints/:id PUT Sets new endpoint configuration by id, returns error if not found Endpoint Model Endpoint Model 200 404
/_api/rest/endpoints/:id DELETE Deletes endpoint configuration by id, returns error if not found - - 204 404

5.1.2. Rest Endpoint API Examples

5.1.2.1. GET /_api/rest/endpoints

Response

[
  {
    "id": ":id",
    "request": {
      "method": "GET",
      "path": "hello",
      "pathReg": "",
      "headers": null
    },
    "response": {
      "status": 200,
      "body": "Hello, World!",
      "bodyFile": "",
      "headers": {
        "Content-Type": "text/html"
      }
    }
  }
]

5.1.2.2. POST /_api/rest/endpoints

Request:

{
  "request": {
    "method": "GET",
    "path": "hello"
  },
  "response": {
    "status": 200,
    "body": "Hello, World!",
    "headers": {
      "Content-Type": "text/html"
    }
  }
}

Response:

{
  "id": ":id",
  "request": {
    "method": "GET",
    "path": "hello",
    "pathReg": "",
    "headers": null
  },
  "response": {
    "status": 200,
    "body": "Hello, World!",
    "bodyFile": "",
    "headers": {
      "Content-Type": "text/html"
    }
  }
}

5.1.2.3. GET /_api/rest/endpoints/:id

Response

{
  "id": ":id",
  "request": {
    "method": "GET",
    "path": "hello",
    "pathReg": "",
    "headers": null
  },
  "response": {
    "status": 200,
    "body": "Hello, World!",
    "bodyFile": "",
    "headers": {
      "Content-Type": "text/html"
    }
  }
}

5.1.2.4. PUT /_api/rest/endpoints/:id

Request:

{
  "request": {
    "method": "GET",
    "path": "hello-new"
  },
  "response": {
    "status": 200,
    "body": "Hello, New World!",
    "headers": {
      "Content-Type": "text/html"
    }
  }
}

Response:

{
  "request": {
    "method": "GET",
    "path": "hello-new",
    "pathReg": "",
    "headers": null
  },
  "response": {
    "status": 200,
    "body": "Hello, New World!",
    "bodyFile": "",
    "headers": {
      "Content-Type": "text/html"
    }
  }
}

5.2. Rest Global API

URL: /_api/rest/global

5.2.1. Rest Global API Description
Path Method Description Request Body Response Body Success Status Failed Status
/_api/rest/global GET Returns global endpoint configurations - Endpoint Model 200 -
/_api/rest/global POST Creates new global endpoint entity Endpoint Model Endpoint Model 201 -
/_api/rest/global DELETE Deletes global endpoint configuration - - 204 -

5.2.2. Rest Global API Examples

5.2.2.1. GET /_api/rest/global

Response

{
  "id": "",
  "request": {
    "method": "",
    "path": "",
    "pathReg": "",
    "headers": {
      "Content-Type": "application/json"
    }
  },
  "response": {
    "status": 0,
    "body": "",
    "bodyFile": "",
    "headers": {
      "Content-Type": "application/json"
    }
  }
}

5.2.2.2. POST /_api/rest/global

Request:

{
  "request": {
    "headers": {
      "Content-Type": "application/json"
    }
  },
  "response": {
    "headers": {
      "Content-Type": "application/json"
    }
  }
}

Response:

{
  "id": "",
  "request": {
    "method": "",
    "path": "",
    "pathReg": "",
    "headers": {
      "Content-Type": "application/json"
    }
  },
  "response": {
    "status": 0,
    "body": "",
    "bodyFile": "",
    "headers": {
      "Content-Type": "application/json"
    }
  }
}

5.3. Rest Entity API

URL: /_api/rest/entities

5.3.1. Rest Entity API Description
Path Method Description Request Body Response Body Success Status Failed Status
/_api/rest/entities GET Returns the list of all entities - List of Entity Model 200 -
/_api/rest/entities POST Creates new entity Entity Model Entity Model 201 -
/_api/rest/entities DELETE Deletes all entities - - 204 -
/_api/rest/entities/:name GET Returns entity by name or error if not found - Entity Model 200 404
/_api/rest/entities/:name PUT Sets new entity by name, returns error if not found Entity Model Entity Model 200 404
/_api/rest/entities/:name DELETE Deletes entity by name, returns error if not found - - 204 404

5.3.2. Rest Entity API Examples

5.3.2.1. GET /_api/rest/entities

Response

[
  {
    "name": "planets",
    "dataAll": "planets.json",
    "dataNew": "planets-new.json",
    "id": "id"
  }
]

5.3.2.2. POST /_api/rest/entities

Request:

{
  "name": "planets",
  "dataAll": "planets.json",
  "dataNew": "planets-new.json",
  "id": "id"
}

Response:

{
  "name": "planets",
  "dataAll": "planets.json",
  "dataNew": "planets-new.json",
  "id": "id"
}

5.3.2.3. GET /_api/rest/entities/:id

Response

{
  "name": "planets",
  "dataAll": "planets.json",
  "dataNew": "planets-new.json",
  "id": "id"
}

5.3.2.4. PUT /_api/rest/entities/:id

Request:

{
  "name": "planets",
  "dataAll": "planets2.json",
  "dataNew": "planets-new.json",
  "id": "id"
}

Response:

{
  "name": "planets",
  "dataAll": "planets2.json",
  "dataNew": "planets-new.json",
  "id": "id"
}

5.4. Files API

URL: /_api/files

5.4.1. Files API Description
Path Method Description Request Body Response Body Success Status Failed Status
/_api/files GET Returns the list of all files configurations - List of File Model 200 -
/_api/files POST Creates new file entity
Body as form-data:
file: <file content>
Headers:
Content-Type: application/x-www-form-urlencoded
File Model 201 -
/_api/files DELETE Deletes all files configuration - - 204 -
/_api/files/:id GET Returns file by id or error if not found - File Model 200 404
/_api/files/:id PUT Sets new file configuration by id, returns error if not found
Body as form-data:
file: <file content>
Headers:
Content-Type: application/x-www-form-urlencoded
File Model 200 404
/_api/files/:id DELETE Deletes file configuration by id, returns error if not found - - 204 404

5.4.2. Files API Examples

5.4.2.1. GET /_api/files

Response

[
  {
    "id": ":id",
    "name": "api-post-request.txt",
    "length": 9
  }
]

5.4.2.2. GET /_api/files/:id

Response

{
  "id": ":id",
  "name": "api-post-request.txt",
  "length": 9
}

6. Models

6.1. Rest Endpoint Model
Field Name Type Description
id string Unique Endpoint ID. Generates by mock-server
request Rest Endpoint Request Model Request configuration model
response Rest Endpoint Response Model Response configuration model

6.1.1. Rest Endpoint Request Model
Field Name Type Description
method string Method name, e.g., GET, POST, DELETE, PUT.
Empty string means all type of requests.
Default: empty string
path string Request Endpoint path, e.g., /my-path.
Default: empty string
pathReg string Request Endpoint path as regular expression. If pathReg value is not empty then path value is ignored.
Default: empty string
headers map<string, string> Request Key-Value pairs of headers, e.g.,
"Content-Type": "application/json"
Default: null

6.1.2. Rest Endpoint Response Model
Field Name Type Description
body any Response body could be type including JSON objects.
Default: empty string
bodyFile string Response body is the content of the file. If bodyFile value is not empty then body value is ignored.
Default: empty string
status integer Response HTTP status code.
Default: 0
headers map<string, string> Response Key-Value pairs of headers, e.g.,
"Content-Type": "application/json"
Default: null

6.2. File Model
Field Name Type Description
id string Unique Endpoint ID or file name in case of config parsing
Generates by mock-server
name string Provided file name
length number Size of file in bytes

6.3. Rest Entity Model
Field Name Type Description
name string Entity name, will be used as path e.g., /some-name.
Required, Not Empty, Unique
dataAll string Name of file with database (array of entity objects)
dataNew string Name of file with response for creating new entity (POST request)
id string Property identifier name (@id) of entity

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
examples
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL