Small and simple mock HTTP server configurable with JSON
1.8K
Small and simple mock HTTP server configurable with JSON.
Features:
POST and PUT endpoints also have request validation and matching
prepareErrorPath, the next request to prepared endpoint will return the prepared errorExample of prepare error request:
POST
{
"method": "POST",
"path": "/example-path",
"status": 401,
"response": {
"mock": "example-mock-response",
"info": { "error": true }
}
}
The server is configured with config.json file.
If you are mounting a config with docker or have the file anywhere else but project root folder,
you can use ENV variable CONFIG_PATH so override it.
{
"prepareErrorPath": "/prepareError",
"handlers": [
{
"path": "/example-path",
"method": "POST",
"response": {
"info": { "success": true },
"data": "post validate request success data"
},
"request": {
"validate": {
"example": "required,lowercase,min=5"
},
"match": {
"example": "mock-request-body"
}
},
"responseEcho": {
"example.mock.mock-2.mock-3": "example.mock.mock-2.mock-3"
},
"queryParams": {
"test": {
"required": false,
"value": "test-value"
}
}
}
]
}
prepareErrorPath defines a path where requests will be sent to prepare the next request to return errorhandler is an array of handler definitions where:
path is URL pathmethod is http method (POST,GET,PATCH, PUT, DELETE)response is a definition of response data you want the mock to returnrequest:
validate key-value of properties that you want validated defined by go-playground/validator tagsmatch configures the mock to check for exact match with requestresponseEcho is a key-value set of dot notation properties you want to take from request and set in response.
some.property.you.want.in.response[2] for example is present, it means take the 3rd element of array. Example some.[2].property.you.[1].want.in.responsequeryParam is a map of query params you want to check
required if false it will not fail when not sentvalue value to compare to when it is sent. if not equal it will failServe port is configured by env PORT, but defaults to :8080
docker pull tmavrin/mock-http-server
docker run --name=mock-server -e PORT=8080 -e CONFIG_PATH='/path/to/config' tmavrin/mock-http-server
Available ENVIRONMENT variables:
PORT -> defaults to 8080CONFIG_PATH -> defaults to example config from the repo (./config.json)You can clone the repo and run the server with go (go 1.22 required)
go run .
Content type
Image
Digest
sha256:515f59e7e…
Size
3 MB
Last updated
over 1 year ago
docker pull tmavrin/mock-http-server