diff --git a/README.md b/README.md index d1338d6..586a263 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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!") @@ -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 ``` @@ -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 ``` diff --git a/lib/bypass.ex b/lib/bypass.ex index 4d7ae77..08d90cc 100644 --- a/lib/bypass.ex +++ b/lib/bypass.ex @@ -5,12 +5,12 @@ defmodule Bypass do |> String.split("") |> 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 @@ -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}/"} setup_framework_integration(test_framework(), bypass) bypass end diff --git a/test/bypass_test.exs b/test/bypass_test.exs index 6f41000..791a8de 100644 --- a/test/bypass_test.exs +++ b/test/bypass_test.exs @@ -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