# README
API-Mocked
API-Mocked is a stand-alone HTTP(s)+ server(s) for serving known (usually static) content.
Table of Content
Overview
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
Getting Started
API-Mocked compiles to a single binary so it can be started with
$ api-mocked
or
$ api-mocked -config basic.hcl
Using a Basic Config File
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)"
}
}
}
License
MIT License, see LICENSE
# Packages
No description provided by the author
# Functions
FileToStr takes in a name, key (used during testing) and returns a HCL function that will return the contents of a file as a string.
TextBlockToStr takes in a textblock and return the data with args filled in.
WriteError is a wrapper function that can wrap around HandlerE types that return errors, but can be used with mux handlers that accept standard library mux handlers.
# Constants
the parsed JWT token.
CtxKeyRetries is the context key that holds retry middleware that is used when error checking and retrying requests route matches.
CtxKeyServerName is the context key that holds name of the server that is supplying the request.
the secret bytes (HMAC bytes or RSA bytes).
all of the system errors.
all of the system errors.
all of the system errors.
all of the system errors.
all of the system errors.
all of the system errors.
all of the system errors.
all of the system errors.
all of the system errors.
all of the system errors.
all of the system errors.
all of the system errors.
all of the system errors.
all of the system errors.
all of the system errors.
all of the system errors.
all of the system errors.
all of the system errors.
all of the system errors.
all of the system errors.
all of the system errors.
all of the system errors.
all of the system errors.
all of the system errors.
all of the system errors.
all of the system errors.
all of the system errors.
all of the system errors.
# Variables
defaults for LetsEncrypt services.
defaults for LetsEncrypt services.
DurToStr takes a tiem duration and returns a unix timestamp of the duration from time.Now().
NowToStr returns the current time as a int64 unix time.
UnixTsToStr takes in a RFC822 formatted string and returns the unix timestamp as a int64 number
RFC822: Sat, 20 Feb 2021 00:00:00 UTC.
# Structs
Config holds all of the configuration options of the MockServer.
ConfigHTTP hold configurations for HTTP services.
Ext400Error is a type to determine a 400 Bad Request error response.
Ext401Error is a type to determine a 401 Unauthorized error response.
Ext404Error is a type to determine a 404 Not Found error response.
ExtError is an error with parameters.
RequestHTTP holds HTTP request configuration options.
ResponseHTTP holds HTTP response options.
Route holds configurations for each HTTP path.
TextBlock holds data that can be accessed by the text(name, arg...) function.
WarnError is a type that should let error continue processing and not immediately return.
# Interfaces
HandlerError is the interface used to write HTTP responses for returned errors during the handling of a mux route.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
RetryError id the interface to determine that there may be more than one response, and that alternative responses can be used during the handling of a mux route.
# Type aliases
HandlerE a type for handling HTTP requests and responses that can return errors, and have those errors handled in a proper way.
MiddlewareHTTP is the middleware type.
Plugin is the min interface needed to provide a plugin.
RunOptions allows tests and alternative entry points (other than the main CLI entrypoint) to add configuration information at runtime.
StdError is a standard error.