http-mockis intended to provide a mock HTTP server, the repsonses of which are read from a YAML config file.http-mocksupports 4 types of body- body
- The value of
bodyas response body content will be output to HTTP client without any change.
- The value of
- file-body
- The value of
file-bodyis a file name, the content of which will be output to the client.
- The value of
- tmpl-body
- The value of
tmpl-bodyis looked as the content of a template, so template actions will be evaluated before outputing the result.
- The value of
- redirect-body
- the value of
redirect-bodyis a URL string, which will be redirected to with a 302 response status.
- the value of
- body
-
http-mock makes use of YAML as its configuratoin.
-
There's a sample configuration file config.sample.yaml
port: 8080
# remove static-home if no static files to show
static-home:
root: ./static
alias: /static
# remove any CORS header if needed
cors:
allow-origin: "*"
allow-headers: "Content-Type, Authorization, Accept, X-Requested-With"
allow-methods: "GET, PUT, POST, DELETE, OPTIONS"
expose-headers: "X-Total-Count, X-Limit-Count, Link"
allow-credentials: "*"
default-content-type: "application/json"
actions:
-
uri: /
method: "GET, POST"
response:
status: 200
headers:
content-type: "text/plain"
cookies:
cookie-name: cookie-value
tmpl-body: |-
Welcome to use
http-mock
{{ .r.Method }}
-
uri: /ping
method: "GET, POST"
response:
body: |-
{
"code": 200,
"msg": "OK"
}
-
uri: /api/users/:id
response:
tmpl-body: '{"code":200,"msg":"OK", "id": "{{pathVar .r "id"}}"}'
-
uri: /showfile
response:
content-type: "text/plain"
file-body: a-file.txt
-
uri: /redirect
response:
redirect-body: /
-
with go1.11.x or above
-
change to any directory, run the commands
$ git clone https://github.com/rosbit/http-mock $ cd http-mock $ go build -
if everything is ok, there will be an executable
http-mockunder the directory -
for binary output under Linux/macos, click releases to download it.
-
run
http-mockwith an env variableCONF_FILEpointed to the configuration file$ CONF_FILE=./config.sample.yaml ./http-mock I am listening at :8080...
-
open another terminal window, run
curlto see the result$ curl http://localhost:8080/ Welcome to use http-mock GET $ curl http://localhost:8080/showfile this is the content of a-file $ curl http://localhost:8080/api/users/1 {"code":200,"msg":"OK", "id": "1"} $ curl -X POST http://localhost:8080/ping { "code": 200, "msg": "OK" } $ curl http://localhost:8080/static/a-file.txt # static file this is the content of a-file $ curl -D - http://localhost:8080/redirect HTTP/1.1 301 Moved Permanently Access-Control-Allow-Credentials: * Access-Control-Allow-Headers: Content-Type, Authorization, Accept, X-Requested-With Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS Access-Control-Allow-Origin: * Access-Control-Expose-Headers: X-Total-Count, X-Limit-Count, Link Content-Type: text/html; charset=utf-8 Location: / Date: Wed, 05 Jun 2019 06:43:58 GMT Content-Length: 36 <a href="/">Moved Permanently</a>.
Pull requests are welcome! Also, if you want to discuss something send a pull request with proposal and changes.