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

Add options for configuring redirect handling #84

Merged
merged 2 commits into from
Nov 8, 2015
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
6 changes: 6 additions & 0 deletions lib/httpoison/base.ex
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ 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
* `:max_redirect` - an integer denoting the maximum number of redirects to follow

Timeouts can be an integer or `:infinity`

Expand Down Expand Up @@ -365,6 +367,8 @@ 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
max_redirect = Keyword.get options, :max_redirect

hn_options = Keyword.get options, :hackney, []

Expand All @@ -373,6 +377,8 @@ 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 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]
Expand Down
26 changes: 26 additions & 0 deletions test/httpoison_base_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,30 @@ 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

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
10 changes: 4 additions & 6 deletions test/httpoison_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down