This repository was archived by the owner on Jul 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
feat: implement custom PLug to forward headers using a Absinthe.Plug #5
Open
frfroes
wants to merge
3
commits into
SpiffInc:main
Choose a base branch
from
frfroes:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,28 @@ | ||
| defmodule Absinthe.Compose.Plug.ForwardHeader do | ||
| @moduledoc """ | ||
| This Plug takes the given headers and add it to Absinthe.Plug context in order to be fowarded | ||
| on to the Upstream service on the resolver level. | ||
| """ | ||
| @behaviour Plug | ||
|
|
||
| import Plug.Conn | ||
|
|
||
| def init(opts), do: opts | ||
|
|
||
| @spec call(Plug.Conn.t(), any) :: Plug.Conn.t() | ||
| def call(conn, opts) do | ||
| headers_to_forward = get_headers(conn, opts) | ||
| Absinthe.Plug.put_options(conn, context: %{headers_to_forward: headers_to_forward}) | ||
| end | ||
|
|
||
| defp get_headers(conn, header_names) do | ||
| Enum.reduce(header_names, [], fn name, acc -> | ||
| name = String.downcase(name) | ||
|
|
||
| case get_req_header(conn, name) do | ||
| [] -> acc | ||
| values -> [{name, values} | acc] | ||
| end | ||
| end) | ||
| end | ||
| end | ||
This file contains hidden or 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 hidden or 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,36 @@ | ||
| defmodule Absinthe.Compose.Plug.ForwardHeaderTest do | ||
| use ExUnit.Case, async: true | ||
| use Plug.Test | ||
|
|
||
| alias Absinthe.Compose.Plug.ForwardHeader | ||
|
|
||
| setup do | ||
| ForwardHeader.init(["Auhtorization", "origin"]) | ||
|
|
||
| conn = | ||
| conn("post", "/", "") | ||
| |> put_req_header("auhtorization", "some-token") | ||
|
frfroes marked this conversation as resolved.
Outdated
|
||
| |> put_req_header("origin", "www.coolwebsite.com") | ||
| |> put_req_header("header-to-ignore", "ignored") | ||
|
|
||
| %{ | ||
| conn: conn | ||
| } | ||
|
frfroes marked this conversation as resolved.
|
||
| end | ||
|
|
||
| test "fowards given headers to abshinte context", %{conn: conn} do | ||
| conn = ForwardHeader.call(conn, ["auhtorization", "Origin"]) | ||
|
frfroes marked this conversation as resolved.
Outdated
|
||
| assert %{context: %{headers_to_forward: headers_to_forward}} = conn.private[:absinthe] | ||
|
|
||
| assert [{"origin", ["www.coolwebsite.com"]}, {"auhtorization", ["some-token"]}] = | ||
|
frfroes marked this conversation as resolved.
Outdated
|
||
| headers_to_forward | ||
|
|
||
| assert length(headers_to_forward) == 2 | ||
| end | ||
|
|
||
| test "ignores non-existent headers", %{conn: conn} do | ||
| conn = ForwardHeader.call(conn, ["auhtorization", "origin", "some-giberish"]) | ||
|
frfroes marked this conversation as resolved.
Outdated
|
||
| assert %{context: %{headers_to_forward: headers_to_forward}} = conn.private[:absinthe] | ||
| assert length(headers_to_forward) == 2 | ||
| end | ||
| end | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we proceed with this approach, I think it would be nice if we add how users of this library would plug this plug on their application. It could be here though, something like this:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Btw, I think would be better if we specify a key for the headers name, something like:
plug Absinthe.Compose.Plug.ForwardHeader, headers: ["authorization"]My main concern is if this plug starts to receive new arguments in the future (I don't have an example of that). If that happens, a new release of this library would result in a breaking change, requiring all users of this library to update their code before proceeding with the update.