Search code examples
elixirphoenix-frameworkex-unit

How can I specify format in the Phoenix.ConnTest?


I have a pretty simple code in the phoenix controller. It does some stuff and returns content depending on the format:

def delete(conn, _params) do
  # some stuff here

  if get_format(conn) == "json" do
    conn |> put_status(200) |> json(%{})
  else
    conn |> redirect(to: "/")
  end
end

It works properly, but I have a problem with testing it. I can't test html return. How can I do it? dispatch/5 doesn't have anything related to format.


Solution

  • Format is defined via accept header for connection, not for get or whatever. For json & html formats it should be application/json or html/text, respectively.

    You can use this conn in your tests:

    conn = build_conn
      |> Plug.Conn.put_req_header("accept", "text/html")