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

How to use bypass with Finch? #116

Closed
danicuki opened this issue Dec 18, 2020 · 2 comments
Closed

How to use bypass with Finch? #116

danicuki opened this issue Dec 18, 2020 · 2 comments

Comments

@danicuki
Copy link

danicuki commented Dec 18, 2020

My project uses Finch to make parallel HTTP requests.

I tried to add bypass to my tests, but the HTTP requests are not being detected. When I run the test, I get this error:

No HTTP request arrived at Bypass

Here is my test:

defmodule MyClientTest do
  use ExUnit.Case, async: true

  setup do
    bypass = Bypass.open()
    {:ok, bypass: bypass}
  end

  describe "list_apps" do
    test "should have an expected app", %{bypass: bypass} do
      {:ok, contents} = File.read("test/apps.json")

      Bypass.expect(
        bypass,
        fn conn ->
          Plug.Conn.resp(conn, 200, contents)
        end
      )

      list_apps = MyClient.list_apps()
      assert length(list_apps) == 57
    end
  end
end

Here is my MyClient module:

defmodule MyClient do
  alias Finch.Response

  def child_spec do
    {Finch,
     name: __MODULE__,
     pools: %{
       "https://myapp.com" => [size: 100]
     }}
  end

  def applications_response do
    :get
    |> Finch.build("https://myapp.com/v2/apps.json")
    |> Finch.request(__MODULE__)
  end

  def handle_applications_response({:ok, %Response{body: body}}) do
    body
    |> Jason.decode!()
    end
  end

  def list_apps do
    handle_applications_response(applications_response())
  end

end
@wkirschbaum
Copy link

I don't think bypass does anything magical, so you still have to set your host on the client to the bypass listener: host = "http://localhost:#{bypass.port}"

@ream88
Copy link
Collaborator

ream88 commented Dec 22, 2020

@wkirschbaum is right, basically you need to instruct Finch to use another URL for testing. I also build an example app here: https://github.com/ream88/bypass-issue116

@ream88 ream88 closed this as completed Dec 22, 2020
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

3 participants