Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F140668
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Award Token
Flag For Later
Size
4 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/lib/tesla/middleware/follow_redirects.ex b/lib/tesla/middleware/follow_redirects.ex
index a6a6313..feb87c3 100644
--- a/lib/tesla/middleware/follow_redirects.ex
+++ b/lib/tesla/middleware/follow_redirects.ex
@@ -1,48 +1,48 @@
defmodule Tesla.Middleware.FollowRedirects do
@moduledoc """
Follow 301/302 redirects
Example:
defmodule MyClient do
use Tesla
plug Tesla.Middleware.FollowRedirects, max_redirects: 3 # defaults to 5
end
"""
@max_redirects 5
@redirect_statuses [301, 302, 307, 308]
def call(env, next, opts \\ []) do
max = Keyword.get(opts || [], :max_redirects, @max_redirects)
redirect(env, next, max)
end
defp redirect(env, next, left) when left == 0 do
case Tesla.run(env, next) do
%{status: status} = env when not status in @redirect_statuses ->
env
_ ->
raise Tesla.Error, "too many redirects"
end
end
defp redirect(env, next, left) do
case Tesla.run(env, next) do
%{status: status, headers: %{"location" => location}} when status in @redirect_statuses ->
location = parse_location(location, env)
redirect(%{env | url: location}, next, left - 1)
env ->
env
end
end
- defp parse_location("/" <> rest = location, env) do
- if String.ends_with?(env.url, "/") do
- env.url <> rest
- else
- env.url <> location
- end
+ defp parse_location("/" <> _rest = location, env) do
+ env.url
+ |> URI.parse
+ |> URI.merge(location)
+ |> URI.to_string
end
+
defp parse_location(location, _env), do: location
end
diff --git a/test/tesla/middleware/follow_redirects_test.exs b/test/tesla/middleware/follow_redirects_test.exs
index 8315200..335de9e 100644
--- a/test/tesla/middleware/follow_redirects_test.exs
+++ b/test/tesla/middleware/follow_redirects_test.exs
@@ -1,90 +1,96 @@
defmodule FollowRedirectsTest do
use ExUnit.Case
use Tesla.Middleware.TestCase, middleware: Tesla.Middleware.FollowRedirects
defmodule Client do
use Tesla
plug Tesla.Middleware.FollowRedirects
adapter fn (env) ->
{status, headers, body} = case env.url do
"http://example.com/0" ->
{200, %{'Content-Type' => 'text/plain'}, "foo bar"}
"http://example.com/" <> n ->
next = String.to_integer(n) - 1
{301, %{'Location' => 'http://example.com/#{next}'}, ""}
end
%{env | status: status, headers: headers, body: body}
end
end
test "redirects if default max redirects isn't exceeded" do
assert Client.get("http://example.com/5").status == 200
end
test "raise error when redirect default max redirects is exceeded" do
assert_raise(Tesla.Error, "too many redirects", fn-> Client.get("http://example.com/6") end)
end
defmodule CustomMaxRedirectsClient do
use Tesla
plug Tesla.Middleware.FollowRedirects, max_redirects: 1
adapter fn (env) ->
{status, headers, body} = case env.url do
"http://example.com/0" ->
{200, %{'Content-Type' => 'text/plain'}, "foo bar"}
"http://example.com/" <> n ->
next = String.to_integer(n) - 1
{301, %{'Location' => 'http://example.com/#{next}'}, ""}
end
%{env | status: status, headers: headers, body: body}
end
end
alias CustomMaxRedirectsClient, as: CMRClient
test "redirects if custom max redirects isn't exceeded" do
assert CMRClient.get("http://example.com/1").status == 200
end
test "raise error when custom max redirects is exceeded" do
assert_raise(Tesla.Error, "too many redirects", fn-> CMRClient.get("http://example.com/2") end)
end
defmodule RelativeLocationClient do
use Tesla
plug Tesla.Middleware.FollowRedirects
adapter fn (env) ->
{status, headers, body} = case env.url do
"https://example.com/pl" ->
{200, %{'Content-Type' => 'text/plain'}, "foo bar"}
"http://example.com" ->
{301, %{'Location' => 'https://example.com'}, ""}
"https://example.com" ->
{301, %{'Location' => '/pl'}, ""}
"https://example.com/" ->
{301, %{'Location' => '/pl'}, ""}
+ "https://example.com/article" ->
+ {301, %{'Location' => '/pl'}, ""}
end
%{env | status: status, headers: headers, body: body}
end
end
alias RelativeLocationClient, as: RLClient
test "supports relative address in location header" do
assert RLClient.get("http://example.com").status == 200
end
test "doesn't create double slashes inside new url" do
assert RLClient.get("https://example.com/").url == "https://example.com/pl"
end
+
+ test "rewrites URLs to their root" do
+ assert RLClient.get("https://example.com/article").url == "https://example.com/pl"
+ end
end
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Tue, Jan 21, 1:07 AM (1 d, 17 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
55629
Default Alt Text
(4 KB)
Attached To
Mode
R28 tesla
Attached
Detach File
Event Timeline
Log In to Comment