Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recognize lowercase location header (fix #453) #454

Merged
merged 1 commit into from
Mar 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we could use :hackney_headers.get_value given that we already have hackney as a dependency?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had a look and gave it a try but it fails because that function (and :hackney_headers_new.get_value as well) expects the headers to be an Erlang dict while here it's a proplist (still haven't investigated why).

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh right! I didn't notice that! As you were then 😬

}}
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