Skip to content

Commit

Permalink
feat(connection): ability to set HTTPoison connection options (#12)
Browse files Browse the repository at this point in the history
Allows calling `Config.set(:connection_opts)` to configure the underlying `HTTPoison` calls (e.g.: to use a timeout value)
  • Loading branch information
naps62 authored and uesteibar committed May 14, 2018
1 parent 4a86e5b commit c940965
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 11 deletions.
12 changes: 12 additions & 0 deletions lib/neuron/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ defmodule Neuron.Config do
def set(:global, nil) do
Application.delete_env(:neuron, :neuron_url)
Application.delete_env(:neuron, :neuron_headers)
Application.delete_env(:neuron, :neuron_connection_opts)
end

def set(:global, url: value) do
Expand All @@ -40,6 +41,15 @@ defmodule Neuron.Config do
:ok
end

def set(:global, connection_opts: value) do
Application.put_env(:neuron, :neuron_connection_opts, value)
end

def set(:process, connection_opts: value) do
Process.put(:neuron_connection_opts, value)
:ok
end

@doc """
gets url configuration value for Neuron
Expand All @@ -57,6 +67,7 @@ defmodule Neuron.Config do
"""
def get(:headers), do: get(:neuron_headers)
def get(:url), do: get(:neuron_url)
def get(:connection_opts), do: get(:neuron_connection_opts)

def get(key) do
key
Expand All @@ -70,6 +81,7 @@ defmodule Neuron.Config do

def current_context(:headers), do: current_context(:neuron_headers)
def current_context(:url), do: current_context(:neuron_url)
def current_context(:connection_opts), do: current_context(:neuron_connection_opts)

def current_context(key) do
if Process.get(key, nil), do: :process, else: :global
Expand Down
7 changes: 6 additions & 1 deletion lib/neuron/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ defmodule Neuron.Connection do
HTTPoison.post(
url,
query,
build_headers()
build_headers(),
connection_opts()
)
end

Expand All @@ -22,4 +23,8 @@ defmodule Neuron.Connection do
defp headers() do
Config.get(:headers) || []
end

defp connection_opts() do
Config.get(:connection_opts) || []
end
end
2 changes: 2 additions & 0 deletions test/neuron/config_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@ defmodule Neuron.ConfigTest do
test "Can set multiple settings" do
Config.set(url: "testurl")
Config.set(headers: [name: "val"])
Config.set(connection_opts: [foo: "bar"])

assert Config.get(:url) == "testurl"
assert Config.get(:headers) == [name: "val"]
assert Config.get(:connection_opts) == [foo: "bar"]
end
end
end
42 changes: 32 additions & 10 deletions test/neuron/connection_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,28 @@ defmodule Neuron.ConnectionTest do

test "sets content-type as default header", %{url: url, query: query} do
with_mock HTTPoison,
post: fn _url, _query, _headers ->
post: fn _url, _query, _headers, _opts ->
%HTTPoison.Response{}
end do
Connection.post(url, query)
assert called(HTTPoison.post(url, query, "Content-Type": "application/graphql"))
assert called(HTTPoison.post(url, query, ["Content-Type": "application/graphql"], []))
end
end

test "overwrites content type if supplied", %{url: url, query: query} do
with_mock HTTPoison,
post: fn _url, _query, _headers ->
post: fn _url, _query, _headers, _opts ->
%HTTPoison.Response{}
end do
Neuron.Config.set(headers: ["Content-Type": "application/json"])
Connection.post(url, query)
assert called(HTTPoison.post(url, query, "Content-Type": "application/json"))
assert called(HTTPoison.post(url, query, ["Content-Type": "application/json"], []))
end
end

test "with basic auth", %{url: url, query: query} do
with_mock HTTPoison,
post: fn _url, _query, _headers ->
post: fn _url, _query, _headers, _opts ->
%HTTPoison.Response{}
end do
Neuron.Config.set(headers: [hackney: [basic_auth: [user: "password"]]])
Expand All @@ -44,16 +44,19 @@ defmodule Neuron.ConnectionTest do
HTTPoison.post(
url,
query,
"Content-Type": "application/graphql",
hackney: [basic_auth: [user: "password"]]
[
"Content-Type": "application/graphql",
hackney: [basic_auth: [user: "password"]]
],
[]
)
)
end
end

test "with custom headers", %{url: url, query: query} do
with_mock HTTPoison,
post: fn _url, _query, _headers ->
post: fn _url, _query, _headers, _opts ->
%HTTPoison.Response{}
end do
Neuron.Config.set(headers: ["X-CUSTOM": "value"])
Expand All @@ -63,8 +66,27 @@ defmodule Neuron.ConnectionTest do
HTTPoison.post(
url,
query,
"Content-Type": "application/graphql",
"X-CUSTOM": "value"
["Content-Type": "application/graphql", "X-CUSTOM": "value"],
[]
)
)
end
end

test "with custom connection options", %{url: url, query: query} do
with_mock HTTPoison,
post: fn _url, _query, _headers, _opts ->
%HTTPoison.Response{}
end do
Neuron.Config.set(connection_opts: [timeout: 50_000])
Connection.post(url, query)

assert called(
HTTPoison.post(
url,
query,
["Content-Type": "application/graphql"],
timeout: 50_000
)
)
end
Expand Down

0 comments on commit c940965

Please sign in to comment.