package httpserver import ( "net/http" "net/http/httptest" "testing" ) func TestHandleHealthz(t *testing.T) { req := httptest.NewRequest(http.MethodGet, "/healthz", nil) rr := httptest.NewRecorder() handleHealthz(rr, req) if status := rr.Code; status != http.StatusOK { t.Errorf("handleHealthz returned wrong status code: got %v want %v", status, http.StatusOK) } expected := "OK" if body := rr.Body.String(); body != expected { t.Errorf("handleHealthz returned unexpected body: got %v want %v", body, expected) } } func TestHandleHealthz_MethodNotAllowed(t *testing.T) { req := httptest.NewRequest(http.MethodPost, "/healthz", nil) rr := httptest.NewRecorder() handleHealthz(rr, req) if status := rr.Code; status != http.StatusMethodNotAllowed { t.Errorf("handleHealthz for POST returned wrong status code: got %v want %v", status, http.StatusMethodNotAllowed) } } func TestHandleReadyz(t *testing.T) { req := httptest.NewRequest(http.MethodGet, "/readyz", nil) rr := httptest.NewRecorder() handleReadyz(rr, req) if status := rr.Code; status != http.StatusOK { t.Errorf("handleReadyz returned wrong status code: got %v want %v", status, http.StatusOK) } expected := "OK" if body := rr.Body.String(); body != expected { t.Errorf("handleReadyz returned unexpected body: got %v want %v", body, expected) } } func TestHandleReadyz_MethodNotAllowed(t *testing.T) { req := httptest.NewRequest(http.MethodPost, "/readyz", nil) rr := httptest.NewRecorder() handleReadyz(rr, req) if status := rr.Code; status != http.StatusMethodNotAllowed { t.Errorf("handleReadyz for POST returned wrong status code: got %v want %v", status, http.StatusMethodNotAllowed) } } func TestServerMux(t *testing.T) { server := NewServer(":0") mux := server.httpServer.Handler.(*http.ServeMux) // Test healthz routing reqHealth := httptest.NewRequest(http.MethodGet, "/healthz", nil) rrHealth := httptest.NewRecorder() mux.ServeHTTP(rrHealth, reqHealth) if rrHealth.Code != http.StatusOK { t.Errorf("mux /healthz failed: got status %v", rrHealth.Code) } // Test readyz routing reqReady := httptest.NewRequest(http.MethodGet, "/readyz", nil) rrReady := httptest.NewRecorder() mux.ServeHTTP(rrReady, reqReady) if rrReady.Code != http.StatusOK { t.Errorf("mux /readyz failed: got status %v", rrReady.Code) } // Test fallback/not found routing reqNotFound := httptest.NewRequest(http.MethodGet, "/unknown", nil) rrNotFound := httptest.NewRecorder() mux.ServeHTTP(rrNotFound, reqNotFound) if rrNotFound.Code != http.StatusNotFound { t.Errorf("mux /unknown routing status code: got %v want %v", rrNotFound.Code, http.StatusNotFound) } }