Last active
April 10, 2023 08:15
-
-
Save gamache/5663a361625c03c6b8be to your computer and use it in GitHub Desktop.
Phoenix.ConnTest appears not to pass request body to controller
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# router.ex | |
defmodule Myapp.Router do | |
use Myapp.Web, :router | |
pipeline :api do | |
plug :accepts, ["json"] | |
end | |
scope "/", Myapp do | |
pipe_through :api | |
post "/", PageController, :post | |
end | |
end | |
# page_controller.ex | |
defmodule Myapp.PageController do | |
use Myapp.Web, :controller | |
def post(conn, params) do | |
{:ok, body, conn} = conn |> Plug.Conn.read_body | |
conn |> json(%{"received_body": body}) | |
end | |
end | |
# page_controller_test.exs | |
defmodule Myapp.PageControllerTest do | |
use Myapp.ConnCase | |
@endpoint Myapp.Endpoint | |
test "POST /" do | |
body_obj = %{"ok" => true} | |
resp = conn | |
|> put_req_header("content-type", "application/json") | |
|> post("/", Poison.encode!(body_obj)) | |
%{"received_body" => received_obj} = resp.resp_body |> Poison.decode! | |
assert(body_obj == received_obj) # this test fails because received_obj == "" | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Pro