diff --git a/lib/httpoison/base.ex b/lib/httpoison/base.ex index 745c8a1..9d67883 100644 --- a/lib/httpoison/base.ex +++ b/lib/httpoison/base.ex @@ -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 diff --git a/test/httpoison_base_test.exs b/test/httpoison_base_test.exs index 16e5309..a43b707 100644 --- a/test/httpoison_base_test.exs +++ b/test/httpoison_base_test.exs @@ -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