Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ defmodule TwitterClientTest do
Plug.Conn.resp(conn, 429, ~s<{"errors": [{"code": 88, "message": "Rate limit exceeded"}]}>)
end)

{:ok, client} = TwitterClient.start_link(url: endpoint_url(bypass.port))
{:ok, client} = TwitterClient.start_link(url: bypass.endpoint_url)
assert {:error, :rate_limited} == TwitterClient.post_tweet(client, "Elixir is awesome!")
end

Expand All @@ -68,7 +68,7 @@ defmodule TwitterClientTest do
Plug.Conn.resp(conn, 200, "")
end)

{:ok, client} = TwitterClient.start_link(url: endpoint_url(bypass.port))
{:ok, client} = TwitterClient.start_link(url: bypass.endpoint_url)

assert :ok == TwitterClient.post_tweet(client, "Elixir is awesome!")

Expand All @@ -85,8 +85,6 @@ defmodule TwitterClientTest do

assert :ok == TwitterClient.post_tweet(client, "Elixir is awesome!")
end

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

Expand Down Expand Up @@ -134,11 +132,9 @@ test configuration is basically the same, there are only two differences:
Plug.Conn.resp(conn, 429, ~s<{"errors": [{"code": 88, "message": "Rate limit exceeded"}]}>)
end)

{:ok, client} = TwitterClient.start_link(url: endpoint_url(shared.bypass.port))
{:ok, client} = TwitterClient.start_link(url: shared.bypass.endpoint_url))
assert {:error, :rate_limited} == TwitterClient.post_tweet(client, "Elixir is awesome!")
end

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

Expand Down
6 changes: 3 additions & 3 deletions lib/bypass.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ defmodule Bypass do
|> String.split("<!-- MDOC !-->")
|> Enum.fetch!(1)

defstruct pid: nil, port: nil
defstruct pid: nil, port: nil, endpoint_url: nil

@typedoc """
Represents a Bypass server process.
"""
@type t :: %__MODULE__{pid: pid, port: non_neg_integer}
@type t :: %__MODULE__{pid: pid, port: non_neg_integer, endpoint_url: String.t()}

import Bypass.Utils
require Logger
Expand Down Expand Up @@ -44,7 +44,7 @@ defmodule Bypass do
pid = start_instance(opts)
port = Bypass.Instance.call(pid, :port)
debug_log("Did open connection #{inspect(pid)} on port #{inspect(port)}")
bypass = %Bypass{pid: pid, port: port}
bypass = %Bypass{pid: pid, port: port, endpoint_url: "http://localhost:#{port}/"}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

localhost is usually resolves to loopback interface address - 127.0.0.1, however it is possible to bind to different interface. Therefore, the URL might not work.

It's safer to use use :inet.sockname/1 https://www.erlang.org/doc/man/inet.html#sockname-1 to retrieve listening address and then derive an interface address from :inet.getifaddrs/0 https://www.erlang.org/doc/man/inet.html#getifaddrs-0

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see some overlap with a PR I just created. For the ip address I made this PR: #137

setup_framework_integration(test_framework(), bypass)
bypass
end
Expand Down
5 changes: 5 additions & 0 deletions test/bypass_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -587,4 +587,9 @@ defmodule BypassTest do
Bypass.open(:error)
end
end

test "Bypass.open/1 open fills endpoint_url" do
bypass = Bypass.open(port: 8000)
assert bypass.endpoint_url == "http://localhost:8000/"
end
end