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

After Upgrading bypass I am getting connection refused exceptions #74

Closed XOfSpades closed 4 years ago

XOfSpades commented 5 years ago

I just upgraded bypass and cowboy and since then I cannot get my tests running. I created a minimal example like this:

Dependencies in mix.exs:

  defp deps do
    [
      {:bypass, "~> 1.0", only: :test},
      {:httpotion, "~> 3.1"},
      {:cowboy, "~> 2.6"},
    ]
  end

Test case:

defmodule BypassTestTest do

  use ExUnit.Case, async: true

  setup do
    bypass = Bypass.open |> IO.inspect
    {:ok, bypass: bypass}
  end

  test "client can handle an error response", %{bypass: bypass} do
    Bypass.expect bypass, fn conn ->
      assert conn.method == "GET"
      Plug.Conn.resp(conn, 200, ~s<{"msg": "Yay"}>)
    end
    IO.puts "REQUEST"

    url = endpoint_url(bypass.port) <> "/1.1/statuses/update.json"
    |> IO.inspect
    HTTPotion.get(url) |> IO.inspect
  end

  defp endpoint_url(port), do: "http://localhost:#{port}"
end

Console output:

Start ExUnit
%Bypass{pid: #PID<0.224.0>, port: 44067}
REQUEST
"http://localhost:44067/1.1/statuses/update.json"
%HTTPotion.ErrorResponse{message: "econnrefused"}

  1) test client can handle an error response (BypassTestTest)
     test/bypass_test_test.exs:10
     No HTTP request arrived at Bypass
     stacktrace:
       (bypass) lib/bypass.ex:86: Bypass.do_verify_expectations/2
       (ex_unit) lib/ex_unit/on_exit_handler.ex:140: ExUnit.OnExitHandler.exec_callback/1
       (ex_unit) lib/ex_unit/on_exit_handler.ex:126: ExUnit.OnExitHandler.on_exit_runner_loop/0

Finished in 0.06 seconds
1 test, 1 failure

The code is here: https://github.com/XOfSpades/bypass_test

Is this a bug or am I doing it somehow wrong? I mean I nearly copied your example from the README. Thank you for your help.

os6sense commented 5 years ago

I have a similar problem. Thanks for the bypass_test example since it helped me to pin down the issue to httpotion which after updating to 3.1.2 fixed things for me (note I'm on 1.6.6 of elixir though - ymmv)

ream88 commented 4 years ago

Thx for the included example, did indeed not work, but updating httpotion with mix deps.update httpotion did fix it. Closing this! 😊

BTW, it seems that httpotion is not maintained anymore:

Upgraded:
  httpotion 3.1.0 => 3.1.3 RETIRED!
    (deprecated) Not really maintained, please check out Tesla