Add /healthz endpoint to server (#259)
test / Linux Go 1.26.x (push) Canceled after 0s
test / Linux (race) Go 1.27.x (push) Canceled after 0s
test / Linux Go 1.27.x (push) Canceled after 0s
test / lint (push) Canceled after 0s
test / Analyze results (push) Canceled after 0s

Co-authored-by: Michael Eischer <michael.eischer@fau.de>
This commit is contained in:
Alessandro (Ale) Segala
2026-10-12 20:57:46 +00:00
committed by GitHub
co-authored by Michael Eischer
parent a0da64d6cc
commit 652b604c90
2 changed files with 19 additions and 0 deletions
+3
View File
@@ -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
+16
View File
@@ -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"`