Skip to content

Commit

Permalink
Recognize lowercase location header (fix #453)
Browse files Browse the repository at this point in the history
  • Loading branch information
gianluca-nitti authored and edgurgel committed Mar 31, 2022
1 parent 791f166 commit 6b6ad05
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 26 deletions.
11 changes: 10 additions & 1 deletion lib/httpoison/base.ex
Original file line number Diff line number Diff line change
Expand Up @@ -952,7 +952,16 @@ defmodule HTTPoison.Base do
headers: process_response_headers.(headers),
request: request,
request_url: request.url,
redirect_url: :proplists.get_value("Location", headers, nil)
redirect_url: get_header(headers, "Location", nil)
}}
end

defp get_header(headers, key, default) do
key = String.downcase(key)

Enum.find_value(headers, default, fn
{k, v} -> if String.downcase(k) == key, do: v, else: nil
_ -> nil
end)
end
end
55 changes: 30 additions & 25 deletions test/httpoison_base_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -569,31 +569,36 @@ defmodule HTTPoisonBaseTest do
}
end

test "request returns MaybeRedirect when passing follow_redirect option" do
expect(:hackney, :request, fn :post,
"http://localhost",
[],
"body",
[follow_redirect: true] ->
# Mock a redirect from `http://localhost` to `https://localhost`
{:ok, {:maybe_redirect, 302, _headers = [{"Location", "https://localhost"}], :client}}
end)

assert HTTPoison.post!("localhost", "body", [], follow_redirect: true) ==
%HTTPoison.MaybeRedirect{
status_code: 302,
headers: [{"Location", "https://localhost"}],
request_url: "http://localhost",
request: %HTTPoison.Request{
body: "body",
headers: [],
method: :post,
options: [follow_redirect: true],
params: %{},
url: "http://localhost"
},
redirect_url: "https://localhost"
}
for loc_header <- ["Location", "location"] do
test "request returns MaybeRedirect when passing follow_redirect option and server sets #{
loc_header
} header" do
expect(:hackney, :request, fn :post,
"http://localhost",
[],
"body",
[follow_redirect: true] ->
# Mock a redirect from `http://localhost` to `https://localhost`
{:ok,
{:maybe_redirect, 302, _headers = [{unquote(loc_header), "https://localhost"}], :client}}
end)

assert HTTPoison.post!("localhost", "body", [], follow_redirect: true) ==
%HTTPoison.MaybeRedirect{
status_code: 302,
headers: [{unquote(loc_header), "https://localhost"}],
request_url: "http://localhost",
request: %HTTPoison.Request{
body: "body",
headers: [],
method: :post,
options: [follow_redirect: true],
params: %{},
url: "http://localhost"
},
redirect_url: "https://localhost"
}
end
end

test "passing max_redirect option" do
Expand Down

0 comments on commit 6b6ad05

Please sign in to comment.