Conn case how to setup tags custom request header

Hello,
Is there a way that I can set global-wide request header inside conn_case.ex?
I would like to set the authorization header for all the tests since setting it in every file is a bit annoying.

This does not work:

# test/support/conn_case.ex
setup tags do
    pid = Ecto.Adapters.SQL.Sandbox.start_owner!(MyApp.Repo, shared: not tags[:async])
    on_exit(fn -> Ecto.Adapters.SQL.Sandbox.stop_owner(pid) end)
    {:ok, conn: Phoenix.ConnTest.build_conn()}

    {:ok,
     conn:
       Phoenix.ConnTest.put_req_header(
         "authorization",
         "Bearer " <> Application.get_env(:myapp, MyAppWeb.Endpoint)[:api_key]
       )}
  end

You need to pass the conn to put_req_header:

conn =
  Phoenix.ConnTest.build_conn()
  |> Phoenix.ConnTest.put_req_header("authorization", "...")

{:ok, conn: conn}

Personally, I do this in the test code with a helper. This way I can test the unauthorized requests too.

1 Like

It is actually Plug.Conn.put_req_header, but

Thanks a lot!!