Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F114426
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Award Token
Flag For Later
Size
3 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/lib/tesla/middleware/decode_rels.ex b/lib/tesla/middleware/decode_rels.ex
index 8c7c43c..5311fbb 100644
--- a/lib/tesla/middleware/decode_rels.ex
+++ b/lib/tesla/middleware/decode_rels.ex
@@ -1,54 +1,54 @@
defmodule Tesla.Middleware.DecodeRels do
@moduledoc """
Decode `Link` Hypermedia HTTP header into `opts[:rels]` field in response.
## Example usage
```
defmodule MyClient do
use Tesla
plug Tesla.Middleware.DecodeRels
end
env = MyClient.get("/...")
env.opts[:rels]
# => %{"Next" => "http://...", "Prev" => "..."}
```
"""
@behaviour Tesla.Middleware
@impl Tesla.Middleware
def call(env, next, _opts) do
env
|> Tesla.run(next)
|> parse_rels
end
defp parse_rels({:ok, env}), do: {:ok, parse_rels(env)}
defp parse_rels({:error, reason}), do: {:error, reason}
defp parse_rels(env) do
if link = Tesla.get_header(env, "link") do
Tesla.put_opt(env, :rels, rels(link))
else
env
end
end
defp rels(link) do
link
|> String.split(",")
|> Enum.map(&String.trim/1)
|> Enum.map(&rel/1)
|> Enum.into(%{})
end
defp rel(item) do
- Regex.run(~r/\A<(.+)>; rel="(.+)"\z/, item, capture: :all_but_first)
+ Regex.run(~r/\A<(.+)>; rel=["]?([^"]+)["]?\z/, item, capture: :all_but_first)
|> Enum.reverse()
|> List.to_tuple()
end
end
diff --git a/test/tesla/middleware/decode_rels_test.exs b/test/tesla/middleware/decode_rels_test.exs
index 6a32ff5..e01de96 100644
--- a/test/tesla/middleware/decode_rels_test.exs
+++ b/test/tesla/middleware/decode_rels_test.exs
@@ -1,38 +1,66 @@
defmodule Tesla.Middleware.DecodeRelsTest do
use ExUnit.Case
defmodule Client do
use Tesla
plug Tesla.Middleware.DecodeRels
adapter fn env ->
{:ok,
case env.url do
+ "/rels-with-semi-colon-in-quote" ->
+ Tesla.put_headers(env, [
+ {"link", ~s(<https://api.github.com/resource?page=2>; rel=next,
+ <https://api.github.com/resource?page=5>; rel="some;back",
+ <https://api.github.com/resource?page=5>; rel=last)}
+ ])
+
+ "/rels-with-no-quotes" ->
+ Tesla.put_headers(env, [
+ {"link", ~s(<https://api.github.com/resource?page=2>; rel=next,
+ <https://api.github.com/resource?page=5>; rel=last)}
+ ])
+
"/rels" ->
Tesla.put_headers(env, [
{"link", ~s(<https://api.github.com/resource?page=2>; rel="next",
<https://api.github.com/resource?page=5>; rel="last")}
])
_ ->
env
end}
end
end
- test "deocde rels" do
+ test "decode rels" do
assert {:ok, env} = Client.get("/rels")
assert env.opts[:rels] == %{
"next" => "https://api.github.com/resource?page=2",
"last" => "https://api.github.com/resource?page=5"
}
+
+ assert {:ok, unquoted_env} = Client.get("/rels-with-no-quotes")
+
+ assert unquoted_env.opts[:rels] == %{
+ "next" => "https://api.github.com/resource?page=2",
+ "last" => "https://api.github.com/resource?page=5"
+ }
+
+ assert {:ok, unquoted_env} = Client.get("/rels-with-semi-colon-in-quote")
+
+ assert unquoted_env.opts[:rels] == %{
+ "next" => "https://api.github.com/resource?page=2",
+ "last" => "https://api.github.com/resource?page=5",
+ "some;back" => "https://api.github.com/resource?page=5"
+ }
end
test "skip if no Link header" do
assert {:ok, env} = Client.get("/")
assert env.opts[:rels] == nil
end
end
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Tue, Nov 26, 7:22 AM (1 d, 12 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
39737
Default Alt Text
(3 KB)
Attached To
Mode
R28 tesla
Attached
Detach File
Event Timeline
Log In to Comment