From 652b604c90eff5ccf794c709bcad9008f4d91987 Mon Sep 17 00:00:00 2001 From: "Alessandro (Ale) Segala" <43508+ItalyPaleAle@users.noreply.github.com> Date: Mon, 12 Oct 2026 13:57:46 -0700 Subject: [PATCH] Add /healthz endpoint to server (#259) Co-authored-by: Michael Eischer --- mux.go | 3 +++ mux_test.go | 16 ++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/mux.go b/mux.go index a6962f4..27225f9 100644 --- a/mux.go +++ b/mux.go @@ -112,6 +112,9 @@ func NewHandler(server *Server) (http.Handler, error) { mux.HandleFunc("/metrics", server.wrapMetricsAuth(promhttp.Handler().ServeHTTP)) } } + mux.HandleFunc("/healthz", func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusNoContent) + }) mux.Handle("/", server) var handler http.Handler = mux diff --git a/mux_test.go b/mux_test.go index 3558825..52c30f1 100644 --- a/mux_test.go +++ b/mux_test.go @@ -80,6 +80,22 @@ func TestCheckAuth(t *testing.T) { } } +func TestHealthz(t *testing.T) { + handler, err := NewHandler(&Server{NoAuth: true}) + if err != nil { + t.Fatal(err) + } + + rec := httptest.NewRecorder() + handler.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/healthz", nil)) + if rec.Code != http.StatusNoContent { + t.Fatalf("status = %d, want %d", rec.Code, http.StatusNoContent) + } + if rec.Body.Len() != 0 { + t.Fatalf("body = %q, want empty", rec.Body.String()) + } +} + func TestUnauthorizedResponsesIncludeBasicChallenge(t *testing.T) { const challenge = `Basic realm="rest-server"`