-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add integration test against mocked HTTP client.
This is overkill for now, but will become necessary to prove we've got everything wired up properly to forward trace context in HTTP headers.
- Loading branch information
Showing
10 changed files
with
108 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
use Mix.Config | ||
|
||
config :plug_gateway, | ||
api_endpoint: "http://api.example.com/api", | ||
auth_token: "BACKEND_AUTH_TOKEN", | ||
http_module: PlugGateway.HTTPMock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,12 @@ | ||
defmodule PlugGateway.BackendClient do | ||
@moduledoc """ | ||
PlugGateway back end client. | ||
""" | ||
|
||
def get(url, opts \\ []) do | ||
auth_token = PlugGateway.Config.get().auth_token | ||
headers = [{"authorization", "Bearer #{auth_token}"}] | ||
|
||
url | ||
|> HTTPoison.get(headers) | ||
|> case do | ||
{:ok, %HTTPoison.Response{status_code: status_code, body: body}} -> {:ok, status_code, body} | ||
{:error, reason} -> {:error, reason} | ||
end | ||
def get(path) do | ||
config = PlugGateway.Config.get() | ||
url = config.api_endpoint <> path | ||
headers = [{"authorization", "Bearer #{config.auth_token}"}] | ||
config.http_module.get(url, headers) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
defmodule PlugGateway.HTTP.API do | ||
@moduledoc """ | ||
PlugGateway API for speaking to HTTP back ends. | ||
""" | ||
|
||
@doc "Get a URL." | ||
@callback get( | ||
url :: String.t(), | ||
headers: [{String.t(), iodata()}] | ||
) :: {:ok, integer(), String.t()} | {:error, term()} | ||
end | ||
|
||
defmodule PlugGateway.HTTP.API.Poisonous do | ||
@moduledoc """ | ||
PlugGateway implementation for speaking to HTTP back ends. | ||
Adapts `c:PlugGateway.HTTP.API.get/1` to `HTTPoison.get/2`. | ||
""" | ||
|
||
@behaviour PlugGateway.HTTP.API | ||
|
||
@impl true | ||
def get(url, headers) do | ||
case HTTPoison.get(url, headers) do | ||
{:ok, %HTTPoison.Response{status_code: status_code, body: body}} -> {:ok, status_code, body} | ||
{:error, reason} -> {:error, reason} | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
ExUnit.start() | ||
Mox.defmock(PlugGateway.HTTPMock, for: PlugGateway.HTTP.API) | ||
ExUnit.start(capture_log: true, timeout: 10_000, exclude: [:skip]) |