Skip to content

Commit

Permalink
fix: merging adapter options using Opts middleware (#613)
Browse files Browse the repository at this point in the history
Signed-off-by: Yordis Prieto <[email protected]>
  • Loading branch information
yordis authored Sep 10, 2023
1 parent a49ee26 commit 5967b11
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
12 changes: 11 additions & 1 deletion lib/tesla/middleware/opts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ defmodule Tesla.Middleware.Opts do

@impl Tesla.Middleware
def call(env, next, opts) do
Tesla.run(%{env | opts: env.opts ++ opts}, next)
adapter =
env.opts
|> Keyword.get(:adapter, [])
|> Keyword.merge(opts[:adapter] || [])

opts =
env.opts
|> Keyword.merge(opts)
|> Keyword.put(:adapter, adapter)

Tesla.run(%{env | opts: opts}, next)
end
end
32 changes: 32 additions & 0 deletions test/tesla/middleware/opts_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,38 @@ defmodule Tesla.Middleware.OptsTest do
adapter fn env -> env end
end

defmodule BaseProxy do
defmacro __using__(_opts) do
quote do
plug Tesla.Middleware.Opts,
adapter: [
proxy: "https://proxy.example.com:8080",
proxy_auth: {"admin", "secret"}
]
end
end
end

defmodule MergeAdapterOptsClient do
use Tesla
use BaseProxy

plug Tesla.Middleware.Opts, adapter: [ssl_options: [{:versions, [:tlsv1]}]]
adapter fn env -> env end
end

test "merge adapter opts" do
env = MergeAdapterOptsClient.get("/")

assert env.opts == [
adapter: [
proxy: "https://proxy.example.com:8080",
proxy_auth: {"admin", "secret"},
ssl_options: [versions: [:tlsv1]]
]
]
end

test "apply middleware options" do
env = Client.get("/")

Expand Down

0 comments on commit 5967b11

Please sign in to comment.