From 5967b117ec1afc9f3b1982bab6b9c2250f4710c1 Mon Sep 17 00:00:00 2001 From: Yordis Prieto Date: Sun, 10 Sep 2023 08:59:04 -0400 Subject: [PATCH] fix: merging adapter options using Opts middleware (#613) Signed-off-by: Yordis Prieto --- lib/tesla/middleware/opts.ex | 12 ++++++++++- test/tesla/middleware/opts_test.exs | 32 +++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/lib/tesla/middleware/opts.ex b/lib/tesla/middleware/opts.ex index f8f30b88..6ca14b66 100644 --- a/lib/tesla/middleware/opts.ex +++ b/lib/tesla/middleware/opts.ex @@ -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 diff --git a/test/tesla/middleware/opts_test.exs b/test/tesla/middleware/opts_test.exs index bf17280c..f1b8931f 100644 --- a/test/tesla/middleware/opts_test.exs +++ b/test/tesla/middleware/opts_test.exs @@ -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("/")