Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to return success or error based on body params #32

Open
kkl260 opened this issue Mar 16, 2021 · 1 comment
Open

How to return success or error based on body params #32

kkl260 opened this issue Mar 16, 2021 · 1 comment
Labels
enhancement New feature or request

Comments

@kkl260
Copy link

kkl260 commented Mar 16, 2021

So I'm not sure if there is a way to do this or not, but in your examples, you have a success endpoint and a failure endpoint. In real life, there would be only 1 endpoint that returned success or failure.

So in my example, a user logs in and the body would be something like {"email":"[email protected]","password":"12345"} and they would get a token returned.

So my mock looks like this:

    RequestFilter("/v1/login", method = Method.POST) to MockResponse().apply {
        setResponseCode(200)
        setBody(
            """
                {
                "token":"000000000000000000000"
                } 
            """.trimIndent()
        )
        setBodyDelay(1, TimeUnit.SECONDS)
        setHeadersDelay(1, TimeUnit.SECONDS)
    }

Which is working fine. But what if I wanted to test a case where the login credentials are bad and therefore it returns an error? This way I can test what the UI looks like after the error response. Obviously, I would need to do this from the same endpoint.

So I would want setResponseCode to be conditional by somehow checking the request body

@kkl260 kkl260 added the enhancement New feature or request label Mar 16, 2021
@donfuxx
Copy link
Owner

donfuxx commented Mar 27, 2021

Hi @kkl260. Looks like what you are looking for is:

RequestFilter("/v1/login", method = Method.POST, body = """{"email":"[email protected]","password":"12345"}""" ) to MockResponse().apply {
        setResponseCode(200)
...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants