Page MenuHomePhorge

No OneTemporary

Size
4 KB
Referenced Files
None
Subscribers
None
diff --git a/lib/tesla/middleware/core.ex b/lib/tesla/middleware/core.ex
index a020574..e68b62a 100644
--- a/lib/tesla/middleware/core.ex
+++ b/lib/tesla/middleware/core.ex
@@ -1,74 +1,74 @@
defmodule Tesla.Middleware.BaseUrl do
def call(env, run, base) do
env = if !Regex.match?(~r/^https?:\/\//, env.url) do
%{env | url: base <> env.url}
else
env
end
run.(env)
end
end
defmodule Tesla.Middleware.Headers do
def call(env, run, headers) do
headers = Map.merge(env.headers, headers)
run.(%{env | headers: headers})
end
end
defmodule Tesla.Middleware.QueryParams do
def call(env, run, query) do
env = %{env | url: merge_url_and_query(env.url, query)}
run.(env)
end
@spec merge_url_and_query(String.t, %{}) :: String.t
def merge_url_and_query(url, query) do
query = for {key, val} <- query, into: %{}, do: {to_string(key), val}
uri = URI.parse(url)
q = if uri.query do
- old_query = URI.decode_query(uri.query)
- Map.merge(old_query, query)
+ env_query = URI.decode_query(uri.query)
+ Map.merge(query, env_query)
else
query
end
uri |> Map.put(:query, URI.encode_query(q)) |> URI.to_string
end
end
defmodule Tesla.Middleware.DecodeRels do
def call(env, run, []) do
env = run.(env)
if env.headers['Link'] do
rels = env.headers['Link']
|> to_string
|> String.split(",")
|> Enum.map(&String.strip/1)
|> Enum.map(fn e -> Regex.run(~r/\A<(.+)>; rel="(.+)"\z/, e, capture: :all_but_first) |> List.to_tuple end)
|> Enum.reduce(%{}, fn ({url, key}, a) -> Dict.put(a, key, url) end)
env |> Map.put(:rels, rels)
else
env
end
end
end
defmodule Tesla.Middleware.AdapterOptions do
def call(env, run, opts) do
run.(%{env | opts: env.opts ++ opts})
end
end
defmodule Tesla.Middleware.BaseUrlFromConfig do
def call(env, run, opts) do
run.(%{env | url: config(opts)[:base_url] <> env.url})
end
defp config(opts) do
Application.get_env(Keyword.fetch!(opts, :otp_app), Keyword.fetch!(opts, :module))
end
end
diff --git a/test/tesla/middleware/core_test.exs b/test/tesla/middleware/core_test.exs
index a48a9dc..09c9099 100644
--- a/test/tesla/middleware/core_test.exs
+++ b/test/tesla/middleware/core_test.exs
@@ -1,41 +1,41 @@
defmodule CoreTest do
use ExUnit.Case
# TODO: Change order of arguments to (mid, opts, env)
def call(mid, env, opts) do
mid.call(env, fn a -> a end, opts)
end
test "Tesla.Middleware.BaseUrl" do
env = call(Tesla.Middleware.BaseUrl, %{url: "/path"}, "http://example.com")
assert env.url == "http://example.com/path"
end
test "Tesla.Middleware.BaseUrl - skip double append" do
env = call(Tesla.Middleware.BaseUrl, %{url: "http://other.foo"}, "http://example.com")
assert env.url == "http://other.foo"
end
test "Tesla.Middlware.QueryParams - joining default query params" do
e = %Tesla.Env{url: "http://example.com"}
env = call(Tesla.Middleware.QueryParams, e, %{access_token: "secret_token"})
assert env.url == "http://example.com?access_token=secret_token"
end
- test "Tesla.Middlware.QueryParams - joining onto existing query params" do
- e = %Tesla.Env{url: "http://example.com?foo=bar&access_token=old_token"}
- env = call(Tesla.Middleware.QueryParams, e, %{access_token: "secret_token"})
- assert env.url == "http://example.com?access_token=secret_token&foo=bar"
+ test "Tesla.Middlware.QueryParams - should not override existing key" do
+ e = %Tesla.Env{url: "http://example.com?foo=bar&access_token=params_token"}
+ env = call(Tesla.Middleware.QueryParams, e, %{access_token: "middleware_token"})
+ assert env.url == "http://example.com?access_token=params_token&foo=bar"
end
test "Tesla.Middleware.Headers" do
env = call(Tesla.Middleware.Headers, %{headers: %{}}, %{'Content-Type' => 'text/plain'})
assert env.headers == %{'Content-Type' => 'text/plain'}
end
test "Tesla.Middleware.BaseUrlFromConfig" do
Application.put_env(:tesla, SomeModule, [base_url: "http://example.com"])
env = call(Tesla.Middleware.BaseUrlFromConfig, %{url: "/path"}, otp_app: :tesla, module: SomeModule)
assert env.url == "http://example.com/path"
end
end

File Metadata

Mime Type
text/x-diff
Expires
Sat, Nov 23, 5:13 PM (1 d, 4 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
39030
Default Alt Text
(4 KB)

Event Timeline