Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 56 additions & 1 deletion handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package status

import (
"context"
"encoding/json"
"encoding/json/v2"
"errors"
"log/slog"
"net/http"
Expand Down Expand Up @@ -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("<failure>")}}, nil, log, http.StatusServiceUnavailable),
wantError: "<failure>",
},
{
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("<failure>")}}, nil, log, http.StatusServiceUnavailable),
wantError: "<failure>",
},
{
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: "<failure>"}}}, nil, log, http.StatusServiceUnavailable),
wantError: "<failure>",
},
{
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) {
Expand Down
5 changes: 3 additions & 2 deletions health.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package status

import (
"encoding/json"
"encoding/json/jsontext"
"encoding/json/v2"
"log/slog"
"net/http"
"sync/atomic"
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions jobs.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package status

import (
"encoding/json"
"encoding/json/jsontext"
"encoding/json/v2"
"log/slog"
"net/http"
"sync/atomic"
Expand Down Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions ready.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package status

import (
"encoding/json"
"encoding/json/jsontext"
"encoding/json/v2"
"log/slog"
"net/http"
"sync/atomic"
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion tests/helpers/http.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package helpers

import (
"encoding/json"
"encoding/json/v2"
"io"
"net/http"
"testing"
Expand Down
Loading