Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F140871
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Award Token
Flag For Later
Size
6 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/lib/tesla/middleware/core.ex b/lib/tesla/middleware/core.ex
index 293839c..8a9069e 100644
--- a/lib/tesla/middleware/core.ex
+++ b/lib/tesla/middleware/core.ex
@@ -1,130 +1,130 @@
defmodule Tesla.Middleware.Normalize do
def call(env, next, _opts) do
env
|> normalize
|> Tesla.run(next)
|> normalize
end
def normalize({:error, reason}) do
raise %Tesla.Error{message: "adapter error: #{inspect reason}", reason: reason}
end
def normalize(env) do
env
|> Map.update!(:status, &normalize_status/1)
|> Map.update!(:headers, &normalize_headers/1)
|> Map.update!(:body, &normalize_body/1)
end
def normalize_status(nil), do: nil
def normalize_status(status) when is_integer(status), do: status
def normalize_status(status) when is_binary(status), do: status |> String.to_integer
def normalize_status(status) when is_list(status), do: status |> to_string |> String.to_integer
def normalize_headers(headers) when is_map(headers) or is_list(headers) do
Enum.into headers, %{}, fn {k,v} ->
{k |> to_string |> String.downcase, v |> to_string}
end
end
def normalize_body(data) when is_list(data), do: to_string(data)
def normalize_body(data), do: data
end
defmodule Tesla.Middleware.BaseUrl do
def call(env, next, base) do
env
|> apply_base(base)
|> Tesla.run(next)
end
defp apply_base(env, base) do
if Regex.match?(~r/^https?:\/\//, env.url) do
env # skip if url is already with scheme
else
%{env | url: join(base, env.url)}
end
end
defp join(base, url) do
case {String.last(to_string(base)), url} do
{nil, url} -> url
{"/", "/" <> rest} -> base <> rest
{"/", rest} -> base <> rest
{_, "/" <> rest} -> base <> "/" <> rest
{_, rest} -> base <> "/" <> rest
end
end
end
defmodule Tesla.Middleware.Headers do
def call(env, next, headers) do
env
|> merge(headers)
|> Tesla.run(next)
end
defp merge(env, nil), do: env
defp merge(env, headers) do
Map.update!(env, :headers, &Map.merge(&1, headers))
end
end
defmodule Tesla.Middleware.Query do
def call(env, next, query) do
env
|> merge(query)
|> Tesla.run(next)
end
defp merge(env, nil), do: env
defp merge(env, query) do
Map.update!(env, :query, & &1 ++ query)
end
end
defmodule Tesla.Middleware.DecodeRels do
def call(env, next, _opts) do
env
|> Tesla.run(next)
|> parse_rels
end
def parse_rels(env) do
- if link = env.headers["Link"] do
+ if link = env.headers["link"] do
Map.update!(env, :opts, &Keyword.put(&1, :rels, rels(link)))
else
env
end
end
defp rels(link) do
link
|> String.split(",")
|> Enum.map(&String.strip/1)
|> Enum.map(&rel/1)
|> Enum.into(%{})
end
def rel(item) do
Regex.run(~r/\A<(.+)>; rel="(.+)"\z/, item, capture: :all_but_first)
|> Enum.reverse
|> List.to_tuple
end
end
defmodule Tesla.Middleware.BaseUrlFromConfig do
def call(env, next, opts) do
base = config(opts)[:base_url]
Tesla.Middleware.BaseUrl.call(env, next, base)
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 81b0596..0af7248 100644
--- a/test/tesla/middleware/core_test.exs
+++ b/test/tesla/middleware/core_test.exs
@@ -1,87 +1,91 @@
defmodule CoreTest do
use ExUnit.Case
+ alias Tesla.Middleware.Normalize
describe "Tesla.Middleware.BaseUrl" do
alias Tesla.Middleware.BaseUrl
use Tesla.Middleware.TestCase, middleware: BaseUrl
test "base without slash, path without slash" do
env = BaseUrl.call(%Tesla.Env{url: "path"}, [], "http://example.com")
assert env.url == "http://example.com/path"
end
test "base without slash, path with slash" do
env = BaseUrl.call(%Tesla.Env{url: "/path"}, [], "http://example.com")
assert env.url == "http://example.com/path"
end
test "base with slash, path without slash" do
env = BaseUrl.call(%Tesla.Env{url: "path"}, [], "http://example.com/")
assert env.url == "http://example.com/path"
end
test "base with slash, path with slash" do
env = BaseUrl.call(%Tesla.Env{url: "/path"}, [], "http://example.com/")
assert env.url == "http://example.com/path"
end
test "skip double append" do
env = BaseUrl.call(%Tesla.Env{url: "http://other.foo"}, [], "http://example.com")
assert env.url == "http://other.foo"
end
end
describe "Tesla.Middleware.Query" do
alias Tesla.Middleware.Query
use Tesla.Middleware.TestCase, middleware: Query
test "joining default query params" do
env = Query.call %Tesla.Env{}, [], page: 1
assert env.query == [page: 1]
end
test "should not override existing key" do
env = Query.call %Tesla.Env{query: [page: 1]}, [], [page: 5]
assert env.query == [page: 1, page: 5]
end
end
describe "Tesla.Middleware.Headers" do
alias Tesla.Middleware.Headers
use Tesla.Middleware.TestCase, middleware: Headers
test "merge headers" do
env = Headers.call %Tesla.Env{headers: %{"Authorization" => "secret"}}, [], %{"Content-Type" => "text/plain"}
assert env.headers == %{"Authorization" => "secret", "Content-Type" => "text/plain"}
end
end
describe "Tesla.Middleware.DecodeRels" do
alias Tesla.Middleware.DecodeRels
use Tesla.Middleware.TestCase, middleware: DecodeRels
test "deocde rels" do
headers = %{"Link" => ~s(<https://api.github.com/resource?page=2>; rel="next",
<https://api.github.com/resource?page=5>; rel="last")}
- env = DecodeRels.call %Tesla.Env{headers: headers}, [], nil
+
+ env = %Tesla.Env{headers: headers}
+ |> Normalize.call([], nil)
+ |> DecodeRels.call([], nil)
assert env.opts[:rels] == %{
"next" => "https://api.github.com/resource?page=2",
"last" => "https://api.github.com/resource?page=5"
}
end
end
test "Tesla.Middleware.BaseUrlFromConfig" do
Application.put_env(:tesla, SomeModule, [base_url: "http://example.com"])
env = Tesla.Middleware.BaseUrlFromConfig.call %Tesla.Env{url: "/path"}, [], otp_app: :tesla, module: SomeModule
assert env.url == "http://example.com/path"
end
end
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Wed, Jan 22, 5:56 AM (1 d, 18 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
55795
Default Alt Text
(6 KB)
Attached To
Mode
R28 tesla
Attached
Detach File
Event Timeline
Log In to Comment