Skip to content

Commit

Permalink
Merge pull request #143 from polvalente/fix/dont-require-tesla-or-htt…
Browse files Browse the repository at this point in the history
…poison

fix: don't require Tesla or HTTPoison
  • Loading branch information
Fedjmike authored Sep 23, 2021
2 parents b345980 + 3f417bb commit 72e788b
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 13 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ config :reverse_proxy_plug, :http_client, ReverseProxyPlug.HTTPClient.Adapters.H
config :reverse_proxy_plug, :http_client, ReverseProxyPlug.HTTPClient.Adapters.Tesla
```

You can also set the config as a per-plug basis, which will override any global config.
Either of those must be set, otherwise the system will attempt to default to the HTTPoison
adapter or raise if it's not present.

```elixir
plug ReverseProxyPlug, client: ReverseProxyPlug.HTTPClient.Adapters.Tesla
```

## Usage

The plug works best when used with
Expand Down
1 change: 0 additions & 1 deletion config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,3 @@ use Mix.Config
# Configuration from the imported file will override the ones defined
# here (which is why it is important to import them last).
#
# import_config "#{Mix.env()}.exs"
22 changes: 15 additions & 7 deletions lib/reverse_proxy_plug.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ defmodule ReverseProxyPlug do
end

opts
|> ensure_http_client()
|> Keyword.merge(upstream_parts)
|> Keyword.put_new(:client, http_client())
|> Keyword.put_new(:client_options, [])
|> Keyword.put_new(:response_mode, :stream)
|> Keyword.put_new(:status_callbacks, %{})
Expand Down Expand Up @@ -413,11 +413,19 @@ defmodule ReverseProxyPlug do
"#{host}:#{port}"
end

defp http_client do
Application.get_env(
:reverse_proxy_plug,
:http_client,
HTTPClient.Adapters.HTTPoison
)
defp ensure_http_client(opts) do
client = opts[:client] || Application.get_env(:reverse_proxy_plug, :http_client)

cond do
not is_nil(client) ->
Keyword.put(opts, :client, client)

Code.ensure_loaded?(HTTPClient.Adapters.HTTPoison) and is_nil(client) ->
Keyword.put(opts, :client, HTTPClient.Adapters.HTTPoison)

true ->
raise ArgumentError,
":client option or :reverse_proxy_plug, :http_client global config must be set"
end
end
end
3 changes: 0 additions & 3 deletions lib/reverse_proxy_plug/http_client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,4 @@ defmodule ReverseProxyPlug.HTTPClient do
| __MODULE__.AsyncResponse.t()
| __MODULE__.MaybeRedirect.t()}
| {:error, error()}

if !Code.ensure_loaded?(HTTPoison) && !Code.ensure_loaded?(Tesla),
do: raise("Either http_poison or tesla must be available")
end
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ defmodule ReverseProxyPlug.MixProject do
{:credo, "~> 1.0", only: [:dev, :test]},
{:mox, "~> 1.0", only: :test, optional: true},
{:ex_doc, ">= 0.0.0", only: :dev, runtime: false},
{:tesla, "~> 1.4.1", optional: true},
{:tesla, "~> 1.4", optional: true},
{:bypass, "~> 2.1.0", optional: true, only: :test}
]
end
Expand Down
47 changes: 46 additions & 1 deletion test/reverse_proxy_plug_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -594,11 +594,56 @@ defmodule ReverseProxyPlugTest do

plug(ReverseProxyPlug,
upstream: "",
error_callback: {__MODULE__, :error_handler, []}
error_callback: {__MODULE__, :error_handler, []},
client: ReverseProxyPlug.HTTPClient.Adapters.HTTPoison
)
end
end

test "must be initialized with either global or local config set" do
prev = Application.get_env(:reverse_proxy_plug, :http_client)

on_exit(fn -> Application.put_env(:reverse_proxy_plug, :http_client, prev) end)

Application.put_env(:reverse_proxy_plug, :http_client, nil)

# Ensure it defaults to HTTPoison for retrocompatibility

assert opts =
ReverseProxyPlug.init(
upstream: "",
error_callback: {__MODULE__, :error_handler, []}
)

assert opts[:client] == ReverseProxyPlug.HTTPClient.Adapters.HTTPoison

adapter = MyCustomAdapter

assert opts =
ReverseProxyPlug.init(
upstream: "",
error_callback: {__MODULE__, :error_handler, []},
client: adapter
)

assert adapter == opts[:client]

Application.put_env(
:reverse_proxy_plug,
:http_client,
adapter
)

assert opts =
ReverseProxyPlug.init(
upstream: "",
error_callback: {__MODULE__, :error_handler, []},
client: nil
)

assert adapter == opts[:client]
end

test_stream_and_buffer "recycles cookies from connection" do
%{opts: opts, get_responder: get_responder} = test_reuse_opts

Expand Down

0 comments on commit 72e788b

Please sign in to comment.