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
972 stars 113 forks source link

Problem trying to access the endpoint #27

Closed imetallica closed 7 years ago

imetallica commented 8 years ago

Hi! I'm doing

  test "capture from invoice.created", %{bypass: bypass} do
    Bypass.expect bypass, fn conn ->
      assert "PUT" == conn.method
      Plug.Conn.resp(conn, 201, ~s<{"response": "ok"}>)
    end

    send_invoice(bypass.port)
  end

def send_invoice(port) do
    case HTTPoison.put("http://localhost:#{port}/", Poison.encode!(to_send)) do
      {:ok, response} -> :ok
      {:error, reason} ->
        Logger.warn "Failed to callback the invoice. Reason: #{inspect reason}."
        :error
    end
  end

But it's failing with this error:

 No HTTP request arrived at Bypass
     stacktrace:
       (bypass) lib/bypass.ex:17: anonymous fn/1 in Bypass.open/0
       (ex_unit) lib/ex_unit/on_exit_handler.ex:82: ExUnit.OnExitHandler.exec_callback/1
       (ex_unit) lib/ex_unit/on_exit_handler.ex:66: ExUnit.OnExitHandler.on_exit_runner_loop/0

Am I doing something wrong?

alco commented 8 years ago

I don't see anything wrong in this code. Could you provide a minimal Mix project that reproduces the problem?

imetallica commented 8 years ago

Sure, just clone and do the ritual mix deps.get, mix test.

https://github.com/imetallica/bypass-issue

fastjames commented 8 years ago

I am having what appears to be the same problem, so I cloned the sample repo and I was able to replicate the problem. Also, I tried inserting a Process.sleep/0 call after bypass starts up. I grabbed the port number and verified that I was able to contact the bypass server via curl on that port. However, the sample app's test was still unable to do so.

fastjames commented 8 years ago

I also tried swapping in HTTPoison for Gun in the tests, since my app and the sample are both using that. It did not cause any tests to fail in a way that would suggest that gun is masking a problem.

fastjames commented 8 years ago

I noticed a small inconsistency while reading the bypass README and the tests. In the readme, it says you should use Plug.Conn.resp/3 to formulate the bypass response. However, in the test they use Plug.Conn.send_resp/3 instead. I tried changing this call in your sample app and it did not appear to change the outcome of the test, but I still wanted to make note of it.

fastjames commented 8 years ago

After further work on this, I think my problem was compound:

  1. I needed to use send_resp/3 instead of resp/3 in my bypass blocks
  2. I needed to properly start one of my dependency libraries in mix.exs

After fixing those two things, I had much better results. I'll make a note to submit a pull request for the doc change I mentioned in the previous comment.

alco commented 7 years ago

Closing this since it doesn't look like a bug in Bypass.

ghost commented 5 years ago
2. I needed to properly start one of my dependency libraries in mix.exs

@fastjames what were the dependencies that needed to start properly.

fastjames commented 5 years ago
2. I needed to properly start one of my dependency libraries in mix.exs

@fastjames what were the dependencies that needed to start properly.

@nikhilmor I looked back through some of my repos, but it has been several years and I don't recall the specific dependency. Sorry I can't be of more help.

ghost commented 5 years ago

I too am facing similar issue, tried a number of things but to no avail. @fastjames thanks for your input. You were really fast with the response.