Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions lib/items_api/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

_ ->
Expand All @@ -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
17 changes: 17 additions & 0 deletions test/items_api_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading