Simple Mock API using Golang
- golang 1.12
- Create your
response
file under directory that same with request path
For example you want to create mock for GET/healthz
Create fileresponse
underresponses/GET/healthz/
. Folderresponses
is mandatory - Write you header and body response HTTP inside your
response
file. The format is following W3 HTTP/1.1 Response.
For example to send simple 200 OK you can do thisHTTP/1.1 200 OK Content-Type: text/plain; charset=utf-8 OK
- Build binary
go build
- Run pimock
./pimock
- Access from port
8080
curl localhost:8080/healthz -v
Regex Match Path Feature
Create response
file under directory with regex name, and it'll automatically find by regex
Example :
responses/GET/users/([0-9]*)/response
Will match any GET request with paths
curl localhost:8080/users/1
curl localhost:8080/users/123/
curl localhost:8080/users/9898?params=value
Template Feature
Currently it only support to get path request under variable {{request.path.[i]}}
and {{request.url.yourquery}}
Example :
Response file like this
HTTP/1.1 200 OK
Content-Type: application/json
{
"path": "{{request.path.[1]}}"
"params": "{{request.url.key}}"
}
Will give response
curl localhost:8080/users/123?key=value
{
"path": "123"
"params": "value"
}