简体   繁体   English

如何将请求参数填充到 Plug.Conn 连接中?

[英]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.我正在尝试测试接收类型为Plug.Conn的连接的方法,但我没有找到使用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):除非你在MyAppWeb.ConnCase设置中做一些特别的MyAppWeb.ConnCase ,你想在这里使用,最简单的方法是使用Phoenix.ConnTest.build_conn/3 (或者Plug.Test.conn/3如果你不是) t 使用凤凰):

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Phoenix:如何在控制台中获得conn%Plug.Conn {} - Phoenix: How to get conn %Plug.Conn{} in the console 来自 %Plug.Conn{} 远程 IP 的 Phoenix 解析请求主机 - Phoenix Resolve Request Host from %Plug.Conn{} remote ip **(RuntimeError)预期的操作/ 2返回一个Plug.Conn,所有插件都必须接收一个连接(conn)并返回一个连接 - ** (RuntimeError) expected action/2 to return a Plug.Conn, all plugs must receive a connection (conn) and return a connection (RuntimeError)期望动作/ 2返回一个Plug.Conn,所有插件必须接收连接(conn)并返回连接 - (RuntimeError) expected action/2 to return a Plug.Conn, all plugs must receive a connection (conn) and return a connection 如何使用Plug.Conn读取phoenix控制器中的小块数据 - How to read small chunks of data in phoenix controller, using Plug.Conn expected:action / 2返回Phoenix控制器中的Plug.Conn错误 - expected :action/2 to return a Plug.Conn error in Phoenix controller 从Plug.Conn获取HTTP_REFERRER - Get HTTP_REFERRER from Plug.Conn Phoenix Framework的Plug.Conn中assign和put_session有什么区别? - What is the difference between assign and put_session in Plug.Conn of the Phoenix Framework? 谁能确切解释Plug.Conn中put_private的含义? - Who can explain exactly the meaning of put_private in Plug.Conn? Elixir从控制器获取错误:预期动作/ 2返回Plug.Conn - Elixir getting error from controller: Expected action/2 to return a Plug.Conn
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM