Plug.Conn, When testing a route, conn give me nil resp_body

Hi, I am starting with Plug.Conn and Tests.

Just for learning, my app have the next route:

  post "/ping" do
      {:ok, str} = Jason.encode(conn.body_params)
      send_resp(conn|> put_resp_content_type("application/json"), 200,str)
  end

Then give a try with curl:

curl -X POST http://localhost:5000/ping -d ‘{“key”: “value”}’ -H “content-type: application/json”

and has spected, receive the same json.

Now want to use Test:


  test "ping json" do
    json =%{ username: "demonds",password: "supersecret"}
    conn=conn(:post, "http://localhost:5000/ping",json)|> put_req_header("content-type", "application/json")
    
    IO.inspect(conn.resp_body, label: "resp_body:")
    IO.inspect(conn, label: "conn:")
  end

Execute: mix test

But the conn.resp_body is nil and also the conn.status is nil and not 200?
Sound like the post is not executed.

What command is missing ?

Your test knows nothing about the router.
According to README, you should call

MyAppRouter.call(conn, MyAppRouter.init([]))

Thanks, I was reading:

https://hexdocs.pm/plug/Plug.Test.html#conn/3-examples

And it say nothing about calling the route.

That’s just a reference for a single function, so it won’t tell you anything about testing.

Testing guide for plug is on the readme