Skip to content

Commit

Permalink
Merge pull request #306 from amencarini/use-http-proxy-env
Browse files Browse the repository at this point in the history
Add check for HTTP_PROXY env var
  • Loading branch information
edgurgel authored Jan 15, 2018
2 parents d7ea79d + 5ffde91 commit f5a6468
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ erl_crash.dump
mix.lock
/.idea
*.iml
/.elixir_ls
30 changes: 25 additions & 5 deletions lib/httpoison/base.ex
Original file line number Diff line number Diff line change
Expand Up @@ -401,8 +401,6 @@ defmodule HTTPoison.Base do
recv_timeout = Keyword.get options, :recv_timeout
stream_to = Keyword.get options, :stream_to
async = Keyword.get options, :async
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
Expand All @@ -411,8 +409,6 @@ defmodule HTTPoison.Base do

hn_options = if timeout, do: [{:connect_timeout, timeout} | hn_options], else: hn_options
hn_options = if recv_timeout, do: [{:recv_timeout, recv_timeout} | hn_options], else: hn_options
hn_options = if proxy, do: [{:proxy, proxy} | hn_options], else: hn_options
hn_options = if proxy_auth, do: [{:proxy_auth, proxy_auth} | hn_options], else: hn_options
hn_options = if ssl, do: [{:ssl_options, ssl} | hn_options], else: hn_options
hn_options = if follow_redirect, do: [{:follow_redirect, follow_redirect} | hn_options], else: hn_options
hn_options = if max_redirect, do: [{:max_redirect, max_redirect} | hn_options], else: hn_options
Expand All @@ -431,10 +427,34 @@ defmodule HTTPoison.Base do
hn_options
end

defp build_hackney_proxy_options(options, request_url) do
proxy =
if Keyword.has_key?(options, :proxy) do
Keyword.get(options, :proxy)
else
case URI.parse(request_url).scheme do
"http" -> System.get_env("HTTP_PROXY") || System.get_env("http_proxy")
"https" -> System.get_env("HTTPS_PROXY") || System.get_env("https_proxy")
_ -> nil
end
end

proxy_auth = Keyword.get(options, :proxy_auth)

hn_proxy_options = if proxy, do: [{:proxy, proxy}], else: []

hn_proxy_options =
if proxy_auth, do: [{:proxy_auth, proxy_auth} | hn_proxy_options], else: hn_proxy_options

hn_proxy_options
end


@doc false
@spec request(atom, atom, binary, body, headers, any, fun, fun, fun) :: {:ok, Response.t | AsyncResponse.t} | {:error, Error.t}
def request(module, method, request_url, request_body, request_headers, options, process_status_code, process_headers, process_response_body) do
hn_options = build_hackney_options(module, options)
hn_proxy_options = build_hackney_proxy_options(options, request_url)
hn_options = hn_proxy_options ++ build_hackney_options(module, options)

case do_request(method, request_url, request_headers, request_body, hn_options) do
{:ok, status_code, headers} -> response(process_status_code, process_headers, process_response_body, status_code, headers, "", request_url)
Expand Down
64 changes: 64 additions & 0 deletions test/httpoison_base_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,70 @@ defmodule HTTPoisonBaseTest do
assert validate :hackney
end

test "having http_proxy env variable set on http requests" do
expect(System, :get_env, [{["HTTP_PROXY"], "proxy"}])

expect(:hackney, :request, [
{[:post, "http://localhost", [], "body", [proxy: "proxy"]], {:ok, 200, "headers", :client}}
])

expect(:hackney, :body, 1, {:ok, "response"})

assert HTTPoison.post!("localhost", "body") ==
%HTTPoison.Response{
status_code: 200,
headers: "headers",
body: "response",
request_url: "http://localhost"
}

assert validate(:hackney)
end

test "having https_proxy env variable set on https requests" do
expect(System, :get_env, [{["HTTPS_PROXY"], "proxy"}])

expect(:hackney, :request, [
{[:post, "https://localhost", [], "body", [proxy: "proxy"]], {:ok, 200, "headers", :client}}
])

expect(:hackney, :body, 1, {:ok, "response"})

assert HTTPoison.post!("https://localhost", "body") ==
%HTTPoison.Response{
status_code: 200,
headers: "headers",
body: "response",
request_url: "https://localhost"
}

assert validate(:hackney)
end

test "having https_proxy env variable set on http requests" do
expect(System, :get_env, [
{["HTTPS_PROXY"], "proxy"},
{["HTTP_PROXY"], nil},
{["http_proxy"], nil}
])

expect(:hackney, :request, [
{[:post, "http://localhost", [], "body", []], {:ok, 200, "headers", :client}}
])

expect(:hackney, :body, 1, {:ok, "response"})

assert HTTPoison.post!("localhost", "body") ==
%HTTPoison.Response{
status_code: 200,
headers: "headers",
body: "response",
request_url: "http://localhost"
}

assert validate(:hackney)
end

test "passing ssl option" do
expect(:hackney, :request, [{[:post, "http://localhost", [], "body", [ssl_options: [certfile: "certs/client.crt"]]],
{:ok, 200, "headers", :client}}])
Expand Down

0 comments on commit f5a6468

Please sign in to comment.