Mock server in go!
Inspired by http://www.mock-server.com/
https://hub.docker.com/r/travix/gozzmock/
Travix uses gozzmock to avoid dependencies on 3rd party services at test environment. Gozzmock is a "transparent" mock and fully manageable trow API calls. Transparency means, some calls can be mocked, other calls will be send to real endpoint.
Package manager: dep
docker pull travix/gozzmock
For instance, there is a task to mock GitHub API call https://api.github.com/user By default, /user returns
{
"message": "Requires authentication",
"documentation_url": "https://developer.github.com/v3"
}
Run container with gozzmock
docker run -it -p8080:8080 travix/gozzmock
Upload a "forward" expectation to gozzmock. Expectation structure:
{
"key": "forwardExpectation",
"forward": {
"host": "api.github.com",
"scheme": "https"
}
}
Expectation should be sent to /gozzmock/add_expectation endpoint like this
curl -d '{"forward":{"host":"api.github.com","scheme":"https"},"key":"forwardExpectation"}' -X POST http://192.168.99.100:8080/gozzmock/add_expectation
NOTE 192.168.99.100 - ip of host machine
To validate that expectation works
curl http://192.168.99.100:8080/user
returns
{
"message": "Requires authentication",
"documentation_url": "https://developer.github.com/v3"
}
Add expectation with response:
{
"key": "responseExpectation",
"request": {
"method": "GET",
"path": "mocked"
},
"response": {
"body": "response from gozzmock",
"headers": [
{
"Content-Type": "text/plain; charset=utf-8"
}
],
"httpcode": 200
},
"priority": 1
}
curl -d '{"key":"responseExpectation","request":{"method":"GET","path":"mocked"},"response":{"body":"response from gozzmock","headers":[{"Content-Type":"text/plain; charset=utf-8"}],"httpcode":200},"priority":1}' -X POST http://192.168.99.100:8080/gozzmock/add_expectation
Send request with "mocked" in path:
curl http://192.168.99.100:8080/user?arg=mocked
curl http://192.168.99.100:8080/user?mocked
curl http://192.168.99.100:8080/user/mocked
For all those request response will be from expectation:
response from gozzmock
To remove expectation, send request to /gozzmock/remove_expectation with structure:
{
"key": "responseExpectation"
}
curl -d '{"key":"responseExpectation"}' -X POST http://192.168.99.100:8080/gozzmock/remove_expectation
loglevel - log level. Values: debug, info, warn, error, fatal, panic. Default: debug expectations - array of expectations is json format. Default: empty. It is used to load default/forward expectations when appication starts.
docker run -it -p8080:8080 travix/gozzmock --expectations="[{\"key\": \"k1\"},{\"key\": \"k2\"}]" --loglevel=info
NOTE only one block should be set: response or forward
Structure of "request" block
NOTE It is allowed to use regex as well as simple string. For instance, if path: ".*" - it will be parsed as regex. if string "abc" - it will be used as substring
Structure of "forward" block
Structure of "response" block
#TODO Add example with js
Content type
Image
Digest
Size
6 MB
Last updated
over 7 years ago
docker pull travix/gozzmock