1
使用Plug.Test.conn
,設置請求主體似乎不起作用。如何在Elixir Plug測試中設置POST請求的主體?
這裏的插頭:
defmodule MyPlug do
import Plug.Conn
def init(_), do: nil
def call(conn, _) do
{:ok, body, _conn} = read_body(conn)
send_resp(conn, 200, "body: #{body}")
end
end
使用curl
:
$ curl -X POST -d foo=bar http://localhost:4000/
body: foo=bar
使用Plug.Test
:
defmodule MyTest do
use ExUnit.Case, async: false
use Plug.Test
test "POST request" do
conn = conn(:post, "/", %{foo: "bar"})
|> MyPlug.call(%{})
assert conn.resp_body == "body: foo=bar"
end
end
失敗:
1) test POST request (MyPlugTest)
test/my_plug_test.exs:28
Assertion with == failed
code: conn.resp_body() == "body: foo=bar"
left: "body: "
right: "body: foo=bar"
我也試過傳遞一個字符串並按照docs設置內容類型標題。