1
votes

In my tests, I'm hitting the router with json for each route, and only json is accepted by Plug.parser. This works fine, except for tests using the delete method, which always fail with Unsupported content type: multipart/mixed. I'm sending an empty body with the delete request with my application/json as content-type in the headers, but I think the _method parameter is causing it to be rejected as wrong content type- although this does not happen with put method which should require _method to be added to request body also.

test "inactivate" do
  {id, token} = register
  response = Myapp.Router.call(conn(:delete, "/api/manager/tenants/" <> id, 
    [], headers: [{"content-type", "application/json"}, {"token", token}] ) |> Plug.Conn.fetch_params(), @opts)
  assert response.status == 200
end

when hitting the same delete routes with httprequester, they work fine and are not stopped by plug parser. Does Router.call handle http requests differently in tests?

1
Phoenix now ships with a MyApp.ConnCase. You should use that when running Controller tests. It provides conveniences for testing Controllers.Gjaldon
You are not sending an empty body. An empty list represents empty parameters. You can try setting it to an empty string "" but that will likely fail because it is not valid JSON. Or send "{}" or do not send anything (nil).José Valim
This worked. I had to send nil as the body.Dania_es

1 Answers

1
votes

Phoenix now ships with a MyApp.ConnCase. You should use that when running Controller tests. It provides conveniences for testing Controllers. It goes through the plug stack in both Endpoint and Router for you every time you call a http method like get conn(), "/".

In your case, a delete request using MyApp.ConnCase would be like:

conn = conn()
  |> put_req_header("content-type", "application/json")
  |> put_req_header("token", token)
  |> delete("/api/manager/tenants/" <> id, params)

assert response(conn, 200)

Let me know if that helps.