PSPDFKit-labs / bypass

Bypass provides a quick way to create a custom plug that can be put in place instead of an actual HTTP server to return prebaked responses to client requests.
https://hex.pm/packages/bypass
MIT License
964 stars 111 forks source link

Support expectation on exact number of requests #128

Open RudolfMan opened 1 year ago

RudolfMan commented 1 year ago

Hi there!

Sometimes I want to set an expectation that my app will call particular endpoint an exact number of times.. I figured I can achieve that by setting some counter and increment it on each request, then assert on its value.. though, I think it would be nice to be able to set that expectation via Bypass.expect functions.. something similar to Mox.expect/4

So in this change I attempted to do exactly that:

Bypass.expect(bypass, 3, fn conn ->
  # ...
  # Plug.Conn.send_resp(conn, 200, "")
end)

and

Bypass.expect(bypass, "GET", "/foo", 3, fn conn ->
  # ...
  # Plug.Conn.send_resp(conn, 200, "")
end)

And if that number of requests is not met - it will raise an error:

     Expected 3 HTTP request for Bypass at GET /foo, got 5
     stacktrace:
       (bypass 2.1.0) lib/bypass.ex:122: Bypass.do_verify_expectations/2
       ...
RudolfMan commented 1 year ago

@bszaf, thank you for the review. I added docs in https://github.com/PSPDFKit-labs/bypass/pull/128/commits/7609e767f215915c3f5e9a4ac09377ba105c302a