1
Plug.Test.conn
を使用すると、リクエストボディの設定が機能していないようです。 Plug.Test
を使用してElixir PlugテストでPOSTリクエストのボディをどのように設定しますか?
$ curl -X POST -d foo=bar http://localhost:4000/
body: foo=bar
:curl
を使用して
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
:
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のようにcontent-typeヘッダーを設定しようとしました。 conn
に渡す前に、バイナリに体を変換する