From b44fc65a87848b2c297fa7865623bfa3cafbf8ff Mon Sep 17 00:00:00 2001 From: joanlopez <5459617+joanlopez@users.noreply.github.com> Date: Fri, 24 Nov 2023 21:49:37 +0000 Subject: [PATCH] =?UTF-8?q?Deploying=20to=20gh-pages=20from=20@=20friendso?= =?UTF-8?q?fgo/killgrave@9282f7becabc37fd37c1459919a2ad732adf3bd3=20?= =?UTF-8?q?=F0=9F=9A=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 404.html | 34 +++ assets/404.html-6Yl8cQE3.js | 1 + assets/404.html-SFz3f4i6.js | 1 + assets/advanced.html-QltpobXN.js | 1 + assets/advanced.html-avBEJl4A.js | 1 + assets/app-GKjJbFgT.js | 16 ++ assets/concepts.html-bXMIjlTf.js | 1 + assets/concepts.html-g1xN_7-T.js | 1 + assets/getting-started.html-9uYXJoA8.js | 3 + assets/getting-started.html-rmTUm9IU.js | 1 + assets/ht-delays.html-5ziKQeRG.js | 1 + assets/ht-delays.html-LuxVH0ER.js | 20 ++ assets/ht-dynamic.html-1SuRYzk0.js | 1 + assets/ht-dynamic.html-JSz71AUZ.js | 32 +++ assets/ht-json.html-91jTj4Ja.js | 71 ++++++ assets/ht-json.html-INXtboAi.js | 1 + assets/ht-regex.html-NdAzqI34.js | 52 ++++ assets/ht-regex.html-btTFIkhL.js | 1 + assets/index.html-9fYTIma7.js | 1 + assets/index.html-CLGSAmWB.js | 236 +++++++++++++++++ assets/index.html-IYIbccC0.js | 1 + assets/index.html-P364yGk2.js | 25 ++ assets/index.html-_BzUMnyZ.js | 1 + assets/index.html-jIgMvDZ4.js | 1 + assets/index.html-mIPCr_qu.js | 1 + assets/index.html-tQcrVKVd.js | 1 + assets/installation.html-13nM83rt.js | 1 + assets/installation.html-_D7ND1iN.js | 4 + assets/style-ysdZ3Cf_.css | 1 + assets/your-first-imposter.html-C8xizH02.js | 16 ++ assets/your-first-imposter.html-t5dvpvCj.js | 1 + cli/index.html | 58 +++++ config/index.html | 269 ++++++++++++++++++++ guide/advanced.html | 34 +++ guide/concepts.html | 34 +++ guide/getting-started.html | 36 +++ guide/ht-delays.html | 53 ++++ guide/ht-dynamic.html | 65 +++++ guide/ht-json.html | 104 ++++++++ guide/ht-regex.html | 85 +++++++ guide/index.html | 34 +++ guide/installation.html | 37 +++ guide/your-first-imposter.html | 49 ++++ img/killgrave.png | Bin 0 -> 45764 bytes index.html | 34 +++ 45 files changed, 1421 insertions(+) create mode 100644 404.html create mode 100644 assets/404.html-6Yl8cQE3.js create mode 100644 assets/404.html-SFz3f4i6.js create mode 100644 assets/advanced.html-QltpobXN.js create mode 100644 assets/advanced.html-avBEJl4A.js create mode 100644 assets/app-GKjJbFgT.js create mode 100644 assets/concepts.html-bXMIjlTf.js create mode 100644 assets/concepts.html-g1xN_7-T.js create mode 100644 assets/getting-started.html-9uYXJoA8.js create mode 100644 assets/getting-started.html-rmTUm9IU.js create mode 100644 assets/ht-delays.html-5ziKQeRG.js create mode 100644 assets/ht-delays.html-LuxVH0ER.js create mode 100644 assets/ht-dynamic.html-1SuRYzk0.js create mode 100644 assets/ht-dynamic.html-JSz71AUZ.js create mode 100644 assets/ht-json.html-91jTj4Ja.js create mode 100644 assets/ht-json.html-INXtboAi.js create mode 100644 assets/ht-regex.html-NdAzqI34.js create mode 100644 assets/ht-regex.html-btTFIkhL.js create mode 100644 assets/index.html-9fYTIma7.js create mode 100644 assets/index.html-CLGSAmWB.js create mode 100644 assets/index.html-IYIbccC0.js create mode 100644 assets/index.html-P364yGk2.js create mode 100644 assets/index.html-_BzUMnyZ.js create mode 100644 assets/index.html-jIgMvDZ4.js create mode 100644 assets/index.html-mIPCr_qu.js create mode 100644 assets/index.html-tQcrVKVd.js create mode 100644 assets/installation.html-13nM83rt.js create mode 100644 assets/installation.html-_D7ND1iN.js create mode 100644 assets/style-ysdZ3Cf_.css create mode 100644 assets/your-first-imposter.html-C8xizH02.js create mode 100644 assets/your-first-imposter.html-t5dvpvCj.js create mode 100644 cli/index.html create mode 100644 config/index.html create mode 100644 guide/advanced.html create mode 100644 guide/concepts.html create mode 100644 guide/getting-started.html create mode 100644 guide/ht-delays.html create mode 100644 guide/ht-dynamic.html create mode 100644 guide/ht-json.html create mode 100644 guide/ht-regex.html create mode 100644 guide/index.html create mode 100644 guide/installation.html create mode 100644 guide/your-first-imposter.html create mode 100644 img/killgrave.png create mode 100644 index.html diff --git a/404.html b/404.html new file mode 100644 index 0000000..debeb29 --- /dev/null +++ b/404.html @@ -0,0 +1,34 @@ + + +
+ + + + + +Access-Control-Request-Method
Access-Control-Request-Headers
Access-Control-Expose-Headers
Access-Control-Allow-Origin
Access-Control-Allow-Credentials
You can use Killgrave in proxy mode using the flags proxy-mode
and proxy-url
or their equivalent fields in the configuration file. The following three proxy modes are available:
none
: (Default) Killgrave won't behave as a proxy and will only use the configured imposters.missing
: Killgrave will try to match the request with a configured imposter. Otherwise, it will forward the request to the configured proxy.all
: Killgrave will forward all the incoming requests to the configured proxy.The proxy-url
must be the root path of the proxy server. For instance, if we have an API running on http://example.com/things
, the configured proxy-url
should be http://example.com
.
Killgrave has a secure mode that lets you expose your mock servers over secure connections by using HTTP over TLS/SSL (HTTPS).
You can use the secure
setting to enable the secure mode. Disabled by default.
If enabled, the mock server will use TLS options with a dummy certificate, to make it work with the HTTPS
protocol.
Killgrave has a file watcher that lets you hot reload your mock servers on every imposters change.
You can use the watcher
setting to enable the secure mode. Disabled by default.
If enabled, the file watcher will be watching changes on the imposters directory so the mock server will be restarted on every imposters change.
Killgrave has an interactive mode that lets you debug the behavior of your mock server.
You can use the debugger
settings to enable the interactive mode. Disabled by default.
You can find further details here.
',16);function b(_,y){const c=a("ExternalLinkIcon"),t=a("RouterLink");return d(),l("div",null,[u,p,g,o("p",null,[e("If you want to use Killgrave from a frontend application, you should consider configuring "),o("a",f,[e("cross-origin resource sharing (CORS)"),r(c)]),e(".")]),o("p",null,[e("In the "),r(t,{to:"/config/#cors"},{default:s(()=>[e("CORS")]),_:1}),e(" section of the file you can find the following options:")]),m,o("p",null,[e("You can find further details "),r(t,{to:"/config/#cors"},{default:s(()=>[e("here")]),_:1}),e(".")]),v])}const w=i(h,[["render",b],["__file","advanced.html.vue"]]);export{w as default}; diff --git a/assets/app-GKjJbFgT.js b/assets/app-GKjJbFgT.js new file mode 100644 index 0000000..2990707 --- /dev/null +++ b/assets/app-GKjJbFgT.js @@ -0,0 +1,16 @@ +function vo(e,t){const n=Object.create(null),r=e.split(",");for(let o=0;oImposters are the most important concept in the Killgrave's world.
They conform the rules that determine how the mock server should respond to a request.
You can identify a Killgrave imposter file by its extension: .imp.json
, .imp.yml
or .imp.yaml
.
The imposter object can be divided in two parts:
This part defines how Killgrave should determine whether an incoming request matches the imposter or not.
The request object has the following properties:
method
(mandatory): The HTTP method of the incoming request.endpoint
(mandatory): Path of the endpoint relative to the base. Supports regular expressions.schemaFile
: A JSON schema to validate the incoming request against.params
: Restrict incoming requests by query parameters. Supports regular expressions.headers
: Restrict incoming requests by HTTP header.This part defines how Killgrave should, in case of match, respond to the incoming request.
The response object has the following properties:
',10),m=t("status
(mandatory): Integer defining the HTTP status code to return.body
or bodyFile
: The response body. Either a literal string (body
) or a path to a file (bodyFile
). bodyFile
is especially useful in the case of large outputs. This property is optional: if not response body should be returned it should be removed or left empty.headers
: HTTP headers to return in the response.To start Killgrave, you can simply do it by running:
$ killgrave
+
While you are welcome to provide your own configuration, Killgrave will default to the following configuration:
- imposters path:
imposters
- host:
localhost
- port:
3000
- CORS:
[]
- proxy:
none
- watcher:
false
- debugger:
- enabled:
false
- address:
localhost:3030
Killgrave is almost fully configurable through the command line, except for CORS, which can only be configured using the config file.
You can find the list with all the available flags at CLI section or by running:
$ killgrave -h
+
In case you are looking for a predictable and reproducible configuration, you can use the -config
command-line flag to specify the location of a configuration file, which can be written either in JSON or YAML.
You can find the list with all the available settings at Config Reference section.
As you can see, you can configure all the options in a very easy way.
`,14),r=[n];function l(s,c){return o(),i("div",null,r)}const h=e(a,[["render",l],["__file","getting-started.html.vue"]]);export{h as default}; diff --git a/assets/getting-started.html-rmTUm9IU.js b/assets/getting-started.html-rmTUm9IU.js new file mode 100644 index 0000000..11aa0f1 --- /dev/null +++ b/assets/getting-started.html-rmTUm9IU.js @@ -0,0 +1 @@ +const e=JSON.parse('{"key":"v-fb0f0066","path":"/guide/getting-started.html","title":"Getting started","lang":"en-US","frontmatter":{"prev":"./installation","next":"./your-first-imposter"},"headers":[{"level":2,"title":"Command-line interface","slug":"command-line-interface","link":"#command-line-interface","children":[]},{"level":2,"title":"Configuration file","slug":"configuration-file","link":"#configuration-file","children":[]}],"git":{"updatedTime":1700862554000,"contributors":[{"name":"Joan López de la Franca Beltran","email":"joanjan14@gmail.com","commits":1}]},"filePathRelative":"guide/getting-started.md"}');export{e as data}; diff --git a/assets/ht-delays.html-5ziKQeRG.js b/assets/ht-delays.html-5ziKQeRG.js new file mode 100644 index 0000000..8285a2c --- /dev/null +++ b/assets/ht-delays.html-5ziKQeRG.js @@ -0,0 +1 @@ +const e=JSON.parse('{"key":"v-d5fc1798","path":"/guide/ht-delays.html","title":"Use delayed responses","lang":"en-US","frontmatter":{"prev":"./ht-json","next":"./ht-dynamic"},"headers":[],"git":{"updatedTime":1700862554000,"contributors":[{"name":"Joan López de la Franca Beltran","email":"joanjan14@gmail.com","commits":1}]},"filePathRelative":"guide/ht-delays.md"}');export{e as data}; diff --git a/assets/ht-delays.html-LuxVH0ER.js b/assets/ht-delays.html-LuxVH0ER.js new file mode 100644 index 0000000..855f199 --- /dev/null +++ b/assets/ht-delays.html-LuxVH0ER.js @@ -0,0 +1,20 @@ +import{_ as a,r as t,o,c as p,a as s,b as n,d as r,e as l}from"./app-GKjJbFgT.js";const c={},i=s("h1",{id:"use-delayed-responses",tabindex:"-1"},[s("a",{class:"header-anchor",href:"#use-delayed-responses","aria-hidden":"true"},"#"),n(" Use delayed responses")],-1),u=s("p",null,[n("If you want to simulate a problem with the network, or create a more realistic response, you can use the "),s("code",null,"delay"),n(" property.")],-1),d=s("code",null,"delay",-1),k={href:"https://golang.org/pkg/time/#ParseDuration",target:"_blank",rel:"noopener noreferrer"},v=l(`Historically, the options
imposters_path
,port
andhost
were mandatory when using a configuration file. However, since the last version (v0.4.1
), they are no longer needed, so you can simply override those options if you actually want to. Furthermore, in previous versions (prior tov0.4.1
), theimposters_path
option was used to refer to the path to the working directory where Killgrave was executed from, but since the last version (v0.4.1
) this is relative to the location of the config file.
Alternatively, the delay
property can take a range of two durations, separated by :
. In this case, the server will respond with a random delay within this range.
With the example below, the response would be delayed between 1 and 5 seconds:
[
+ {
+ "request": {
+ "method": "POST",
+ "endpoint": "/gophers",
+ "schemaFile": "schemas/create_gopher_request.json",
+ "headers": {
+ "Content-Type": "application/json"
+ }
+ },
+ "response": {
+ "status": 201,
+ "headers": {
+ "Content-Type": "application/json"
+ },
+ "delay": "1s:5s"
+ }
+ }
+]
+
Killgrave allows dynamic responses. Using this feature, Killgrave can return different responses on the same endpoint.
To do this, all imposters need to be sorted from most restrictive to least. Killgrave tries to match the request with each of the imposters in sequence, stopping at the first imposter that matches the request.
In the following example, there are defined multiple imposters for the POST /gophers
endpoint:
[
+ {
+ "request": {
+ "method": "POST",
+ "endpoint": "/gophers",
+ "schemaFile": "schemas/create_gopher_request.json",
+ "headers": {
+ "Content-Type": "application/json"
+ }
+ },
+ "response": {
+ "status": 201,
+ "headers": {
+ "Content-Type": "application/json"
+ }
+ }
+ },
+ {
+ "request": {
+ "method": "POST",
+ "endpoint": "/gophers"
+ },
+ "response": {
+ "status": 400,
+ "headers": {
+ "Content-Type": "application/json"
+ },
+ "body": "{\\"errors\\":\\"bad request\\"}"
+ }
+ }
+]
+
Now,
schemaFile
.To do that you will need to define our json schema
first:
e.g. imposters/schemas/create_gopher_request.json
{
+ "type": "object",
+ "properties": {
+ "data": {
+ "type": "object",
+ "properties": {
+ "type": {
+ "type": "string",
+ "enum": [
+ "gophers"
+ ]
+ },
+ "attributes": {
+ "type": "object",
+ "properties": {
+ "name": {
+ "type": "string"
+ },
+ "color": {
+ "type": "string"
+ },
+ "age": {
+ "type": "integer"
+ }
+ },
+ "required": [
+ "name",
+ "color",
+ "age"
+ ]
+ }
+ },
+ "required": [
+ "type",
+ "attributes"
+ ]
+ }
+ },
+ "required": [
+ "data"
+ ]
+}
+
With this json schema
, we expect a request
like this:
{
+ "data": {
+ "type": "gophers",
+ "attributes": {
+ "name": "Zebediah",
+ "color": "Purples",
+ "age": 55
+ }
+ }
+}
+
Then the imposter could be configured as follows:
[
+ {
+ "request": {
+ "method": "POST",
+ "endpoint": "/gophers",
+ "schemaFile": "schemas/create_gopher_request.json",
+ "headers": {
+ "Content-Type": "application/json"
+ }
+ },
+ "response": {
+ "status": 201,
+ "headers": {
+ "Content-Type": "application/json"
+ }
+ }
+ }
+]
+
The path where the schema is located is relative to where the imposters are.
`,8);function k(v,q){const a=e("ExternalLinkIcon");return o(),p("div",null,[l,s("p",null,[n("Sometimes you may need to validate requests more thoroughly. In such case, you can create an imposter that only matches with a valid "),s("a",r,[n("json schema"),c(a)]),n(".")]),d])}const b=t(u,[["render",k],["__file","ht-json.html.vue"]]);export{b as default}; diff --git a/assets/ht-json.html-INXtboAi.js b/assets/ht-json.html-INXtboAi.js new file mode 100644 index 0000000..6ef6e9f --- /dev/null +++ b/assets/ht-json.html-INXtboAi.js @@ -0,0 +1 @@ +const e=JSON.parse('{"key":"v-5cac6c5c","path":"/guide/ht-json.html","title":"Use JSON Schema","lang":"en-US","frontmatter":{"prev":"./ht-regex","next":"./ht-delays"},"headers":[],"git":{"updatedTime":1700862554000,"contributors":[{"name":"Joan López de la Franca Beltran","email":"joanjan14@gmail.com","commits":1}]},"filePathRelative":"guide/ht-json.md"}');export{e as data}; diff --git a/assets/ht-regex.html-NdAzqI34.js b/assets/ht-regex.html-NdAzqI34.js new file mode 100644 index 0000000..d86d0a2 --- /dev/null +++ b/assets/ht-regex.html-NdAzqI34.js @@ -0,0 +1,52 @@ +import{_ as o,r as p,o as u,c as r,a as s,b as n,d as e,e as t}from"./app-GKjJbFgT.js";const i={},l=s("h1",{id:"use-regular-expressions-regex",tabindex:"-1"},[s("a",{class:"header-anchor",href:"#use-regular-expressions-regex","aria-hidden":"true"},"#"),n(" Use regular expressions (regex)")],-1),c=s("h2",{id:"regex-in-the-endpoint",tabindex:"-1"},[s("a",{class:"header-anchor",href:"#regex-in-the-endpoint","aria-hidden":"true"},"#"),n(" Regex in the endpoint")],-1),d={href:"https://github.com/gorilla/mux",target:"_blank",rel:"noopener noreferrer"},q={href:"https://cran.r-project.org/web/packages/ulid/vignettes/intro-to-ulid.html",target:"_blank",rel:"noopener noreferrer"},k=t(`[
+ {
+ "request": {
+ "method": "GET",
+ "endpoint": "/gophers/{_id:[\\\\w]{26}}"
+ },
+ "response": {
+ "status": 200,
+ "headers": {
+ "Content-Type": "application/json"
+ },
+ "body": "{\\"data\\":{\\"type\\":\\"gophers\\",\\"id\\":\\"01D8EMQ185CA8PRGE20DKZTGSR\\",\\"attributes\\":{\\"name\\":\\"Zebediah\\",\\"color\\":\\"Purples\\",\\"age\\":55}}}"
+ }
+ }
+]
+
In this example, we have configured an imposter that only matches if we receive an apiKey as query parameter:
[
+ {
+ "request": {
+ "method": "GET",
+ "endpoint": "/gophers/{_id:[\\\\w]{26}}",
+ "params": {
+ "apiKey": "{_apiKey:[\\\\w]+}"
+ }
+ },
+ "response": {
+ "status": 200,
+ "headers": {
+ "Content-Type": "application/json"
+ },
+ "body": "{\\"data\\":{\\"type\\":\\"gophers\\",\\"id\\":\\"01D8EMQ185CA8PRGE20DKZTGSR\\",\\"attributes\\":{\\"name\\":\\"Zebediah\\",\\"color\\":\\"Purples\\",\\"age\\":55}}}"
+ }
+ }
+]
+
In this case we will not need the gorilla mux nomenclature
to write our regex.
In the next example, we have configured an imposter that uses regex to match an Authorization
header.
[
+ {
+ "request": {
+ "method": "GET",
+ "endpoint": "/gophers/{id:[\\\\w]{26}}",
+ "headers": {
+ "Authorization": "\\\\w+"
+ }
+ },
+ "response": {
+ "status": 200,
+ "headers": {
+ "Content-Type": "application/json"
+ },
+ "body": "{\\"data\\":{\\"type\\":\\"gophers\\",\\"id\\":\\"01D8EMQ185CA8PRGE20DKZTGSR\\",\\"attributes\\":{\\"name\\":\\"Zebediah\\",\\"color\\":\\"Purples\\",\\"age\\":55}}}"
+ }
+ }
+]
+
Killgrave can be used without explicitly providing any configuration. However, you can tune up some of their settings like the host and port where the mock server is listening to, among others, by providing some configuration settings.
To provide those settings, you can either use the available CLI flags or use the -config
flag to provide the path to a settings file. In such case, you can either use JSON or YAML.
Minimal configuration attributes needed to get your mock up and running.
string
localhost
{
+ "host": "localhost"
+}
+
host: "localhost"
+
number
3000
{
+ "port": 3000
+}
+
port: 3000
+
string
imposters
Specify the directory the imposter files (either .imp.json
, .imp.yml
or .imp.yaml
) will be loaded from. On a regular set up, this directory will contain multiple of those imposter files and the directories with schemas
and responses
.
{
+ "imposters_path": "imposters"
+}
+
imposters_path: imposters
+
Set up a proxy to redirect the incoming requests as you want.
string
none
Specify the proxy mode for the Killgrave server. The default value is none
which means no proxy. Use missing
to redirect only those incoming requests that aren't defined within the imposters. Use all
to redirect all incoming requests.
{
+ "proxy": {
+ "mode": "missing"
+ }
+}
+
proxy:
+ mode: missing
+
string
Specify the url for the Killgrave's proxy. The incoming requests will be redirected to this url based on the proxy mode.
{
+ "proxy": {
+ "url": "https://example.com"
+ }
+}
+
proxy:
+ url: https://example.com
+
string array
["GET", "HEAD", "POST", "PUT", "OPTIONS", "DELETE", "PATCH", "TRACE", "CONNECT"]
Represents the Access-Control-Request-Method
header.
{
+ "cors": {
+ "methods": ["GET"]
+ }
+}
+
cors:
+ methods: ["GET"]
+
string array
["X-Requested-With", "Content-Type", "Authorization"]
Represents the Access-Control-Request-Headers
header.
{
+ "cors": {
+ "headers": ["Content-Type"]
+ }
+}
+
cors:
+ headers: ["Content-Type"]
+
string array
["Cache-Control", "Content-Language", "Content-Type", "Expires", "Last-Modified", "Pragma"]
Represents the Access-Control-Expose-Headers
header.
{
+ "cors": {
+ "exposed_headers": ["Cache-Control"]
+ }
+}
+
cors:
+ exposed_headers: ["Cache-Control"]
+
string array
[]
Represents the Access-Control-Allow-Origin
header.
{
+ "cors": {
+ "origins": ["*"]
+ }
+}
+
cors:
+ origins: ["*"]
+
boolean
false
Enables or disables the Access-Control-Allow-Credentials
header.
{
+ "cors": {
+ "allow_credentials": true
+ }
+}
+
cors:
+ allow_credentials: true
+
Sometimes you may want to simulate a production-like behavior with your mock servers, for instance to verify that your frontend application manages correctly HTTPS/TLS connections.
boolean
false
Expose the mock server through HTTP over TLS(SSL).
{
+ "secure": true
+}
+
secure: true
+
When building the imposters to mock your server, you way want to reduce the feedback loop on configuration changes.
boolean
false
Enable the file watcher to hot reload the mock server on every imposters change.
{
+ "watcher": true
+}
+
watcher: true
+
See below a full example of the configuration file:
{
+ "port": 3000,
+ "host": "localhost",
+ "imposters_path": "imposters",
+ "proxy": {
+ "url": "https://example.com",
+ "mode": "missing"
+ },
+ "cors": {
+ "methods": [
+ "GET"
+ ],
+ "headers": [
+ "Content-Type"
+ ],
+ "exposed_headers": [
+ "Cache-Control"
+ ],
+ "origins": [
+ "*"
+ ],
+ "allow_credentials": true
+ },
+ "secure": true,
+ "watcher": true
+}
+
port: 3000
+host: "localhost"
+imposters_path: "imposters"
+proxy:
+ url: https://example.com
+ mode: missing
+cors:
+ methods: ["GET"]
+ headers: ["Content-Type"]
+ exposed_headers: ["Cache-Control"]
+ origins: ["*"]
+ allow_credentials: true
+secure: true
+watcher: true
+
Imposters are the first-class citizens in Killgrave, they define how the mock server will respond to the incoming requests. You can use either JSON or YAML to define them.
The request
configuration is used to evaluate whether an incoming request matches the imposter or not. If there's a match, the mock server will respond with the imposter's response.
string
Specify the expected HTTP method.
{
+ "request": {
+ "method": "POST"
+ }
+}
+
request:
+ method: "POST"
+
string
{
+ "request": {
+ "endpoint": "/gophers"
+ }
+}
+
request:
+ endpoint: /gophers
+
string
Specify the path to the file that contains the expected JSON schema.
{
+ "request": {
+ "schemaFile": "schemas/create_gopher_request.json"
+ }
+}
+
request:
+ schemaFile: "schemas/create_gopher_request.json"
+
string map
{
+ "request": {
+ "params": {
+ "id": "01EKPT"
+ }
+ }
+}
+
request:
+ params:
+ id: "01EKPT"
+
string map
Specify the expected HTTP headers.
{
+ "request": {
+ "headers": {
+ "Content-Type": "application/json",
+ "Return-Error": "error"
+ }
+ }
+}
+
request:
+ headers:
+ Content-Type: "application/json"
+ Return-Error: "error"
+
The response
configuration is used to build the response in case of match with the incoming request.
number
200
Specify the response's HTTP status code.
{
+ "response": {
+ "status": 401
+ }
+}
+
response:
+ status: 401
+
string
Specify the raw contents (as string) to be returned as the response body. You could use a stringified JSON, for instance.
Although, in such cases where the desired response body is a complex payload (e.g. a JSON object), we recommend to use a separate file in combination with the bodyFile
request attribute.
{
+ "response": {
+ "body": "Simple response body"
+ }
+}
+
response:
+ body: "Simple response body"
+
string
Specify the path to the file that contains the response body's contents.
{
+ "response": {
+ "bodyFile": "/path/to/file"
+ }
+}
+
response:
+ bodyFile: "/path/to/file"
+
string map
Specify the response's HTTP headers.
{
+ "response": {
+ "headers": {
+ "Content-Type": "application/json",
+ "Return-Error": "error"
+ }
+ }
+}
+
response:
+ headers:
+ Content-Type: "application/json"
+ Return-Error: "error"
+
string
Specify the response delay. It is really helpful to reproduce real-world examples and/or to simulate situations with peaks of load where the responses are considerably slow.
You can use either a fixed time (single value) or an interval (two values joined by :
).
{
+ "response": {
+ "delay": "2s:5s"
+ }
+}
+
response:
+ delay: "2s:5s"
+
See below some full examples:
{
+ "request": {
+ "method": "POST",
+ "endpoint": "/gophers",
+ "schemaFile": "schemas/create_gopher_request.json",
+ "params": {
+ "id": "01EKPT"
+ },
+ "headers": {
+ "Content-Type": "application/json",
+ "Return-Error": "error"
+ }
+ }
+}
+
request:
+ method: "POST"
+ endpoint: "/gophers"
+ schemaFile": "schemas/create_gopher_request.json"
+ params":
+ id: "01EKPT"
+ headers:
+ Content-Type: "application/json"
+ Return-Error: "error"
+
{
+ "response": {
+ "status": 401,
+ "body": "Simple response body",
+ "bodyFile": "/path/to/file",
+ "headers": {
+ "Content-Type": "application/json",
+ "Return-Error": "error"
+ },
+ "delay": "2s:5s"
+ }
+}
+
response:
+ status: 401
+ body: "Simple response body"
+ bodyFile: "/path/to/file"
+ headers:
+ Content-Type: "application/json"
+ Return-Error: "error"
+ delay: "2s:5s"
+
Note from the examples above that you should only provide one of body
or bodyFile
, but not both. Here both are defined just for the sake of showing a full example.
Killgrave is a tool that provides a simple way to create a powerful simulator for HTTP-based APIs.
The Killgrave's philosophy is to provide an easy way to configure your mock server, ensuring that you don't need to learn another tooling language. For that reason we use JSON and YAML to generate all necessary configurations.
Some Killgrave features are:
However, you can tune up some of their settings like the host and port where the mock server is listening to, among others, by providing some configuration settings.
To provide those settings, you can either use the available CLI flags or use the -config
flag to provide the path to a settings file. In such case, you can either use a JSON or YAML configuration file.
See below the list of available flags:
$ killgrave -h
+
+ -config string
+ path to the configuration file
+ -debugger
+ run your server with the debugger
+ -debugger-addr string
+ debugger address (default "localhost:3030")
+ -host string
+ run your server on a different host (default "localhost")
+ -imposters string
+ directory where imposters are read from (default "imposters")
+ -port int
+ run your server on a different port (default 3000)
+ -proxy-mode string
+ proxy mode (choose between 'all', 'missing' or 'none') (default "none")
+ -proxy-url string
+ proxy url, use it in combination with proxy-mode
+ -secure
+ run your server using TLS (https)
+ -version
+ show the version of the application
+ -watcher
+ enable the file watcher, which reloads the server on every file change
+
You can install Killgrave in different ways, but all of them are very simple:
One of them is of course using go install
, Killgrave is a Go project and therefore can be compiled using the go toolchain:
$ go install github.com/friendsofgo/killgrave/cmd/killgrave@{version}
+
Note that version
must be replaced by the version that you want to install. If left unspecified, the main
branch will be installed.
$ brew install friendsofgo/tap/killgrave
+
⚠️ If you are installing via Homebrew, you always get the latest Killgrave version, we hope to fix this soon!
$ docker run -it --rm -p 3000:3000 -v $PWD/:/home -w /home friendsofgo/killgrave -host 0.0.0.0
+
-p 3000:3000
is used to forward the local port 3000
(Killgrave's default port) to container's port 3000
, otherwise Killgrave won't be reachable from the host.
-host 0.0.0.0
is used to change the Killgrave's default host (localhost
) to allow Killgrave to listen to and respond to incoming requests from outside the container, otherwise Killgrave won't be reachable from the host.
At least one imposter must be configured in order to use Killgrave.
Every file with any of the valid extensions (.imp.json
, .imp.yml
or .imp.yaml
), present in the "imposters" folder (default "./imposters"
) will be interpreted as an imposters file.
We use a rule-based system to match requests to imposters. Therefore, you have to organize your imposters from most restrictive to least.
Here's an example of the contents of an imposters file with a single imposter:
[
+ {
+ "request": {
+ "method": "GET",
+ "endpoint": "/gophers/01D8EMQ185CA8PRGE20DKZTGSR"
+ },
+ "response": {
+ "status": 200,
+ "headers": {
+ "Content-Type": "application/json"
+ },
+ "body": "{\\"data\\":{\\"type\\":\\"gophers\\",\\"id\\":\\"01D8EMQ185CA8PRGE20DKZTGSR\\",\\"attributes\\":{\\"name\\":\\"Zebediah\\",\\"color\\":\\"Purples\\",\\"age\\":55}}}"
+ }
+ }
+]
+
This a very simple example. Killgrave has more possibilities for configuring imposters.
You can take a look at some of them in the "How to...?" section below.
`,9),p=[a];function i(r,u){return n(),t("div",null,p)}const c=s(o,[["render",i],["__file","your-first-imposter.html.vue"]]);export{c as default}; diff --git a/assets/your-first-imposter.html-t5dvpvCj.js b/assets/your-first-imposter.html-t5dvpvCj.js new file mode 100644 index 0000000..2912c24 --- /dev/null +++ b/assets/your-first-imposter.html-t5dvpvCj.js @@ -0,0 +1 @@ +const e=JSON.parse('{"key":"v-56beebfd","path":"/guide/your-first-imposter.html","title":"Your first imposter","lang":"en-US","frontmatter":{"prev":"./getting-started","next":"./advanced"},"headers":[],"git":{"updatedTime":1700862554000,"contributors":[{"name":"Joan López de la Franca Beltran","email":"joanjan14@gmail.com","commits":1}]},"filePathRelative":"guide/your-first-imposter.md"}');export{e as data}; diff --git a/cli/index.html b/cli/index.html new file mode 100644 index 0000000..d520866 --- /dev/null +++ b/cli/index.html @@ -0,0 +1,58 @@ + + + + + + + + +⚠️Remember that you will need to escape any special char, in the properties that admit text.
Killgrave is basically a command-line interface (CLI) that can be used with no explicit configuration, but a set of imposters. Look at the config reference or guide for further details.
However, you can tune up some of their settings like the host and port where the mock server is listening to, among others, by providing some configuration settings.
To provide those settings, you can either use the available CLI flags or use the -config
flag to provide the path to a settings file. In such case, you can either use a JSON or YAML configuration file.
See below the list of available flags:
$ killgrave -h
+
+ -config string
+ path to the configuration file
+ -debugger
+ run your server with the debugger
+ -debugger-addr string
+ debugger address (default "localhost:3030")
+ -host string
+ run your server on a different host (default "localhost")
+ -imposters string
+ directory where imposters are read from (default "imposters")
+ -port int
+ run your server on a different port (default 3000)
+ -proxy-mode string
+ proxy mode (choose between 'all', 'missing' or 'none') (default "none")
+ -proxy-url string
+ proxy url, use it in combination with proxy-mode
+ -secure
+ run your server using TLS (https)
+ -version
+ show the version of the application
+ -watcher
+ enable the file watcher, which reloads the server on every file change
+
Killgrave can be used without explicitly providing any configuration. However, you can tune up some of their settings like the host and port where the mock server is listening to, among others, by providing some configuration settings.
To provide those settings, you can either use the available CLI flags or use the -config
flag to provide the path to a settings file. In such case, you can either use JSON or YAML.
Minimal configuration attributes needed to get your mock up and running.
string
localhost
Specify the host for the Killgrave server. If you are using Docker, you must override the default value. Otherwise, the server will be reachable only from the container itself, so probably it won't work as you expect.
{
+ "host": "localhost"
+}
+
host: "localhost"
+
number
3000
Specify the port for the Killgrave server. If you are using Docker, you need to forward it.
{
+ "port": 3000
+}
+
port: 3000
+
string
imposters
Specify the directory the imposter files (either .imp.json
, .imp.yml
or .imp.yaml
) will be loaded from. On a regular set up, this directory will contain multiple of those imposter files and the directories with schemas
and responses
.
{
+ "imposters_path": "imposters"
+}
+
imposters_path: imposters
+
Set up a proxy to redirect the incoming requests as you want.
string
none
Specify the proxy mode for the Killgrave server. The default value is none
which means no proxy. Use missing
to redirect only those incoming requests that aren't defined within the imposters. Use all
to redirect all incoming requests.
{
+ "proxy": {
+ "mode": "missing"
+ }
+}
+
proxy:
+ mode: missing
+
string
Specify the url for the Killgrave's proxy. The incoming requests will be redirected to this url based on the proxy mode.
{
+ "proxy": {
+ "url": "https://example.com"
+ }
+}
+
proxy:
+ url: https://example.com
+
Set up the cross-origin resource sharing (CORS) mechanism for the Killgrave server. Especially useful when mocking servers that are consumed by frontend applications.
string array
["GET", "HEAD", "POST", "PUT", "OPTIONS", "DELETE", "PATCH", "TRACE", "CONNECT"]
Represents the Access-Control-Request-Method
header.
{
+ "cors": {
+ "methods": ["GET"]
+ }
+}
+
cors:
+ methods: ["GET"]
+
string array
["X-Requested-With", "Content-Type", "Authorization"]
Represents the Access-Control-Request-Headers
header.
{
+ "cors": {
+ "headers": ["Content-Type"]
+ }
+}
+
cors:
+ headers: ["Content-Type"]
+
string array
["Cache-Control", "Content-Language", "Content-Type", "Expires", "Last-Modified", "Pragma"]
Represents the Access-Control-Expose-Headers
header.
{
+ "cors": {
+ "exposed_headers": ["Cache-Control"]
+ }
+}
+
cors:
+ exposed_headers: ["Cache-Control"]
+
string array
[]
Represents the Access-Control-Allow-Origin
header.
{
+ "cors": {
+ "origins": ["*"]
+ }
+}
+
cors:
+ origins: ["*"]
+
boolean
false
Enables or disables the Access-Control-Allow-Credentials
header.
{
+ "cors": {
+ "allow_credentials": true
+ }
+}
+
cors:
+ allow_credentials: true
+
Sometimes you may want to simulate a production-like behavior with your mock servers, for instance to verify that your frontend application manages correctly HTTPS/TLS connections.
boolean
false
Expose the mock server through HTTP over TLS(SSL).
{
+ "secure": true
+}
+
secure: true
+
When building the imposters to mock your server, you way want to reduce the feedback loop on configuration changes.
boolean
false
Enable the file watcher to hot reload the mock server on every imposters change.
{
+ "watcher": true
+}
+
watcher: true
+
See below a full example of the configuration file:
{
+ "port": 3000,
+ "host": "localhost",
+ "imposters_path": "imposters",
+ "proxy": {
+ "url": "https://example.com",
+ "mode": "missing"
+ },
+ "cors": {
+ "methods": [
+ "GET"
+ ],
+ "headers": [
+ "Content-Type"
+ ],
+ "exposed_headers": [
+ "Cache-Control"
+ ],
+ "origins": [
+ "*"
+ ],
+ "allow_credentials": true
+ },
+ "secure": true,
+ "watcher": true
+}
+
port: 3000
+host: "localhost"
+imposters_path: "imposters"
+proxy:
+ url: https://example.com
+ mode: missing
+cors:
+ methods: ["GET"]
+ headers: ["Content-Type"]
+ exposed_headers: ["Cache-Control"]
+ origins: ["*"]
+ allow_credentials: true
+secure: true
+watcher: true
+
Imposters are the first-class citizens in Killgrave, they define how the mock server will respond to the incoming requests. You can use either JSON or YAML to define them.
The request
configuration is used to evaluate whether an incoming request matches the imposter or not. If there's a match, the mock server will respond with the imposter's response.
string
Specify the expected HTTP method.
{
+ "request": {
+ "method": "POST"
+ }
+}
+
request:
+ method: "POST"
+
string
Specify the expected URL endpoint (path). You can define URL parameters and use regular expressions. Look at Gorilla Mux examples to see how regular expressions work.
{
+ "request": {
+ "endpoint": "/gophers"
+ }
+}
+
request:
+ endpoint: /gophers
+
string
Specify the path to the file that contains the expected JSON schema.
{
+ "request": {
+ "schemaFile": "schemas/create_gopher_request.json"
+ }
+}
+
request:
+ schemaFile: "schemas/create_gopher_request.json"
+
string map
Specify the expected URL params. You can use regular expressions. Look at Gorilla Mux examples to see how regular expressions work.
{
+ "request": {
+ "params": {
+ "id": "01EKPT"
+ }
+ }
+}
+
request:
+ params:
+ id: "01EKPT"
+
string map
Specify the expected HTTP headers.
{
+ "request": {
+ "headers": {
+ "Content-Type": "application/json",
+ "Return-Error": "error"
+ }
+ }
+}
+
request:
+ headers:
+ Content-Type: "application/json"
+ Return-Error: "error"
+
The response
configuration is used to build the response in case of match with the incoming request.
number
200
Specify the response's HTTP status code.
{
+ "response": {
+ "status": 401
+ }
+}
+
response:
+ status: 401
+
string
Specify the raw contents (as string) to be returned as the response body. You could use a stringified JSON, for instance.
Although, in such cases where the desired response body is a complex payload (e.g. a JSON object), we recommend to use a separate file in combination with the bodyFile
request attribute.
{
+ "response": {
+ "body": "Simple response body"
+ }
+}
+
response:
+ body: "Simple response body"
+
string
Specify the path to the file that contains the response body's contents.
{
+ "response": {
+ "bodyFile": "/path/to/file"
+ }
+}
+
response:
+ bodyFile: "/path/to/file"
+
string map
Specify the response's HTTP headers.
{
+ "response": {
+ "headers": {
+ "Content-Type": "application/json",
+ "Return-Error": "error"
+ }
+ }
+}
+
response:
+ headers:
+ Content-Type: "application/json"
+ Return-Error: "error"
+
string
Specify the response delay. It is really helpful to reproduce real-world examples and/or to simulate situations with peaks of load where the responses are considerably slow.
You can use either a fixed time (single value) or an interval (two values joined by :
).
Look at Go time.ParseDuration
examples to see the accepted format to specify the time durations of the response delay.
{
+ "response": {
+ "delay": "2s:5s"
+ }
+}
+
response:
+ delay: "2s:5s"
+
See below some full examples:
{
+ "request": {
+ "method": "POST",
+ "endpoint": "/gophers",
+ "schemaFile": "schemas/create_gopher_request.json",
+ "params": {
+ "id": "01EKPT"
+ },
+ "headers": {
+ "Content-Type": "application/json",
+ "Return-Error": "error"
+ }
+ }
+}
+
request:
+ method: "POST"
+ endpoint: "/gophers"
+ schemaFile": "schemas/create_gopher_request.json"
+ params":
+ id: "01EKPT"
+ headers:
+ Content-Type: "application/json"
+ Return-Error: "error"
+
{
+ "response": {
+ "status": 401,
+ "body": "Simple response body",
+ "bodyFile": "/path/to/file",
+ "headers": {
+ "Content-Type": "application/json",
+ "Return-Error": "error"
+ },
+ "delay": "2s:5s"
+ }
+}
+
response:
+ status: 401
+ body: "Simple response body"
+ bodyFile: "/path/to/file"
+ headers:
+ Content-Type: "application/json"
+ Return-Error: "error"
+ delay: "2s:5s"
+
Note from the examples above that you should only provide one of body
or bodyFile
, but not both. Here both are defined just for the sake of showing a full example.
Killgrave has some advanced features:
If you want to use Killgrave from a frontend application, you should consider configuring cross-origin resource sharing (CORS).
In the CORS section of the file you can find the following options:
Access-Control-Request-Method
Access-Control-Request-Headers
Access-Control-Expose-Headers
Access-Control-Allow-Origin
Access-Control-Allow-Credentials
You can find further details here.
You can use Killgrave in proxy mode using the flags proxy-mode
and proxy-url
or their equivalent fields in the configuration file. The following three proxy modes are available:
none
: (Default) Killgrave won't behave as a proxy and will only use the configured imposters.missing
: Killgrave will try to match the request with a configured imposter. Otherwise, it will forward the request to the configured proxy.all
: Killgrave will forward all the incoming requests to the configured proxy.The proxy-url
must be the root path of the proxy server. For instance, if we have an API running on http://example.com/things
, the configured proxy-url
should be http://example.com
.
Killgrave has a secure mode that lets you expose your mock servers over secure connections by using HTTP over TLS/SSL (HTTPS).
You can use the secure
setting to enable the secure mode. Disabled by default.
If enabled, the mock server will use TLS options with a dummy certificate, to make it work with the HTTPS
protocol.
Killgrave has a file watcher that lets you hot reload your mock servers on every imposters change.
You can use the watcher
setting to enable the secure mode. Disabled by default.
If enabled, the file watcher will be watching changes on the imposters directory so the mock server will be restarted on every imposters change.
Killgrave has an interactive mode that lets you debug the behavior of your mock server.
You can use the debugger
settings to enable the interactive mode. Disabled by default.
You can find further details here.
Imposters are the most important concept in the Killgrave's world.
They conform the rules that determine how the mock server should respond to a request.
You can identify a Killgrave imposter file by its extension: .imp.json
, .imp.yml
or .imp.yaml
.
You can learn more about how to configure imposters in the Imposter Configuration Section.
The imposter object can be divided in two parts:
This part defines how Killgrave should determine whether an incoming request matches the imposter or not.
The request object has the following properties:
method
(mandatory): The HTTP method of the incoming request.endpoint
(mandatory): Path of the endpoint relative to the base. Supports regular expressions.schemaFile
: A JSON schema to validate the incoming request against.params
: Restrict incoming requests by query parameters. Supports regular expressions.headers
: Restrict incoming requests by HTTP header.This part defines how Killgrave should, in case of match, respond to the incoming request.
The response object has the following properties:
status
(mandatory): Integer defining the HTTP status code to return.body
or bodyFile
: The response body. Either a literal string (body
) or a path to a file (bodyFile
). bodyFile
is especially useful in the case of large outputs. This property is optional: if not response body should be returned it should be removed or left empty.headers
: HTTP headers to return in the response.delay
: Time the server waits before responding. This can help simulate network issues, or high server load. Uses the Go time.ParseDuration
format. Also, you can specify minimum and maximum delays separated by :
. The response delay will be chosen randomly between these values. Default value is "0s"
(no delay).To start Killgrave, you can simply do it by running:
$ killgrave
+
While you are welcome to provide your own configuration, Killgrave will default to the following configuration:
- imposters path:
imposters
- host:
localhost
- port:
3000
- CORS:
[]
- proxy:
none
- watcher:
false
- debugger:
- enabled:
false
- address:
localhost:3030
Killgrave is almost fully configurable through the command line, except for CORS, which can only be configured using the config file.
You can find the list with all the available flags at CLI section or by running:
$ killgrave -h
+
In case you are looking for a predictable and reproducible configuration, you can use the -config
command-line flag to specify the location of a configuration file, which can be written either in JSON or YAML.
You can find the list with all the available settings at Config Reference section.
As you can see, you can configure all the options in a very easy way.
Historically, the options
imposters_path
,port
andhost
were mandatory when using a configuration file. However, since the last version (v0.4.1
), they are no longer needed, so you can simply override those options if you actually want to. Furthermore, in previous versions (prior tov0.4.1
), theimposters_path
option was used to refer to the path to the working directory where Killgrave was executed from, but since the last version (v0.4.1
) this is relative to the location of the config file.
If you want to simulate a problem with the network, or create a more realistic response, you can use the delay
property.
The delay
property can take duration in the Go ParseDuration format. The server response will be delayed by the specified duration.
Alternatively, the delay
property can take a range of two durations, separated by :
. In this case, the server will respond with a random delay within this range.
With the example below, the response would be delayed between 1 and 5 seconds:
[
+ {
+ "request": {
+ "method": "POST",
+ "endpoint": "/gophers",
+ "schemaFile": "schemas/create_gopher_request.json",
+ "headers": {
+ "Content-Type": "application/json"
+ }
+ },
+ "response": {
+ "status": 201,
+ "headers": {
+ "Content-Type": "application/json"
+ },
+ "delay": "1s:5s"
+ }
+ }
+]
+
Killgrave allows dynamic responses. Using this feature, Killgrave can return different responses on the same endpoint.
To do this, all imposters need to be sorted from most restrictive to least. Killgrave tries to match the request with each of the imposters in sequence, stopping at the first imposter that matches the request.
In the following example, there are defined multiple imposters for the POST /gophers
endpoint:
[
+ {
+ "request": {
+ "method": "POST",
+ "endpoint": "/gophers",
+ "schemaFile": "schemas/create_gopher_request.json",
+ "headers": {
+ "Content-Type": "application/json"
+ }
+ },
+ "response": {
+ "status": 201,
+ "headers": {
+ "Content-Type": "application/json"
+ }
+ }
+ },
+ {
+ "request": {
+ "method": "POST",
+ "endpoint": "/gophers"
+ },
+ "response": {
+ "status": 400,
+ "headers": {
+ "Content-Type": "application/json"
+ },
+ "body": "{\"errors\":\"bad request\"}"
+ }
+ }
+]
+
Now,
schemaFile
.Sometimes you may need to validate requests more thoroughly. In such case, you can create an imposter that only matches with a valid json schema.
To do that you will need to define our json schema
first:
e.g. imposters/schemas/create_gopher_request.json
{
+ "type": "object",
+ "properties": {
+ "data": {
+ "type": "object",
+ "properties": {
+ "type": {
+ "type": "string",
+ "enum": [
+ "gophers"
+ ]
+ },
+ "attributes": {
+ "type": "object",
+ "properties": {
+ "name": {
+ "type": "string"
+ },
+ "color": {
+ "type": "string"
+ },
+ "age": {
+ "type": "integer"
+ }
+ },
+ "required": [
+ "name",
+ "color",
+ "age"
+ ]
+ }
+ },
+ "required": [
+ "type",
+ "attributes"
+ ]
+ }
+ },
+ "required": [
+ "data"
+ ]
+}
+
With this json schema
, we expect a request
like this:
{
+ "data": {
+ "type": "gophers",
+ "attributes": {
+ "name": "Zebediah",
+ "color": "Purples",
+ "age": 55
+ }
+ }
+}
+
Then the imposter could be configured as follows:
[
+ {
+ "request": {
+ "method": "POST",
+ "endpoint": "/gophers",
+ "schemaFile": "schemas/create_gopher_request.json",
+ "headers": {
+ "Content-Type": "application/json"
+ }
+ },
+ "response": {
+ "status": 201,
+ "headers": {
+ "Content-Type": "application/json"
+ }
+ }
+ }
+]
+
The path where the schema is located is relative to where the imposters are.
Killgrave uses the gorilla/mux regular expression format for endpoint matching.
In the next example, we have configured an endpoint to match with any kind of ULID ID:
[
+ {
+ "request": {
+ "method": "GET",
+ "endpoint": "/gophers/{_id:[\\w]{26}}"
+ },
+ "response": {
+ "status": 200,
+ "headers": {
+ "Content-Type": "application/json"
+ },
+ "body": "{\"data\":{\"type\":\"gophers\",\"id\":\"01D8EMQ185CA8PRGE20DKZTGSR\",\"attributes\":{\"name\":\"Zebediah\",\"color\":\"Purples\",\"age\":55}}}"
+ }
+ }
+]
+
Killgrave uses the gorilla/mux regular expression format for query parameter matching.
In this example, we have configured an imposter that only matches if we receive an apiKey as query parameter:
[
+ {
+ "request": {
+ "method": "GET",
+ "endpoint": "/gophers/{_id:[\\w]{26}}",
+ "params": {
+ "apiKey": "{_apiKey:[\\w]+}"
+ }
+ },
+ "response": {
+ "status": 200,
+ "headers": {
+ "Content-Type": "application/json"
+ },
+ "body": "{\"data\":{\"type\":\"gophers\",\"id\":\"01D8EMQ185CA8PRGE20DKZTGSR\",\"attributes\":{\"name\":\"Zebediah\",\"color\":\"Purples\",\"age\":55}}}"
+ }
+ }
+]
+
In this case we will not need the gorilla mux nomenclature
to write our regex.
In the next example, we have configured an imposter that uses regex to match an Authorization
header.
[
+ {
+ "request": {
+ "method": "GET",
+ "endpoint": "/gophers/{id:[\\w]{26}}",
+ "headers": {
+ "Authorization": "\\w+"
+ }
+ },
+ "response": {
+ "status": 200,
+ "headers": {
+ "Content-Type": "application/json"
+ },
+ "body": "{\"data\":{\"type\":\"gophers\",\"id\":\"01D8EMQ185CA8PRGE20DKZTGSR\",\"attributes\":{\"name\":\"Zebediah\",\"color\":\"Purples\",\"age\":55}}}"
+ }
+ }
+]
+
Killgrave is a tool that provides a simple way to create a powerful simulator for HTTP-based APIs.
The Killgrave's philosophy is to provide an easy way to configure your mock server, ensuring that you don't need to learn another tooling language. For that reason we use JSON and YAML to generate all necessary configurations.
Some Killgrave features are:
⚠️ Even though Killgrave is a very robust tool and is being used by some companies in production environments, it's still in initial development. Therefore, 'minor' version numbers are used to signify breaking changes and 'patch' version numbers are used for non-breaking changes or bugfixing. As soon as v1.0.0 is released, Killgrave will start to use SemVer as usual.
You can install Killgrave in different ways, but all of them are very simple:
One of them is of course using go install
, Killgrave is a Go project and therefore can be compiled using the go toolchain:
$ go install github.com/friendsofgo/killgrave/cmd/killgrave@{version}
+
Note that version
must be replaced by the version that you want to install. If left unspecified, the main
branch will be installed.
If you are a macOS user, you can install Killgrave using Homebrew:
$ brew install friendsofgo/tap/killgrave
+
⚠️ If you are installing via Homebrew, you always get the latest Killgrave version, we hope to fix this soon!
Killgrave is also available through Docker.
$ docker run -it --rm -p 3000:3000 -v $PWD/:/home -w /home friendsofgo/killgrave -host 0.0.0.0
+
-p 3000:3000
is used to forward the local port 3000
(Killgrave's default port) to container's port 3000
, otherwise Killgrave won't be reachable from the host.
-host 0.0.0.0
is used to change the Killgrave's default host (localhost
) to allow Killgrave to listen to and respond to incoming requests from outside the container, otherwise Killgrave won't be reachable from the host.
Windows and Linux users can download binaries from the GitHub Releases page.
At least one imposter must be configured in order to use Killgrave.
Every file with any of the valid extensions (.imp.json
, .imp.yml
or .imp.yaml
), present in the "imposters" folder (default "./imposters"
) will be interpreted as an imposters file.
We use a rule-based system to match requests to imposters. Therefore, you have to organize your imposters from most restrictive to least.
Here's an example of the contents of an imposters file with a single imposter:
[
+ {
+ "request": {
+ "method": "GET",
+ "endpoint": "/gophers/01D8EMQ185CA8PRGE20DKZTGSR"
+ },
+ "response": {
+ "status": 200,
+ "headers": {
+ "Content-Type": "application/json"
+ },
+ "body": "{\"data\":{\"type\":\"gophers\",\"id\":\"01D8EMQ185CA8PRGE20DKZTGSR\",\"attributes\":{\"name\":\"Zebediah\",\"color\":\"Purples\",\"age\":55}}}"
+ }
+ }
+]
+
This a very simple example. Killgrave has more possibilities for configuring imposters.
You can take a look at some of them in the "How to...?" section below.
⚠️Remember that you will need to escape any special char, in the properties that admit text.