简体   繁体   中英

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.

Eg:

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM