I’m trying to call the Phoenix (default generated) ErrorView from a plug like this:
defp basic_verify(conn, credentials) do
with {:ok, decoded} <- Base.decode64(credentials),
[username, password] <- String.split(decoded, ":", parts: 2),
{:ok, user} <- Hexer.Auth.login(username, password, allow_unverified: true) do
put_user(conn, user)
else
_ ->
conn
|> put_resp_header("www-authenticate", "Basic realm=\"API keys\"")
|> put_status(401)
|> Phoenix.Controller.put_view(HexerWeb.ErrorView)
|> Phoenix.Controller.render(:"401")
|> halt()
end
end
And when testing this in the browser everything works great.
However, in my unit tests when I’m testing the plug itself using the this code:
test "sets WWW-Authenticate header on invalid username/password", %{
conn: conn,
invalid_credentials: credentials
} do
conn =
conn
|> put_req_header("authorization", "Basic #{credentials}")
|> BasicAuth.basic_auth([])
%{halted: halted} = conn
assert halted
assert "Basic realm=\"API keys\"" in get_resp_header(conn, "www-authenticate")
end
I get the following error:
I’ve fiddled around with bypass_through
but I can’t seem to get rid of the error.