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

fix: merging adapter options using Opts middleware #613

Merged
merged 1 commit into from
Sep 10, 2023
Merged
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
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
Loading