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

How to test request timeout with bypass? #117

Closed ahmadferdous closed 2 years ago

ahmadferdous commented 3 years ago

Hi,

Bypass is an awesome library for integration testing. It helped me uncover a few problems with my implementation that regular unit tests failed to catch (yup, because mocked response was incorrect 😂 ).

I was looking for a way to write an integration test for request timeout scenario. I looked at the documentation and the bypass code but couldn't figure out a way. Does bypass support this? Or is there way a way to do that? Please help.

Thanks.

rlopzc commented 3 years ago

I used mock:

      with_mock Finch,
        build: fn _, _, _, _ -> :ok end,
        request: fn _, _ -> {:error, %Mint.TransportError{reason: :timeout}} end do
        assert 1 == 1
      end
sneako commented 2 years ago

Here is an example where we test request timeouts using Process.sleep/1 https://github.com/sneako/finch/blob/main/test/finch_test.exs#L318

With this method Bypass accepts the connection but waits to send the response.

You can use a short timeout to avoid slowing down your test suite too much.