From ca42c45321d4d9f703adb2bc1b0c1fa857193022 Mon Sep 17 00:00:00 2001 From: Valery Piashchynski Date: Sun, 8 Nov 2026 00:33:08 +0100 Subject: [PATCH] refactor(status): use JSON v2 Signed-off-by: Valery Piashchynski --- handler_test.go | 57 ++++++++++++++++++++++++++++++++++++++++++- health.go | 5 ++-- jobs.go | 5 ++-- ready.go | 5 ++-- tests/helpers/http.go | 2 +- 5 files changed, 66 insertions(+), 8 deletions(-) diff --git a/handler_test.go b/handler_test.go index ca02d0a..f32db4b 100644 --- a/handler_test.go +++ b/handler_test.go @@ -2,7 +2,7 @@ package status import ( "context" - "encoding/json" + "encoding/json/v2" "errors" "log/slog" "net/http" @@ -101,6 +101,61 @@ func parseJobsReports(t *testing.T, body []byte) []*JobsReport { return reports } +func TestReportJSON(t *testing.T) { + log := slog.New(slog.DiscardHandler) + tests := []struct { + name string + handler http.Handler + wantError string + }{ + { + name: "health error text", + handler: NewHealthHandler(map[string]Checker{"http": &mockChecker{err: errors.New("")}}, nil, log, http.StatusServiceUnavailable), + wantError: "", + }, + { + name: "health invalid error text", + handler: NewHealthHandler(map[string]Checker{"http": &mockChecker{err: errors.New("\xff")}}, nil, log, http.StatusServiceUnavailable), + wantError: "\ufffd", + }, + { + name: "ready error text", + handler: NewReadyHandler(map[string]Readiness{"http": &mockReadiness{err: errors.New("")}}, nil, log, http.StatusServiceUnavailable), + wantError: "", + }, + { + name: "ready invalid error text", + handler: NewReadyHandler(map[string]Readiness{"http": &mockReadiness{err: errors.New("\xff")}}, nil, log, http.StatusServiceUnavailable), + wantError: "\ufffd", + }, + { + name: "jobs error text", + handler: NewJobsHandler(&mockJobsChecker{states: []*jobsApi.State{{ErrorMessage: ""}}}, nil, log, http.StatusServiceUnavailable), + wantError: "", + }, + { + name: "jobs invalid error text", + handler: NewJobsHandler(&mockJobsChecker{states: []*jobsApi.State{{ErrorMessage: "\xff"}}}, nil, log, http.StatusServiceUnavailable), + wantError: "\ufffd", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + rec := httptest.NewRecorder() + tt.handler.ServeHTTP(rec, httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/", nil)) + + var reports []struct { + ErrorMessage string `json:"error_message"` + } + require.NoError(t, json.Unmarshal(rec.Body.Bytes(), &reports)) + require.Len(t, reports, 1) + require.Equal(t, tt.wantError, reports[0].ErrorMessage) + require.NotContains(t, rec.Body.String(), `\u003c`) + }) + } +} + // --- Health Handler Tests --- func TestHealthHandler(t *testing.T) { diff --git a/health.go b/health.go index 42beb3f..66f7cf6 100644 --- a/health.go +++ b/health.go @@ -1,7 +1,8 @@ package status import ( - "encoding/json" + "encoding/json/jsontext" + "encoding/json/v2" "log/slog" "net/http" "sync/atomic" @@ -152,7 +153,7 @@ func (rd *Health) ServeHTTP(w http.ResponseWriter, r *http.Request) { } } - data, _ := json.Marshal(report) + data, _ := json.Marshal(report, jsontext.AllowInvalidUTF8(true)) // write the response _, err := w.Write(data) diff --git a/jobs.go b/jobs.go index ee649ee..ee0bccb 100644 --- a/jobs.go +++ b/jobs.go @@ -1,7 +1,8 @@ package status import ( - "encoding/json" + "encoding/json/jsontext" + "encoding/json/v2" "log/slog" "net/http" "sync/atomic" @@ -58,7 +59,7 @@ func (jb *Jobs) ServeHTTP(w http.ResponseWriter, r *http.Request) { }) } - data, err := json.Marshal(report) + data, err := json.Marshal(report, jsontext.AllowInvalidUTF8(true)) if err != nil { jb.log.Error("failed to marshal jobs state report", "error", err) return diff --git a/ready.go b/ready.go index 43cd721..2c7af76 100644 --- a/ready.go +++ b/ready.go @@ -1,7 +1,8 @@ package status import ( - "encoding/json" + "encoding/json/jsontext" + "encoding/json/v2" "log/slog" "net/http" "sync/atomic" @@ -150,7 +151,7 @@ func (rd *Ready) ServeHTTP(w http.ResponseWriter, r *http.Request) { } } - data, _ := json.Marshal(report) + data, _ := json.Marshal(report, jsontext.AllowInvalidUTF8(true)) // write the response _, err := w.Write(data) diff --git a/tests/helpers/http.go b/tests/helpers/http.go index 4eed8b0..253d868 100644 --- a/tests/helpers/http.go +++ b/tests/helpers/http.go @@ -1,7 +1,7 @@ package helpers import ( - "encoding/json" + "encoding/json/v2" "io" "net/http" "testing"