envoy / Ambassador

Super lightweight web framework in Swift based on SWSGI

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Supporting different states of a mocked response

AyaAkl25 opened this issue · comments

Hey guys,

just wondering because I didn't find a doc about this, but what we have a case of doing a mock to different states of the same response.
for example:
/welcome api will return "Hi World" and then on subsequent /welcome needs to be returned like “Hi Globe”

is there a way to play around this that could enable me to mock the response in states
because my issue now is that the UI test just continue without waiting the response to be fully mocked (same response) or it just override everything to the latest mock

Thanks so much guys for your support that I really need because I am stuck in this for quite some time :)

@Aya-Akl

You can use define a state class and pass the reference into your response handler closure. Modify the value of State every time it gets called. Here's an example

class State {
    var index = 0
}
let state = State()
let responses: [String] = ["Hi World", "Hi Globe", "Hola", "What's up"]

router["/responses"] = DelayResponse(JSONResponse(handler: { [state, responses] _ -> Any in
    let index = state.index
    state.index += 1
    state.index = state.index % responses.count
    return [
        "value": responses[state.index]
    ]
}))

@fangpenlin Thank you so much for this, it helped me so much adding my data responses and passing them in states.

there is some concern left, when I did this, sometimes the responses got mixed so the API is not mocked in the same order of state I am given, I had to use sleep(timeout) which I don't like, is there any reason might be the reason for this issue?