Simple controller index function throws Plug.MissingAdapter.send_resp/4 is undefined

I have a db with two users that are getting queried up fine, but then I get a Plug.MissingAdapter.send_resp/4 is undefined error (new one for me). here is the complete error message:

(UndefinedFunctionError) function Plug.MissingAdapter.send_resp/4 is undefined (module Plug.MissingAdapter is not available)
Plug.MissingAdapter.send_resp(nil, 200, [{"content-type", "application/json; charset=utf-8"}, {"cache-control", "max-age=0, private, must-revalidate"}], [123, [[34, ["data"], 34], 58, [91, [[123, [[34, ["username"], 34], 58, [34, ["first_user"], 34], 44, [34, ["id"], 34], 58, "1", 44, [34, ["email"], 34], 58, [34, ["first@user.com"], 34]], 125], 44, [123, [[34, ["username"], 34], 58, [34, ["second_user"], 34], 44, [34, ["id"], 34], 58, "3", 44, [34, ["email"], 34], 58, [34, ["second@user.com"], 34]], 125]], 93]], 125])
(plug) lib/plug/conn.ex:394: Plug.Conn.send_resp/1

Is this saying that I need to create a Plug.MissingAdapter module? This is my first experiment in Phoenix 1.3 but I didn’t think things had changed that much. Although that said, setting up Guardian is proving to be much more complicated than it used to be as well.

Relevant part of controller:

defmodule MyAppWeb.UserController do
  use MyAppWeb, :controller

  alias MyApp.Profiles
  alias MyApp.Profiles.User
  alias MyAppWeb.SessionView
  alias MyAppWeb.UserView

  action_fallback MyAppWeb.FallbackController

  def index(conn, _params) do
    users = Profiles.list_users()
    conn
    |> put_view(UserView)
    |> render("index.json", users: users)
  end

and from the view:

defmodule MyAppWeb.UserView do
  use MyAppWeb, :view
  alias MyAppWeb.UserView

  def render("index.json", %{users: users}) do
    %{data: render_many(users, UserView, "user.json")}
  end

Nevermind, I’m just an idiot. You need to have a valid Plug.Conn to correctly populate the adapter field (module), I was just trying to test with an empty Conn

1 Like

There is a default adapter for cowboy, and it’s missing from conn. That’s what the error says.

I was just going to ask about this, but I figured you weren’t testing since you didn’t mention it… d’oh! :smiley: You should mark as solved. :slight_smile:

One more “idiot” here! :joy: Kidding!

Thanks for publishing the solution.

Btw, if someone stumbles upon this page in the future: you shouldn’t generate raw Plug.Conn structs manually. Instead, use the conn generated in your YourProject.ConnCase module (file path should be something like test/support/conn_case.ex).

Ex.:

def YourProject.YourPlugTest do

  # the line below provides some utilities for you, including
  # the `conn` variable mentioned below
  use YourProjectWeb.ConnCase

  # descruct `conn` from the provided map
  test "does something with a conn", %{conn: conn} do
    # here you have a `conn` variable that's good to be used in tests.
    # use it whenever you're testing your plugs

    conn = YourProject.YourPlug.call(conn, [])

    # assert whatever you need here
  end
end

As a matter of suggestion: go ahead and read the whole conn_case.ex file. Reading these “magical” files is helping me a lot to understand WTF is going on in my Phoenix project.