API-Mocked is a stand-alone HTTP(s)+ server(s) for serving known (usually static) content.
API-Mocked is a tool to provide a way to mock an api using a standalone server.
It can mock HTTP, HTTPS and Websockets, so it can provide wide coverage of how an API can look. It has the following features:
- Uses HCL for a config format, so it's easy to use and copy/paste
- Allows websocket connections (via socket.io)
- Can use CORS headers
- Can use JWTs for auth
- Can use BasicAuth for auth
- Can use values from Headers, Query Paths and JWT in responses
- Can startup multiple servers (i.e http and https) at once
- Can have HTTP2 only servers
- Can have a response on a delay
- Can have multiple responses
- Can have multiple responses on a timer
- Sends back HPKP header
API-Mocked compiles to a single binary so it can be started with
$ api-mocked
or
$ api-mocked -config basic.hcl
When you use the -config
option you can have a file that looks like below:
#basic.hcl
version = "0.0.1"
server "service" {
host = ":8888"
ssl {
# lets_encrypt = ["service.api-mocked.com"]
}
jwt "test-1" {
algo = "S256"
private_key = file("keys/rsa256.key")
}
}
notfound {
response "404" {
body = "Not Found - Check your code."
}
}
path "/path/to/api" {
_-= "Return a JWT token as a cookie with a delay of 2s"
request "get" {
delay = "2s"
response "200" {
jwt "test1-1" "cookie" "access-token" {
iss = "my issue"
sub = "my subject"
nbf = now()
iat = now()
exp = duration("1h")
hello = "world"
}
}
}
request "post" {
response "200" {
body = "Accepted"
}
}
}
path "/send/back/ws/{id}" {
request "get" {
socketio "connection" {
broadcast "ns" "event" {
data = <<_JSON_
{
"hello": "world"
}
_JSON_
}
}
response "200" {
body = "Sent"
}
}
}
path "/ping" {
_-= "A simple endpoint to check if things are working"
request "get" {
order = "random"
response "200" {
body = "OK"
}
response "200" {
body = "Works"
}
response "200" {
body = "Pong"
}
# Sometimes return a 500, to see what happens with our application
response "500" {
body = "Internal Server Error (OK)"
}
}
}
MIT License, see LICENSE