Skip to content

Commit

Permalink
Automatic Host header in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
voltone committed Aug 31, 2020
1 parent eb55ca4 commit 207fd9e
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions lib/plug_signature/conn_test.ex
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ defmodule PlugSignature.ConnTest do
parameter in the Authorization header
"""
def with_signature(conn, key, key_id, opts \\ []) do
conn = maybe_add_host_header(conn)

request_target =
Keyword.get_lazy(opts, :request_target, fn ->
method = conn.method |> to_string |> String.downcase()
Expand Down Expand Up @@ -194,6 +196,18 @@ defmodule PlugSignature.ConnTest do
to_string(now + validity)
end

defp maybe_add_host_header(%Plug.Conn{host: host} = conn) when is_binary(host) do
case get_req_header(conn, "host") do
[] ->
put_req_header(conn, "host", host)

_ ->
conn
end
end

defp maybe_add_host_header(conn), do: conn

defp raise_on_missing_phoenix_conntest! do
Code.ensure_loaded?(Phoenix.ConnTest) ||
raise "endpoint testing requirs Phoenix.ConnTest"
Expand Down

0 comments on commit 207fd9e

Please sign in to comment.