From 877d46e562df2c198c75acf0d5239d5baef7555a Mon Sep 17 00:00:00 2001 From: Shane Wilton Date: Thu, 15 Oct 2015 15:38:45 -0700 Subject: [PATCH 1/2] Add an option to enable following redirects --- lib/httpoison/base.ex | 3 +++ test/httpoison_base_test.exs | 13 +++++++++++++ test/httpoison_test.exs | 10 ++++------ 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/lib/httpoison/base.ex b/lib/httpoison/base.ex index efa9861..6bc7dad 100644 --- a/lib/httpoison/base.ex +++ b/lib/httpoison/base.ex @@ -131,6 +131,7 @@ defmodule HTTPoison.Base do or a `{Host, Proxy}` tuple * `:proxy_auth` - proxy authentication `{User, Password}` tuple * `:ssl` - SSL options supported by the `ssl` erlang module + * `:follow_redirect` - a boolean that causes redirects to be followed Timeouts can be an integer or `:infinity` @@ -365,6 +366,7 @@ defmodule HTTPoison.Base do proxy = Keyword.get options, :proxy proxy_auth = Keyword.get options, :proxy_auth ssl = Keyword.get options, :ssl + follow_redirect = Keyword.get options, :follow_redirect hn_options = Keyword.get options, :hackney, [] @@ -373,6 +375,7 @@ defmodule HTTPoison.Base do if proxy, do: hn_options = [{:proxy, proxy} | hn_options] if proxy_auth, do: hn_options = [{:proxy_auth, proxy_auth} | hn_options] if ssl, do: hn_options = [{:ssl_options, ssl} | hn_options] + if follow_redirect, do: hn_options = [{:follow_redirect, follow_redirect} | hn_options] if stream_to do hn_options = [:async, {:stream_to, spawn(module, :transformer, [stream_to])} | hn_options] diff --git a/test/httpoison_base_test.exs b/test/httpoison_base_test.exs index 928d1ba..4dfbc60 100644 --- a/test/httpoison_base_test.exs +++ b/test/httpoison_base_test.exs @@ -131,4 +131,17 @@ defmodule HTTPoisonBaseTest do assert validate :hackney end + + test "passing follow_redirect option" do + expect(:hackney, :request, [{[:post, "http://localhost", [], "body", [follow_redirect: true]], + {:ok, 200, "headers", :client}}]) + expect(:hackney, :body, 1, {:ok, "response"}) + + assert HTTPoison.post!("localhost", "body", [], follow_redirect: true) == + %HTTPoison.Response{ status_code: 200, + headers: "headers", + body: "response" } + + assert validate :hackney + end end diff --git a/test/httpoison_test.exs b/test/httpoison_test.exs index 7348bef..79f647e 100644 --- a/test/httpoison_test.exs +++ b/test/httpoison_test.exs @@ -64,14 +64,12 @@ defmodule HTTPoisonTest do end end - test "hackney option follow redirect absolute url" do - hackney = [follow_redirect: true] - assert_response HTTPoison.get("http://localhost:8080/redirect-to?url=http%3A%2F%2Flocalhost:8080%2Fget", [], [ hackney: hackney ]) + test "option follow redirect absolute url" do + assert_response HTTPoison.get("http://localhost:8080/redirect-to?url=http%3A%2F%2Flocalhost:8080%2Fget", [], [follow_redirect: true]) end - test "hackney option follow redirect relative url" do - hackney = [follow_redirect: true] - assert_response HTTPoison.get("http://localhost:8080/relative-redirect/1", [], [ hackney: hackney ]) + test "option follow redirect relative url" do + assert_response HTTPoison.get("http://localhost:8080/relative-redirect/1", [], [follow_redirect: true]) end test "basic_auth hackney option" do From 3bbfa6c739e97bb6badeae3c9147b58d28dfb175 Mon Sep 17 00:00:00 2001 From: Shane Wilton Date: Thu, 15 Oct 2015 15:42:14 -0700 Subject: [PATCH 2/2] Add an option for configuring the max number of redirects to follow --- lib/httpoison/base.ex | 3 +++ test/httpoison_base_test.exs | 13 +++++++++++++ 2 files changed, 16 insertions(+) diff --git a/lib/httpoison/base.ex b/lib/httpoison/base.ex index 6bc7dad..2f13864 100644 --- a/lib/httpoison/base.ex +++ b/lib/httpoison/base.ex @@ -132,6 +132,7 @@ defmodule HTTPoison.Base do * `:proxy_auth` - proxy authentication `{User, Password}` tuple * `:ssl` - SSL options supported by the `ssl` erlang module * `:follow_redirect` - a boolean that causes redirects to be followed + * `:max_redirect` - an integer denoting the maximum number of redirects to follow Timeouts can be an integer or `:infinity` @@ -367,6 +368,7 @@ defmodule HTTPoison.Base do proxy_auth = Keyword.get options, :proxy_auth ssl = Keyword.get options, :ssl follow_redirect = Keyword.get options, :follow_redirect + max_redirect = Keyword.get options, :max_redirect hn_options = Keyword.get options, :hackney, [] @@ -376,6 +378,7 @@ defmodule HTTPoison.Base do if proxy_auth, do: hn_options = [{:proxy_auth, proxy_auth} | hn_options] if ssl, do: hn_options = [{:ssl_options, ssl} | hn_options] if follow_redirect, do: hn_options = [{:follow_redirect, follow_redirect} | hn_options] + if max_redirect, do: hn_options = [{:max_redirect, max_redirect} | hn_options] if stream_to do hn_options = [:async, {:stream_to, spawn(module, :transformer, [stream_to])} | hn_options] diff --git a/test/httpoison_base_test.exs b/test/httpoison_base_test.exs index 4dfbc60..150eca9 100644 --- a/test/httpoison_base_test.exs +++ b/test/httpoison_base_test.exs @@ -144,4 +144,17 @@ defmodule HTTPoisonBaseTest do assert validate :hackney end + + test "passing max_redirect option" do + expect(:hackney, :request, [{[:post, "http://localhost", [], "body", [max_redirect: 2]], + {:ok, 200, "headers", :client}}]) + expect(:hackney, :body, 1, {:ok, "response"}) + + assert HTTPoison.post!("localhost", "body", [], max_redirect: 2) == + %HTTPoison.Response{ status_code: 200, + headers: "headers", + body: "response" } + + assert validate :hackney + end end