From 6f1b1aed9dc9202dcda901194be8787c4fc8ede5 Mon Sep 17 00:00:00 2001 From: Alex Banna Date: Sat, 28 Mar 2026 14:13:28 -0500 Subject: [PATCH] test(health): add dedicated health endpoint test coverage - Verify GET /health returns 200 with {"status": "ok"} - Verify Content-Type: application/json header - Test that no request body is required - Test non-GET methods return 404 - Test idempotency across multiple calls Implements: HAR-577 --- test/items_api/health_test.exs | 66 ++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 test/items_api/health_test.exs diff --git a/test/items_api/health_test.exs b/test/items_api/health_test.exs new file mode 100644 index 0000000..60bc38d --- /dev/null +++ b/test/items_api/health_test.exs @@ -0,0 +1,66 @@ +defmodule ItemsApi.HealthTest do + use ExUnit.Case, async: false + + setup do + :ok = Ecto.Adapters.SQL.Sandbox.checkout(ItemsApi.Repo) + :ok + end + + defp call(conn) do + ItemsApi.Router.call(conn, ItemsApi.Router.init([])) + end + + describe "GET /health" do + test "returns HTTP 200" do + conn = Plug.Test.conn(:get, "/health") |> call() + + assert conn.status == 200 + end + + test "returns {\"status\": \"ok\"} JSON body" do + conn = Plug.Test.conn(:get, "/health") |> call() + + assert Jason.decode!(conn.resp_body) == %{"status" => "ok"} + end + + test "sets content-type to application/json" do + conn = Plug.Test.conn(:get, "/health") |> call() + + assert Plug.Conn.get_resp_header(conn, "content-type") == ["application/json"] + end + + test "requires no request body" do + conn = Plug.Test.conn(:get, "/health") |> call() + + assert conn.status == 200 + assert Jason.decode!(conn.resp_body) == %{"status" => "ok"} + end + + test "POST /health returns 404" do + conn = Plug.Test.conn(:post, "/health") |> call() + + assert conn.status == 404 + end + + test "PUT /health returns 404" do + conn = Plug.Test.conn(:put, "/health") |> call() + + assert conn.status == 404 + end + + test "DELETE /health returns 404" do + conn = Plug.Test.conn(:delete, "/health") |> call() + + assert conn.status == 404 + end + + test "is idempotent across multiple calls" do + for _ <- 1..3 do + conn = Plug.Test.conn(:get, "/health") |> call() + + assert conn.status == 200 + assert Jason.decode!(conn.resp_body) == %{"status" => "ok"} + end + end + end +end