diff --git a/lib/items_api/router.ex b/lib/items_api/router.ex index de8c571..b6cfd91 100644 --- a/lib/items_api/router.ex +++ b/lib/items_api/router.ex @@ -76,10 +76,7 @@ defmodule ItemsApi.Router do item -> {:ok, _} = ItemsApi.Repo.delete(item) - - conn - |> Plug.Conn.put_resp_header("content-type", "application/json") - |> Plug.Conn.send_resp(204, "") + send_response(conn, 204, "") end _ -> @@ -98,4 +95,10 @@ defmodule ItemsApi.Router do |> Plug.Conn.put_resp_header("content-type", "application/json") |> Plug.Conn.send_resp(status, json) end + + defp send_response(conn, status, body) do + conn + |> Plug.Conn.put_resp_header("content-type", "application/json") + |> Plug.Conn.send_resp(status, body) + end end diff --git a/test/items_api_test.exs b/test/items_api_test.exs index ca5f8b5..264e692 100644 --- a/test/items_api_test.exs +++ b/test/items_api_test.exs @@ -169,6 +169,23 @@ defmodule ItemsApi.RouterTest do not_found = json_conn(:get, "/items/99999") |> call() assert Plug.Conn.get_resp_header(not_found, "content-type") == ["application/json"] end + + test "DELETE responses include content-type header" do + {:ok, item} = + ItemsApi.Repo.insert( + ItemsApi.Item.changeset(%ItemsApi.Item{}, %{"name" => "Test Delete"}) + ) + + # Successful delete (204) has content-type + conn = json_conn(:delete, "/items/#{item.id}") |> call() + assert conn.status == 204 + assert Plug.Conn.get_resp_header(conn, "content-type") == ["application/json"] + + # Not found (404) has content-type + conn = json_conn(:delete, "/items/99999") |> call() + assert conn.status == 404 + assert Plug.Conn.get_resp_header(conn, "content-type") == ["application/json"] + end end describe "unknown routes" do