** (RuntimeError) no content-type was set, expected a json response

I have a auth error method like so.

 def auth_error(conn, {type, _reason}, _opts) do
    body = Poison.encode!(%{error: to_string(type)})
    send_resp(conn, 401, body)
  end

and I have a test like so

  conn = get(conn, user_path(conn, :show))
  assert json_response(conn, 401)["error"] == "unauthenticated"

And I get an error like so when I try and run my test.

** (RuntimeError) no content-type was set, expected a json response
     code: assert json_response(conn, 401)["error"] == "unauthenticated"

What am I missing?

The json_response function checks if the Content-Type is set to application/json and send_resp by itself doesn’t set it.

You might have to do:

conn
|> put_resp_content_type("application/json")
|> send_resp(401,  body)
2 Likes

I had tried something similar to that but it had not worked. It works now, Thank you!