friendsofgo / killgrave

Simple way to generate mock servers written in Go

Home Page:https://friendsofgo.github.io/killgrave/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Examples about dynamic responses

Danielavw opened this issue · comments

Hi guys
I was messing around your server mocker, and I was planning using it in one of our projects :)
I miss some examples, in specific, about how deal with dynamic responses based on regex endpoint or request schema. So, based in a value from request, I want to get that value and reflect it into the generated response.
If possible, could you provide some kind of example about that feature?
Thanks a lot in advance

Autoanswer:Just putting tags just like ##test1## in the response:
{
"param1":"##test1##",
"param2":"blablablah"
}
Thanks!

Hi @Danielavw you're right we need to improve a little our documentation, to use dynamic responses you could uses somes stuff:

First of all you could you use the schemaFile to indicate a proper json schema, that you could create and the endpoint must met.

{
        "request": {
            "method": "POST",
            "endpoint": "/gophers",
            "schemaFile": "schemas/create_gopher_request.json"
        },
        "response": {
            "status": 500,
            "headers": {
                "Content-Type": "application/json"
            },
            "body": "{\"error\": \"Server error ocurred\"}",
            "delay": "1s:5s"
        }
    }

Another one is the params:

{
        "request": {
            "method": "POST",
            "endpoint": "/gophers",
            "schemaFile": "schemas/create_gopher_request.json",
            "params": {
                "id": "123456                
            }
        },
        "response": {
            "status": 500,
            "headers": {
                "Content-Type": "application/json"
            },
            "body": "{\"error\": \"Server error ocurred\"}",
            "delay": "1s:5s"
        }
    }

For example to use this imposter, you should call to http://localhost:3000/gophers?id=123456, you could include regex inside with a handicap that using gorilla mux so we need to declare as variable:

{
        "request": {
            "method": "POST",
            "endpoint": "/gophers",
            "schemaFile": "schemas/create_gopher_request.json",
            "params": {
                "id": "{_id:[0-9]+}"
            }
        },
        "response": {
            "status": 500,
            "headers": {
                "Content-Type": "application/json"
            },
            "body": "{\"error\": \"Server error ocurred\"}",
            "delay": "1s:5s"
        }
    }

Another one is the header option:

{
        "request": {
            "method": "POST",
            "endpoint": "/gophers",
            "headers": {
                "Content-Type": "application/json",
                "Return-Error": "error"
            }
        },
        "response": {
            "status": 500,
            "headers": {
                "Content-Type": "application/json"
            },
            "body": "{\"error\": \"Server error ocurred\"}",
            "delay": "1s:5s"
        }
    }

All options must be met for the endpoint to match, you could combine these options whatever you want. And for create dynamic responses you need to play with this options and sort your imposters from more restrictive to less.

[
    {
        "request": {
            "method": "POST",
            "endpoint": "/gophers",
            "schemaFile": "schemas/create_gopher_request.json",
            "headers": {
                "Content-Type": "application/json",
                "Return-Error": "error"
            }
        },
        "response": {
            "status": 500,
            "headers": {
                "Content-Type": "application/json"
            },
            "body": "{\"error\": \"Server error ocurred\"}",
            "delay": "1s:5s"
        }
    },
    {
        "request": {
            "method": "POST",
            "endpoint": "/gophers",
            "schemaFile": "schemas/create_gopher_request.json",
            "headers": {
                "Content-Type": "application/json"
            }
        },
        "response": {
            "status": 200,
            "headers": {
                "Content-Type": "application/json"
            },
            "bodyFile": "responses/create_gopher_response.json"
        }
    }
]

In the above example you can call to /gophers only with the header "Content-Type": "application/json" and then the response will be a 200 OK with the information , but if you include the header "Return-Error": "error" then the response print an error 500.

I hope I have resolved your doubts, and we will work on improve a lot the documentation ^^