elixirphoenix-frameworkplug

How to populate request parameters into a Plug.Conn connection?


I'm trying to test a method that receives a connection of type Plug.Conn but I don't find a way of initializing the connection with the request parameters with the Plug.Conn API.

E.g:

test "put request params", %{conn: conn} do
  # put %{"foo" => "bar"} into the connection params

  assert conn.params == %{"foo" => "bar"}
end

Is there any way to set those parameters in the connection?


Solution

  • Unless you're doing something special in your MyAppWeb.ConnCase setup that you want to use here, the easiest way would be building a new conn with Phoenix.ConnTest.build_conn/3 (or Plug.Test.conn/3 if you aren't using Phoenix):

    test "put request params" do
      conn = build_conn(:get, "/", %{"foo" => "bar"})
    
      assert conn.params == %{"foo" => "bar"}
    end