How do you set the body of a POST request in an Elixir Plug test?

Viewed 2016

Using Plug.Test.conn, setting the request body doesn't seem to work.

Here's the plug:

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

Using curl:

$ curl -X POST -d foo=bar http://localhost:4000/
body: foo=bar

Using 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

Failure:

  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"

I've also tried passing a string and setting content-type header as per docs.

1 Answers
Related