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

Cannot mock 5xx responses #14

Closed
feld opened this issue Aug 8, 2023 · 3 comments
Closed

Cannot mock 5xx responses #14

feld opened this issue Aug 8, 2023 · 3 comments

Comments

@feld
Copy link

feld commented Aug 8, 2023

If I try to mock a 5xx response it blows up the entire test. I'm using the Bandit adapter.

e.g.,

    TestServer.add("/some/path",
      via: :get,
      to: fn conn ->
        Plug.Conn.send_resp(conn, 500, "")
      end
    )
14:20:45.892 [error] retry: got response with status 500, will retry in 1000ms, 3 attempts left
warning: ** (RuntimeError) TestServer.Instance #PID<0.628.0> received an unexpected GET request at /some/path.


No active routes. The following routes have been processed:

(list of all the routes here)
@feld
Copy link
Author

feld commented Aug 8, 2023

I started digging deeper and found the error message is actually from Req.

I'm in the process of attempting to switch a project away from Tesla/Tesla.Mock to Req/TestServer

@feld
Copy link
Author

feld commented Aug 8, 2023

I have the solution. If your code expects to be able to do intelligent handling of 408/429/5xx responses you must pass retry: false to Req or else this kind of scenario surfaces and you will not be able to validate it in your tests.

Req is batteries included, but this is more than I expected 🥲

Sorry for the noise, hopefully someone else finds this useful.

@feld feld closed this as completed Aug 8, 2023
@danschultzer
Copy link
Owner

danschultzer commented Aug 8, 2023

Yeah, by default req will retry four times, so either you'll have to add four expectations (could be a small helper function you wrap around TestServer.add/2), or add retry: false as you did (though that setting should only exist for test env). I think the latter is much better choice because otherwise the test becomes super slow with the throttle that req has on retries.

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

No branches or pull requests

2 participants